有人可以帮我解析这个吗?我有以下 XML。我需要获取与“75”max-width
匹配的 photo-url
值。我如何在 PHP 中过滤它
$xml->posts->post['photo-url']
....?
<photo-url max-width="100">
image1.jpg
</photo-url>
<photo-url max-width="75">
image2.jpg
</photo-url>
最佳答案
使用 PHP DOM
$dom = new DomDocument;
$dom->loadXml('
<root>
<photo-url max-width="100">image1.jpg</photo-url>
<photo-url max-width="75">image2.jpg</photo-url>
</root>
');
$xpath = new DomXpath($dom);
foreach ($xpath->query('//photo-url[@max-width="75"]') as $photoUrlNode) {
echo $photoUrlNode->nodeValue; // will be image2.jpg
}
关于php - 帮助按属性值将 XML 解析为 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6763760/