使用regex-phpxpatch搜索XML标记
我有一个XML文档:使用regex-phpxpatch搜索XML标记,php,xml,regex,search,xpath,Php,Xml,Regex,Search,Xpath,我有一个XML文档: <product> <item> <item00> <name>DVD</name> </item00> </item> </product> <product> <item> <item11> <name>CD&
<product>
<item>
<item00>
<name>DVD</name>
</item00>
</item>
</product>
<product>
<item>
<item11>
<name>CD</name>
</item11>
</item>
</product>
数字化视频光盘
光盘
我想显示这些产品的名称,但有些产品的项目为“item00”和“item11”
我尝试在XPath中添加路径正则表达式,但没有成功
是否可以使用XPath显示这些产品(DVD和CD)的名称
<?php
$xml = 'file.xml';
$content = '';
$f = fopen($xml, 'r');
while($data = fread($f, filesize($xml))) {
$content.= $data;
}
fclose($f);
preg_match_all('/\<product\>(.*?)\<\/product\>/s', $content, $product);
$product = $product[1];
$doc = new SimpleXMLElement($content);
for($i = 0; $i <= count($product) - 1; $i++) {
// So far, no problems. Seriously.
// The issue starts here.
$query = $doc->xpath('/product/item/???');
foreach($query as $item) {
echo $item->name . '<br>';
}
}
?>
其中“?”是“项目00”和“项目11”的问题
如果有人知道并能帮助我,我将非常感激 我认为在这种情况下不能使用正则表达式,这正是使用属性的原因
<item num="00">
不过,我相信这就是你要找的
那些00 11东西真的应该是属性这里是全部工作代码
Here is the total working code
<?php
$xml = 'file.xml';
$content = '';
$f = fopen($xml, 'r');
while($data = fread($f, filesize($xml))) {
$content.= $data;
}
fclose($f);
$content = "<root>$conten</root>";
$doc = new SimpleXmlElement($content);
$query = $doc->xpath('//item/child::*');
foreach($query as $item) {
echo $item->name . '<br>';
}