使用cURL和simpleXMLElement提取数据。如何获取XPATH后的XML元素的值? [英] Using cURL and simpleXMLElement to extract data. How do I get the value of the XML element after XPATH?
本文介绍了使用cURL和simpleXMLElement提取数据。如何获取XPATH后的XML元素的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些问题从SimpleXMLElement对象提取我想要的数据。这里是我使用的代码的基础:
I am having some problems extracting the data I want from a SimpleXMLElement object. Here is the basics of the code I am using:
curl_setopt( $ch, CURLOPT_URL, $URL );
$html = curl_exec( $ch );
$html = $tidy->parseString( $html, $tc, 'utf8' );
$tidy->cleanRepair();
$html = $tidy->body()->value;
$xml = new SimpleXMLElement( $html );
$xml = $xml->xpath( "//ul[@id='wxoptions']/li[3]/a" ); //Your XPATH
print_r( $xml );
这将导航到所需的正确HTML元素,但会打印:
This navigates to the correct HTML element I want, but prints:
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[href] => http://www.mylink.com
[title] => mylink
)
[0] => mylink
)
)
值,我需要的是数组中的[href], http://www.mylink.com 。如何从我包括的输出中提取?
The value I need is the [href], "http://www.mylink.com" in that array. How do I extract that from the output I included? I'm stumped and very new to SimpleXMLElement and Xpath.
推荐答案
使用iterate和属性
Use iterate, and attributes
foreach ( $xml->xpath( "//ul[@id='wxoptions']/li[3]/a" ) as $node)
{
$href = $node->attributes("href");
}
或直接致电:
$href = $xml[0]->attributes("href");
这篇关于使用cURL和simpleXMLElement提取数据。如何获取XPATH后的XML元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文