我有一个XML文档:
<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>';
}
}
?>
其中"???"是"item00"和"item11"的问题。
如果有人知道并可以帮助我,我将不胜感激!
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>';
}
我认为你不能在那种情况下使用正则表达式,这就是使用属性的原因
<item num="00">
但是检查一下,我相信这就是您正在寻找的
那 00 11 件事真的应该是属性