如何从包含 Product = Product1 的记录中获取所有子注释并将它们放入 JSON 数组中?
<RECORD>
<PROP NAME="Product">
<PVAL><!CDATA[Produkt1]]</PVAL>
<PROP NAME="Value">
<PVAL><!CDATA[10]]</PVAL>
<PROP NAME="Status">
<PVAL><!CDATA[Active]]</PVAL>
</RECORD>
<RECORD>
<PROP NAME="Product">
<PVAL><!CDATA[Produkt2]]</PVAL>
<PROP NAME="Value">
<PVAL><!CDATA[20]]</PVAL>
<PROP NAME="Status">
<PVAL><!CDATA[Active]]</PVAL>
</RECORD>
<RECORD>
<PROP NAME="Product">
<PVAL><!CDATA[Produkt3]]</PVAL>
<PROP NAME="Value">
<PVAL><!CDATA[30]]</PVAL>
<PROP NAME="Status">
<PVAL><!CDATA[Active]]</PVAL>
</RECORD>
我需要 iPad 应用程序的数据,调用类似 www.products.com/product.php?Product1
让 JSON 看起来像
{
"Product": [
{ "Name":"Product1" , "Value":"10" , "Status":"Active" }
]
}
编辑:使用 XML 阅读器的解决方案
<?php
$z = new XMLReader;
$z->open('products.xml');
$doc = new DOMDocument;
// move to the first <product /> node
while ($z->read() && $z->name !== 'RECORD');
// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'RECORD')
{
$node = simplexml_import_dom($doc->importNode($z->expand(), true));
$strvalue = $node->PROP[6]->PVAL;
echo $strvalue."<p>" ;
// go to next <product />
$z->next('RECORD');
}
?>