0

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

4

1 回答 1

1

编辑:我更正了代码,分别遍历了每个项目的价格。

要获取项目代码,请执行以下操作;

$record->getAttribute('code');

您可以像您说的那样通过在循环中循环来循环该项目的价格。要访问价格,请执行以下操作;

$pricesTag = $record->getElementsByTagName('prices');
$prices = $pricesTag->item(0)->getElementsByTagName('price');

然后重复循环和访问节点值和价格属性的恶作剧。

来源:类似问题

工作示例:更正的代码

于 2013-03-06T16:17:49.200 回答