gpt4 book ai didi

php - 帮助按属性值将 XML 解析为 PHP

转载 作者:行者123 更新时间:2023-12-03 01:04:51 25 4
gpt4 key购买 nike

有人可以帮我解析这个吗?我有以下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com