任何人都可以帮我阅读大文件吗? 我正在阅读项目:
$xmlReader = new XMLReader();
$xmlReader->open($path);
while ($xmlReader->read() && $xmlReader->name !== 'item') ;
while ($xmlReader->name == 'item')
{
$node = new SimpleXMLElement($xmlReader->readOuterXML());
foreach($node->properties as $property)
{
var_dump($property->price); //empty class SimpleXMLElement
var_dump($property->attributes()); //empty class SimpleXMLElement
}
$xmlReader->next('item');
}
$xmlReader->close();
所以我无法阅读 $ property->价格,因为我收到空类SimpleXMLElement
属性 sku 的情况相同我也收到空类SimpleXMLElement
我无法转换为(string),因为我收到空字符串。
如何阅读子节点? 源XML:
<items>
<item>
<code>be274178-9039-11e6-86d0-001e6727034e</code>
<delete>0</delete>
<title>Полотенцесушитель М-обр. 500*500 нар. р. 1" арт.00004-5050</title>
<category>13760cb9-8f7b-11e6-86d0-001e6727034e</category>
<producer>5a457cfd-b088-11e2-9c54-001e6727034e</producer>
<properties>
<property sku="40 451">
<price>2831.00</price>
<characteristics>
<characteristic>
<title>Свойство</title>
<value>накопление</value>
<filter>0</filter>
<visible>0</visible>
</characteristic>
<characteristic>
<title>Форма</title>
<value>М-образный</value>
<filter>1</filter>
<visible>1</visible>
</characteristic>
</characteristics>
</property>
<property sku="40 464">
<price>3442.00</price>
<characteristics>
<characteristic>
<title>Свойство</title>
<value>накопление</value>
<filter>0</filter>
<visible>0</visible>
</characteristic>
</characteristics>
</property>
</properties>
</item>
</items>
答案 0 :(得分:1)
应该是
foreach($node->properties->property as $property)
因为它只有一个<properties>
个元素,但很多<property>
元素。
请参阅PHP手册中的Example #4 Accessing non-unique elements in SimpleXML:
当元素的多个实例作为单个父元素的子元素存在时,将应用常规迭代技术。
<?php include 'example.php'; $movies = new SimpleXMLElement($xmlstr); /* For each <character> node, we echo a separate <name>. */ foreach ($movies->movie->characters->character as $character) { echo $character->name, ' played by ', $character->actor, PHP_EOL; } ?>