Reputation: 4686
I've got some XML that looks a bit like this:
<item code="1">
<description>This is an item.</description>
<prices>
<price nett="1.50"/>
<price code="RPP" nett="20.33" />
</prices>
</item>
And I'm trying to import it into our DB using PHP like this:
$xmlurl = 'thexmlfile.xml';
$dom = new DOMDocument();
$dom->load($xmlurl);
$records = $dom->getElementsByTagName('item');
foreach($records as $record) {
$descs = $record-> getElementsByTagName('description');
$desc = $descs->item(0)->nodeValue;
echo $desc . '<br />';
}
That works fine. It pulls the description. However, how do I get the item code and the prices section? Loop within a loop?]
Thank you
Upvotes: 0
Views: 2903
Reputation: 895
Edited: I corrected the code, iterating through the prices for each item separately.
To get the item code, do the following;
$record->getAttribute('code');
You can loop through the prices of this item by looping within the loop like you said. To access the prices do the following;
$pricesTag = $record->getElementsByTagName('prices');
$prices = $pricesTag->item(0)->getElementsByTagName('price');
Then repeat the shenanigans of looping and accessing the node values and attributes of the prices all over.
Source: similar question
Working example: corrected code
Upvotes: 1