PDA

View Full Version : Using simpleXML with attributes?


stfc_boy
05-12-2010, 06:21 PM
Hello,

Wondering if anyone can help. I'm not too familiar with XML, but when I do use it I usually use it like so:



<?php

$feed='table.xml';
$xmlfeed = file_get_contents($feed);
$xml = new SimpleXMLElement($xmlfeed);

foreach ( $xml->team as $team ) {
echo $position = $team->position."<br/>";
echo $name = $team->name."<br/>";
echo $played = $team->played."<br/>";
echo $won = $team->won."<br/>";
echo $drawn = $team->drawn."<br/>";
} /* End For Each */

?>



Now I normally use this style for all XML documents I wish to parse, but i've now come across one with attributes like so:


<?xml version="1.0"?>
<DataSet>
<Row rowNum="1">
<Column name="EntryNum"><Val xsi:type="xsd:string">First Enrty</Val></Column>
<Column name="StartDate"><Val xsi:type="xsd:date">2009-11-10</Val></Column>
<Column name="EndDate"><Val xsi:type="xsd:date">2009-12-10</Val></Column>
<Column name="Number"><Val xsi:type="xsd:integer">33940</Val></Column>
<Column name="Order"><Val xsi:type="xsd:integer">23</Val></Column>
</Row>
<Row rowNum="2">
<Column name="EntryNum"><Val xsi:type="xsd:string">Second Entry</Val></Column>
<Column name="StartDate"><Val xsi:type="xsd:date">2009-11-10</Val></Column>
<Column name="EndDate"><Val xsi:type="xsd:date">2009-12-10</Val></Column>
<Column name="Number"><Val xsi:type="xsd:integer">33940</Val></Column>
<Column name="Order"><Val xsi:type="xsd:integer">23</Val></Column>
</Row>
</DataSet>


But i'm not sure how to grab the various data where attriubutes are involved. Here is below what I tried, but I failed miserably!


<?php

$feed='file.xml';
$xmlfeed = file_get_contents($feed);
$xml = new SimpleXMLElement($xmlfeed);

foreach ( $xml->Row->attributes->rowNum as $data ) {
echo $entry_no = $data->Column->attributes->name->Val->attributes->xsi:type."<br/>";
//Here I want to echo the value 'First Enrty'
echo $order_start = $data->Column->attributes->name->Val->attributes->xsi:type."<br/>";
//Here I want to echo the value '2009-11-10'
echo $order_end = $data->Column->attributes->name->Val->attributes->xsi:type."<br/>";
//Here I want to echo the value '2009-12-10'
echo $number = $data->Column->attributes->name->Val->attributes->xsi:type."<br/>";
//Here I want to echo the value '33940'
echo $order = $data->Column->attributes->name->Val->attributes->xsi:type."<br/>";
//Here I want to echo the value '23'

} /* End For Each and grab the second values*/

?>


Can anyone kindly help me?

Thanks

Dormilich
05-12-2010, 07:17 PM
the xsi in xsi:type refers to a namespace. see “SimpleXML and Namespaces” (http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/)