4 Replies Latest reply on Dec 7, 2006 8:02 AM by dishmael

    Multiple XML files to feed a DataGrid

    M. Huisman Level 1
      Ok, Another problem,

      How can i populate a DataGrid using 2 xml files.

      The first xml contains information about a contactperson, and 2 or more contactmethodid's, which details i'll have to get from the second xml file.

      Is there a fast way to do this?
        • 2. Re: Multiple XML files to feed a DataGrid
          dishmael
          What is the expected outcome? Are you wanting to show the contents of both XML sources in a single DataGrid?
          • 3. Re: Multiple XML files to feed a DataGrid
            M. Huisman Level 1
            Contactperson.xml

            <?xml version="1.0" encoding="UTF-8"?>
            <Contactperson xmlns:p2="http:nl4bintern" id="1">
            <Customerid Type="Primairy">1</Customerid>
            <Prefix>Sir</Prefix>
            <Initials>M.</Initials>
            <Firstname>Mark</Firstname>
            <Middlename/>
            <Lastname>Huisman</Lastname>
            <Suffix/>
            <Function>Director</Function>
            <Communicationid>1</Communicationid>
            <Communicationid>2</Communicationid>
            <Communicationid>3</Communicationid>
            <Communicationid>4</Communicationid>
            <Communicationid>5</Communicationid>
            <Department/>
            <Responsibilities>All</Responsibilities>
            <Businesscard/>
            <Addressid Type="">1</Addressid>
            </Contactperson>


            Communication.xml

            <?xml version="1.0" encoding="UTF-8"?>
            <Communications>
            <Communication id='0'>
            <type>Phone</type>
            <value>01235588</value>
            </Communication>
            <Communication id='1'>
            <type>Mobile</type>
            <value>0614000000</value>
            </Communication>
            <Communication id='2'>
            <type>Fax</type>
            <value>08000005</value>
            </Communication>
            <Communication id='3'>
            <type>Email</type>
            <value>info <at> nl4b.com</value>
            </Communication>
            <Communication id='4'>
            <type>Website</type>
            <value> http://www.nl4b.com</value>
            </Communication>
            <Communication id='5'>
            <type>Phone</type>
            <value>0384220000</value>
            </Communication>
            <Communication id='6'>
            <type>Mobile</type>
            <value>0614707000</value>
            </Communication>
            <Communication id='7'>
            <type>Fax</type>
            <value>0841234567</value>
            </Communication>
            <Communication id='8'>
            <type>Email</type>
            <value>mark.huisman <at> nl4b.com</value>
            </Communication>
            <Communication id='9'>
            <type>Website</type>
            <value> http://www.digisjonnie.nl</value>
            </Communication>
            </Communications>

            Now I want to parse the Communication id's, get the contact info and place it into one single (or two linked datagrids).
            But I don't seem to manage.
            • 4. Re: Multiple XML files to feed a DataGrid
              dishmael Level 1
              I think I understand what you're trying to do. I would recommend combining the two XML sources into a single source if possible simply because it would be easier to deal with the data. If that's not possible, then you'll probably want to store the XML content into an ArrayCollection so it's easier to work with. I would recommend the following change to your ContactPerson XML:

              <Communication>
              <Communicationid>1</Communicationid>
              <Communicationid>2</Communicationid>
              <Communicationid>3</Communicationid>
              <Communicationid>4</Communicationid>
              <Communicationid>5</Communicationid>
              </Communication>

              Storing the ContactPerson XML as the dataProvider for a DataGrid then gives you the start of what you want. The next part is creating some sort of filter or lookup against the ArrayCollection containing the content of the Commnication XML file. I started to put this together for you but ran out of time. Here's the code for where I was going:

              Contacts.xml

              <?xml version="1.0" encoding="UTF-8"?>
              <Contacts>
              <Person>
              <Customerid Type="Primary">1</Customerid>
              <Prefix>Sir</Prefix>
              <Initials>M.</Initials>
              <Firstname>Mark</Firstname>
              <Middlename/>
              <Lastname>Huisman</Lastname>
              <Suffix/>
              <Function>Director</Function>
              <Communications>
              <Communicationid>1</Communicationid>
              <Communicationid>3</Communicationid>
              <Communicationid>5</Communicationid>
              </Communications>
              <Department/>
              <Responsibilities>All</Responsibilities>
              <Businesscard/>
              <Addressid Type="">1</Addressid>
              </Person>
              <Person>
              <Customerid Type="Primary">2</Customerid>
              <Prefix>Sir</Prefix>
              <Initials>A.</Initials>
              <Firstname>John</Firstname>
              <Middlename/>
              <Lastname>Doe</Lastname>
              <Suffix/>
              <Function>Director</Function>
              <Communications>
              <Communicationid>2</Communicationid>
              <Communicationid>4</Communicationid>
              </Communications>
              <Department/>
              <Responsibilities>All</Responsibilities>
              <Businesscard/>
              <Addressid Type="">1</Addressid>
              </Person>
              </Contacts>

              Communication.xml

              <?xml version="1.0" encoding="UTF-8"?>
              <Communications>
              <Communication id='0'>
              <type>Phone</type>
              <value>01235588</value>
              </Communication>
              <Communication id='1'>
              <type>Mobile</type>
              <value>0614000000</value>
              </Communication>
              <Communication id='2'>
              <type>Fax</type>
              <value>08000005</value>
              </Communication>
              <Communication id='3'>
              <type>Email</type>
              <value>info [at] nl4b.com</value>
              </Communication>
              <Communication id='4'>
              <type>Website</type>
              <value> http://www.nl4b.com</value>
              </Communication>
              <Communication id='5'>
              <type>Phone</type>
              <value>0384220000</value>
              </Communication>
              <Communication id='6'>
              <type>Mobile</type>
              <value>0614707000</value>
              </Communication>
              <Communication id='7'>
              <type>Fax</type>
              <value>0841234567</value>
              </Communication>
              <Communication id='8'>
              <type>Email</type>
              <value>mark.huisman [at] nl4b.com</value>
              </Communication>
              <Communication id='9'>
              <type>Website</type>
              <value> http://www.digisjonnie.nl</value>
              </Communication>
              </Communications>

              Main.mxml

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" initialize="{init()}">

              <mx:Script>
              <![CDATA[
              import mx.utils.ArrayUtil;
              import mx.collections.ArrayCollection;
              import mx.controls.Alert;
              import mx.rpc.events.ResultEvent;

              [Bindable] private var comms:ArrayCollection;

              private function init():void {
              comms = new ArrayCollection();

              commsrv.send();
              personsrv.send();
              }

              private function handleComms(event:ResultEvent):void {
              comms.source = ArrayUtil.toArray(event.result.Communications.Communication);
              }

              private function personSelect(event:Event):void {

              }

              ]]>
              </mx:Script>

              <mx:HTTPService id="commsrv" url="xml/Communication.xml" result="{handleComms(event)}" resultFormat="e4x" />
              <mx:HTTPService id="personsrv" url="xml/Contacts.xml" />

              <mx:Panel x="10" y="10" width="555" height="450" layout="absolute">
              <mx:DataGrid id="people" dataProvider="{personsrv.lastResult.Contacts.Person}" itemClick="{personSelect(event)}"
              x="10" y="10" height="227" width="515">
              <mx:columns>
              <mx:DataGridColumn headerText="ID" dataField="Customerid"/>
              <mx:DataGridColumn headerText="First Name" dataField="Firstname"/>
              <mx:DataGridColumn headerText="Last Name" dataField="Lastname"/>
              </mx:columns>
              </mx:DataGrid>
              <mx:DataGrid dataProvider="{comms}" x="10" y="245" height="155" width="515">
              <mx:columns>
              <mx:DataGridColumn headerText="Type" dataField="type"/>
              <mx:DataGridColumn headerText="Value" dataField="value"/>
              </mx:columns>
              </mx:DataGrid>
              </mx:Panel>

              </mx:Application>

              Wish I had more time to help, but maybe this will get you pointed in the right direction. GOOD LUCK!