帮助通过属性值解析 XML 到 PHP [英] Help parse XML to PHP by attribute value
本文介绍了帮助通过属性值解析 XML 到 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮助我解析这个.我有以下 XML.我需要获取与75"max-width
匹配的 photo-url
值.我如何在 PHP 中过滤它$xml->posts->post['photo-url']
..?
Could someone kindly help me get this parsed. I have the following XML. I need to get the value of photo-url
that matches "75" max-width
. How do I filter that in PHP
$xml->posts->post['photo-url']
....?
<photo-url max-width="100">
image1.jpg
</photo-url>
<photo-url max-width="75">
image2.jpg
</photo-url>
推荐答案
使用 SimpleXMLElement
和 xpath 查询.
Use SimpleXMLElement
and an xpath query.
$xml = new SimpleXMLElement($your_xml_string);
$result = $xml->xpath('//photo-url[@max-width="75"]');
// Loop over all the <photo-url> nodes and dump their contents
foreach ($result as $node ) {
print_r($node);
$image = strip_tags($node->asXML);
}
这篇关于帮助通过属性值解析 XML 到 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文