tags:

views:

38

answers:

1

Hi Im trying to parse an xml feed using simplexml in php. The xml feed is laid out as follows:

<Member>
    <MemberType>Full</MemberType>
    <JoinDate>2010-06-12</JoinDate>
    <DataType>A</DataType>
    <Data>
        <FirstName>Ted</FirstName>
        <LasttName>Smith</LasttName>
        <Data1>56</Data1>
        <Data2>100</Data2>
        <Data3>120</Data3>
    </Data>
</Member>
<Member>
    <MemberType>Full</MemberType>
    <JoinDate>2010-06-12</JoinDate>
    <DataType>B</DataType>
    <Data>
        <FirstName>Ted</FirstName>
        <LasttName>Smith</LasttName>
        <Data1>57</Data1>
        <Data2>110</Data2>
        <Data3>130</Data3>
    </Data>
</Member>
<Member>
    <MemberType>Full</MemberType>
    <JoinDate>2010-06-12</JoinDate>
    <DataType>C</DataType>
    <Data>
        <FirstName>Ted</FirstName>
        <LasttName>Smith</LasttName>
        <Data4>58</Data4>
        <Data5>115</Data5>
        <Data6>230</Data6>
    </Data>
</Member>

where the member element loops over and over again in the xml doc, but the data inside it changes. What im trying to do is enter all the data for certain members into an sql database. So ideally i want to enter this all in one line in the db. The xml feed contains different member types, like 'full' or 'associate'.

At the moment i am trying to loop through all the full members, and get all the data for this particular member. The data for each member is broken up into three parts each with a separate member tag, so above Ted Bloggs has data in three member tags, where Datatype is A, B and C

$PLF = simplexml_load_file('../XML/Members.xml');

foreach ($PLF->Root->xpath('//Member') as $member) {

    if ($member->MemberType == 'Full') {


        echo $member->MemberType.'<br/>';
        echo $member->JoinDate.'<br />';
        echo $member->DataType.'<br/>';
        echo $member->Data->FirstName.'<br/>';
        echo $member->Data->LastName.'<br/>';
        echo $member->Data->Data1.'<br/>';
        echo $member->Data->Data2.'<br/>';
        echo '<br />';


        }}

the code i have at the moment can only pull data from the first type (type A) in each loop, and i really want to combine all types A, B, and C into the same loop. So i can get all the data for each member like Ted Smith into one line in the DB.

+1  A: 

I use simple xml to read some remote files and loop thru them as well. This is my code:

         $sUrl      = "some url";
         $sContent  = file_get_contents($sUrl);

        $oXml       = new SimpleXMLElement($sContent);

        $aReturn    = array();

        foreach ($oXml->children() as $oStation)
        {
            $iRackId    = (int)$oStation->rack_id;
            $dLong      = (double)str_replace(",", ".", $oStation->longitute);
            $dLati      = (double)str_replace(",", ".", $oStation->latitude);
            $sDescription = (string)$oStation->description;

            $aRes   = array();
            $aRes['rack_id']        = $iRackId;
            $aRes['longitute']      = $dLong;
            $aRes['latitude']       = $dLati;
            $aRes['description']    = utf8_decode($sDescription);

            if ($dLong > 0 && $dLati > 0)
                $aReturn[$iRackId]  = $aRes;
        }

What I do is I put the result of the XML file into an array. Later in the code I save that data to the database as well.

Hope this helped you. I don't use xpath... had nothing but problems with it and didn't had the time to sort them out. This seems to work for me though.

Br, Paul Peelen

Paul Peelen