[英]XML parsing using simplexml and xpath
i trying to get name 'property' atributtes from this sample xml: 我试图从此示例xml中获取名称“属性”属性:
<offers>
<offer><id>1578</id>
<instock>13</instock>
</offer>
<offer><id>1579</id>
<property name="EAN">634932593250256</property>
<property name="Dostępne kolory">Niebiesko-Biały</property>
<property name="Dostępne rozmiary">L</property>
<instock>1</instock>
</offer>
</offers>
to something like this: 像这样:
if property dosen't exist
iD > 1578
if property exist:
iD > 1579
EAN > 634932593250256
Dostępne kolory > Niebiesko-Biały
Dostępne rozmiary > L
but i don't know how do this :/ maybe someone can help me 但我不知道该怎么做:/也许有人可以帮助我
my code: 我的代码:
$xml = simplexml_load_file($xmlfile);
$xml_offer = $xml->xpath("//offer");
$total = count($xml_offer);
for($i=0;$i<=$total;$i++){
echo $xml_offer[$i]->id.' ';
foreach ($xml_offer[$i]->children() as $child) {
echo $child['name'];
}
echo ' <br>';
}
but its only show me the name of childern like 'Dostępne rozmiary' how can i get value? 但它只会向我显示孩子的名字,例如“Dostępnerozmiary”,我如何获得价值?
You don't really need xpath here. 您这里实际上不需要xpath。 You can process this XML using the SimpleXMLElement php class like this: 您可以使用SimpleXMLElement php类来处理此XML,如下所示:
<pre>
<?php
$xmlStr = '<offers>
<offer><id>1578</id>
<instock>13</instock>
</offer>
<offer><id>1579</id>
<property name="EAN">634932593250256</property>
<property name="Dostępne kolory">Niebiesko-Biały</property>
<property name="Dostępne rozmiary">L</property>
<instock>1</instock>
</offer>
</offers>';
$xml = new SimpleXMLElement($xmlStr);
foreach ($xml->offer as $offer)
{
print_r($offer);
}
Outputs: 输出:
SimpleXMLElement Object
(
[id] => 1578
[instock] => 13
)
SimpleXMLElement Object
(
[id] => 1579
[property] => Array
(
[0] => 634932593250256
[1] => Niebiesko-Biały
[2] => L
)
[instock] => 1
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.