通过php搜索XML数据
我正在尝试使用php搜索XML文件中的数据。 我不想获取某些元素的值,如果我想要,我会使用xpath。通过php搜索XML数据
这是我的XML文件的例子:
<root>
<author>foo</author>
<date>bar</date>
</root>
比方说,我的客户希望搜索词“工厂”。 我想要所有带字符'f'和'b'和'a'的字符串返回。 所以输出将是:
Foo
bar
作者姓名可能是詹姆斯·西城为例。
<author>James Westside</author>
而且用户搜索jam
它将返回James Westside
我希望我的问题是清楚的。
您应该使用PHP:s XMLReader类。 XMLReader充当向文档流前进的光标,并在路上的每个节点处停止。
事情是这样的:
$search_phrase = 'fab';
$xml = new XMLReader;
$xml->open('your-xml-file.xml');
while ($xml->read()) {
$node = $xml->expand();
/* Looping through all elements in the XML */
/* Test if the current node is a text node: */
if ($node->nodeType == XMLReader::TEXT) {
/* Loop all letters in search_phrase */
for ($i = 0; $i < strlen($search_phrase); $i++) {
/* Test if the text in the text node is matching any letter i search_phrase: */
if (strpos($node->nodeValue, substr($search_phrase, $i, 1)) !== false) {
echo($node->nodeValue . "\n");
break;
}
}
}
}
试过这个,应该是某些东西应该在引号或什么的东西 我得到一个解析错误 解析错误:语法错误,意想不到的T_STRING在/var/www/other_projects/musify/search_output.php在线7是'如果($ xml-> readString()的东西){' – 2012-03-28 12:22:23
你能告诉我如果($ xml-> readString()东西) readString()导致错误之后的东西... – 2012-03-28 12:39:14
Hello Marshall!我使我的示例代码更加详细。 – 2012-03-28 14:08:40
请出示一些代码,你已经尝试 – 2012-03-28 10:02:31
我还没有在此刻尝试新鲜事物,即时通讯在做什么,因为即时寻找从XML文件中的字符串很困惑,而比抓取元素的值 – 2012-03-28 10:07:55