如何使用xpath检索xml并将其作为responseXML发送回客户端js?
我有php作为服务器,js作为客户端,需要指定的数据并显示为html表。
这是我的XML
// goods.xml
<items>
<item>
<id>1</id>
<itemname>Apple iPhone X</itemname>
<itemqty>20</itemqty>
</item>
<item>
<id>2</id>
<itemname>Apple iPhone 7</itemname>
<itemqty>20</itemqty>
</item>
<item>
<id>3</id>
<itemname>Apple iPhone 8</itemname>
<itemqty>2</itemqty>
</item>
</items>
我想要那些数量超过10的物品,但我的php文件只能得到其中之一
// handle.php
$xmlFile = "../../data/goods.xml";
$doc->load($xmlFile);
$xpath = new DOMXPath($doc);
$xml = new SimpleXMLElement($xmlFile, NULL, TRUE);
$nodes = $xml->xpath("/items/item[itemqty>10]");
echo $doc->saveXML($xpathresultset->item(0)); // send the xml response back to the client
然后我只得到第一个结果,我不能同时得到两个结果(ID 1和ID 2)
<item>
<id>1</id>
<itemname>Apple iPhone X</itemname>
<itemqty>20</itemqty>
</item>
但我想要
<item>
<id>1</id>
<itemname>Apple iPhone X</itemname>
<itemqty>20</itemqty>
</item>
<item>
<id>2</id>
<itemname>Apple iPhone 7</itemname>
<itemqty>20</itemqty>
</item>
任何帮助,将不胜感激!!
This is probably more easily done using
DOMDocument
thanSimpleXML
, as you can then usexpath
to search for nodes withitemqty <= 10
and remove them from the document:输出: