SimpleXML 搜索子节点并检索所有节点
SimpleXML Search Children and Retrieve all Nodes
或多或少我需要指导才能继续前进..
有一个页面显示容器中的每个节点(对其执行操作)
XML
<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>
这部分采用 XML 并回显值等..
$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
}
我想要做的是有一个搜索字段,该字段采用该搜索关键字并搜索所有子节点,然后搜索匹配的每个节点+子节点。
老实说,我只是希望得到有关如何实现此目标的指导。我知道如何通过 id= 单独获取一个节点,但在那之后..需要指导。
作为快速示例,使用 XPath 搜索 <text>
元素(我更改了您提供的示例数据以显示其选择内容的差异)
$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
XPath - //data[contains(text,"'.$textSearch.'")]
基本上是说要找到任何 <data>
元素,其中 <text>
元素的值包含要搜索的字符串。您可以通过更改 text
来更改它使用的字段
或多或少我需要指导才能继续前进..
有一个页面显示容器中的每个节点(对其执行操作)
XML
<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>
这部分采用 XML 并回显值等..
$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
}
我想要做的是有一个搜索字段,该字段采用该搜索关键字并搜索所有子节点,然后搜索匹配的每个节点+子节点。
老实说,我只是希望得到有关如何实现此目标的指导。我知道如何通过 id= 单独获取一个节点,但在那之后..需要指导。
作为快速示例,使用 XPath 搜索 <text>
元素(我更改了您提供的示例数据以显示其选择内容的差异)
$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
XPath - //data[contains(text,"'.$textSearch.'")]
基本上是说要找到任何 <data>
元素,其中 <text>
元素的值包含要搜索的字符串。您可以通过更改 text