4 Replies Latest reply on Jul 18, 2006 12:27 PM by michaeljbaird

    How to populate a grid from xml file?

    krishilife
      Hi,

      I wonder how to populate a grid from xml file located in c:\...\sample.xml file?
      The file actually is located in the same folder as the project. Any working sample would be great.

      Thanx,

      Kris.
        • 1. Re: How to populate a grid from xml file?
          njadobe Level 1
          Hi,

          If your data file looks something like this:

          <employees>
          <employee name="John Smith" phone="800-555-1212" email="john@smith.com"/>
          <!-- ... -->
          </employees>

          You should be able to do something like this:

          <!-- ... -->
          <mx:Model id="sample" source="sample.xml"/>
          <mx:DataGrid dataProvider="{sample.employee}">
          <mx:columns>
          <mx:DataGridColumn dataField="name" headerText="Name"/>
          <mx:DataGridColumn dataField="phone" headerText="Phone"/>
          <mx:DataGridColumn dataField="email" headerText="Email"/>
          </mx:columns>
          </mx:DataGrid>

          where the "dataField" entries in the DataGridColumn tag correspond to properties or child tag names in your XML. Depending on the format of your data, you may need to do something more complex, but you should be able to try this as a starting point.

          Thanks,

          nj
          Flex Builder team
          • 2. Re: How to populate a grid from xml file?
            krishilife Level 1
            Thanx for quick answer. It works fine.
            BTW, I found some good and simple examples in Flex 1.5 section. There is one with example "employee". I've tried to use HTTPService as showed below, and for some reason I keep getting error "Access of possibly undefined property employee through a reference with static type mx.rpc.http.mxml:HTTPService". I get this error even if upload employee.xml file to the WEBServer, and point "url=..." to it.

            Employee file::

            <?xml version="1.0" encoding="utf-8"?>
            <employess>
            <employee>
            <name>Christina Coenraets</name>
            <phone>555-219-2270</phone>
            <email>ccoenraets@fictitious.com</email>
            <active>true</active>
            </employee>
            <employee>
            <name>Louis Freligh</name>
            <phone>555-219-2100</phone>
            <email>lfreligh@fictitious.com</email>
            <active>true</active>
            </employee>
            <employee>
            <name>Ronnie Hodgman</name>
            <phone>555-219-2030</phone>
            <email>rhodgman@fictitious.com</email>
            <active>false</active>
            </employee>
            <employee>
            <name>Joanne Wall</name>
            <phone>555-219-2012</phone>
            <email>jwall@fictitious.com</email>
            <active>true</active>
            </employee>
            <employee>
            <name>Maurice Smith</name>
            <phone>555-219-2012</phone>
            <email>maurice@fictitious.com</email>
            <active>false</active>
            </employee>
            <employee>
            <name>Mary Jones</name>
            <phone>555-219-2000</phone>
            <email>mjones@fictitious.com</email>
            <active>true</active>
            </employee>
            </employess>

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
            <mx:HTTPService
            id="employeeModel" urlid="employeeModel" url="../employees.xml" useProxy="false"/>
            <mx:DataGrid height="300" width="100%" dataProvider="{employeeModel.employee}"/>
            </mx:Application>
            • 3. Re: How to populate a grid from xml file?
              Level 7
              Thanx for quick answer. It works fine.
              BTW, I found some good and simple examples in Flex 1.5 section. There is one
              with example "employee". I've tried to use HTTPService as showed below, and for
              some reason I keep getting error "Access of possibly undefined property
              employee through a reference with static type mx.rpc.http.mxml:HTTPService". I
              get this error even if upload employee.xml file to the WEBServer, and point
              "url=..." to it.

              Employee file::

              <?xml version="1.0" encoding="utf-8"?>
              <employess>
              <employee>
              <name>Christina Coenraets</name>
              <phone>555-219-2270</phone>
              <email>ccoenraets@fictitious.com</email>
              <active>true</active>
              </employee>
              <employee>
              <name>Louis Freligh</name>
              <phone>555-219-2100</phone>
              <email>lfreligh@fictitious.com</email>
              <active>true</active>
              </employee>
              <employee>
              <name>Ronnie Hodgman</name>
              <phone>555-219-2030</phone>
              <email>rhodgman@fictitious.com</email>
              <active>false</active>
              </employee>
              <employee>
              <name>Joanne Wall</name>
              <phone>555-219-2012</phone>
              <email>jwall@fictitious.com</email>
              <active>true</active>
              </employee>
              <employee>
              <name>Maurice Smith</name>
              <phone>555-219-2012</phone>
              <email>maurice@fictitious.com</email>
              <active>false</active>
              </employee>
              <employee>
              <name>Mary Jones</name>
              <phone>555-219-2000</phone>
              <email>mjones@fictitious.com</email>
              <active>true</active>
              </employee>
              </employess>

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
              <mx:HTTPService
              id="employeeModel" urlid="employeeModel" url="../employees.xml"
              useProxy="false"/>
              <mx:DataGrid height="300" width="100%"
              dataProvider="{employeeModel.employee}"/>
              </mx:Application>

              • 4. Re: How to populate a grid from xml file?
                michaeljbaird
                What happens if you have a little more complex xml file like:

                <?xml version="1.0" encoding="utf-8"?>
                <employess>
                <employee>
                <name level="1">Christina Coenraets</name>
                <address>
                <city>Miami</city>
                <state>FL</state>
                </address>
                <phone>555-219-2270</phone>
                <email>ccoenraets@fictitious.com</email>
                <active>true</active>
                </employee>

                how would you go about getting.displaying the data from level and address?