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

帮助按属性值将XML解析为PHP,php,xml,xpath,Php,Xml,Xpath,有人能帮我分析一下吗。我有以下XML。我需要得到照片url的值匹配75最大宽度。如何在PHP中过滤 $xml->posts->post['photo-url'] 使用SimpleXMLElement和xpath查询 $xml = new SimpleXMLElement($your_xml_string); $result = $xml->xpath('//photo-url[@max-width="75"]'); // Loop over all the <photo-url&g

有人能帮我分析一下吗。我有以下XML。我需要得到照片url的值匹配75最大宽度。如何在PHP中过滤 $xml->posts->post['photo-url']

使用SimpleXMLElement和xpath查询

$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);
}
使用SimpleXMLElement和xpath查询

$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);
}
使用PHPDOM

$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
}
使用PHPDOM

$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
}

您可以使用XPath://photo-url[@max-width='75']。它将选择满足此条件的所有照片url。要仅选择第一个照片url,请使用此http://photo-url[@max-width='75'][1]

您可以使用XPath://photo-url[@max-width='75']。它将选择满足此条件的所有照片url。要仅选择第一个照片url,请使用以下链接://照片url[@max width='75'][1]

Woo,谢谢!因此,我的最终用法如下:$img=$xml->xpath'//photo-url[@max-width=75];回响哇,谢谢!因此,我的最终用法如下:$img=$xml->xpath'//photo-url[@max-width=75];回响