嗨,我的xml有这个结构。
<articles>
<article>
<title></title>
<text></text>
<notes>
<note>
<code></code>
<text></text>
</note>
<note>
<code></code>
<text></text>
</note>
</notes>
</article>
</articles>我必须执行以下查询( SELECT : ):选择note.code=XXX和note.text包含YYY的文章
我如何在php中做到这一点?
发布于 2014-05-28 20:57:14
您可以使用Xpath作为xml的查询语言。W3School这里有一个很好的教程:http://www.w3schools.com/XPath/
使用SimpleXML很简单:P:
$xml = new SimpleXMLElement($xmlstring);
$result = $xml->xpath('/articles/article[code=XXX] and /articles/article[text contains(YYY)]');
foreach ($result as $node)
{
//Do stuff
}发布于 2014-05-28 20:56:03
如果使用SimpleXML,则必须对所有子节点进行迭代,然后对其子节点进行递归。看看DOMDocument和getElementsByTagName方法。
我想,这就是你要找的东西。
http://www.php.net/manual/en/class.domdocument.php
http://www.php.net/manual/en/domdocument.getelementsbytagname.php
https://stackoverflow.com/questions/23921559
复制相似问题