Php SimpleXML查询

Php SimpleXML查询,php,xml,simplexml,Php,Xml,Simplexml,嗨,我的xml有这样的结构 <articles> <article> <title></title> <text></text> <notes> <note> <code></code> <text></text> </note> <no

嗨,我的xml有这样的结构

<articles>
    <article>
    <title></title>
    <text></text>
    <notes>
          <note>
        <code></code>
        <text></text>
    </note>
    <note>
        <code></code>
        <text></text>
      </note>
    </notes>
  </article>

</articles>
我必须执行以下查询: 选择note.code=XXX且note.text包含YYY的文章


如何在php中做到这一点?

如果使用SimpleXML,则必须迭代所有子对象,然后递归它们的子对象。查看DOMDocument和getElementsByTagName方法

我想,这就是你要找的


您可以使用Xpath作为xml的查询语言。这里有一个很好的教程:

使用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
}

您可以将XPath与SimpleXml结合使用来实现这一点:忽略了这一点。很高兴知道。我仍然认为使用DOMDocument更容易实现这一点,它类似于javascript dom接口。这是我的真实代码,具有真实且稍微复杂的xml结构$massime=$xml->xpath'//prouncia[massime/massima/norme/norma/norma[data contains.$str1.]]和//prouncia[massime/massima/norme/norma/numero='.$num.];我得到错误SimpleXMLElement::xpath:无效谓词:$massime=$xml->xpath'//Prononia[massime/massima/Normae/norma[数据包含'.$str1.]]和//Prononia/massime/massima/norma/norma/[numero='.$num.];我随意处理这个问题,很久没有编写xpath了,所以它可能是错误的。您的代码仍然有一个错误无效谓词。我解决了这个问题。我怎样才能让你的答案被接受$xml->xpath'//prouncia[massime/massima/norme/norma/data[containstext',.$str1.]和massime/massima/norme/norma/numero='.$num.]';