Php SimpleXML搜索子节点并检索所有节点
我或多或少需要一些指导来推进这项工作 有一个显示容器中每个节点的页面(对其进行填充) XML 我想做的是有一个搜索字段,该字段接受该搜索关键字并搜索所有子项,然后搜索匹配的每个节点+子项Php SimpleXML搜索子节点并检索所有节点,php,xml,search,simplexml,Php,Xml,Search,Simplexml,我或多或少需要一些指导来推进这项工作 有一个显示容器中每个节点的页面(对其进行填充) XML 我想做的是有一个搜索字段,该字段接受该搜索关键字并搜索所有子项,然后搜索匹配的每个节点+子项 老实说,我只是想知道如何做到这一点。我知道如何通过id=单独抓取节点,但之后。。需要指导。作为一个快速示例,使用XPath搜索元素(我已经更改了您提供的示例数据,以显示它选择的内容的差异) XPath-//data[contains(text,“.$textSearch.”]基本上表示查找任何元素,该元素的元素
老实说,我只是想知道如何做到这一点。我知道如何通过id=单独抓取节点,但之后。。需要指导。作为一个快速示例,使用XPath搜索
元素(我已经更改了您提供的示例数据,以显示它选择的内容的差异)
XPath-
//data[contains(text,“.$textSearch.”]
基本上表示查找任何
元素,该元素的
元素的值包含要搜索的字符串。您只需更改文本即可更改它使用的字段提示:使用这是一个地狱般的“快速示例”:-)。。我将尝试一下,但我已经看到代码的逻辑是有意义的。本质上是寻找[contains…part,我没有意识到xpath(…part是.Thx)有多灵活
<archive>
<data id="1111">
<name>Name</name>
<text>Lots of stuff and things</text>
<etc>Etc</etc>
</data>
<data id="2222">
<name>Name</name>
<text>Different stuff and things</text>
<etc>Etc</etc>
</data>
<data id="3333">
<name>Name</name>
<text>More stuff and things</text>
<etc>Etc</etc>
</data>
// and so on
</archive>
$master = array_slice($xml_get->xpath('data'), $start_page, 25);
$master = array_reverse($master);
foreach($master as $arc) {
$last_name = $arc[0]->name;
$last_data = $arc[0]->data;
$last_etc = $arc[0]->etc;
// does stuff with values
}
$data = '<archive>
<data id="1111">
<name>Name</name>
<text>Lots of stuff and things</text>
<etc>Etc</etc>
</data>
<data id="2222">
<name>Name</name>
<text>Different stuff and things</text>
<etc>Etc</etc>
</data>
<data id="3333">
<name>Name</name>
<text>More stuff and other things</text>
<etc>Etc</etc>
</data>
</archive>';
$xml_get = simplexml_load_string($data);
$textSearch = "stuff and things";
$matches = $xml_get->xpath('//data[contains(text,"'.$textSearch.'")]');
foreach($matches as $arc) {
echo "text=".$arc->text.PHP_EOL;
}
text=Lots of stuff and things
text=Different stuff and things