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 );
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
)
)
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.
Use iterate, and attributes
foreach ( $xml->xpath( "//ul[@id='wxoptions']/li[3]/a" ) as $node)
{
$href = $node->attributes("href");
}
Or directly called :
$href = $xml[0]->attributes("href");