I need to get the value of url2 from the following xml:
<videoplayer>
<embed_code>aaa</embed_code>
<volume>bbb</volume>
<stats_pixel>
<secret>ccc</secret>
<url>ddd</url>
<url2>HOW TO GET THIS???</url2>
<video_plays>
<site_url>eee</site_url>
</video_plays>
</stats_pixel>
</videoplayer>
This didn't work:
$xml = simplexml_load_file($url);
$xml->videoplayer[0]->stats_pixel->url2;
videoplayer is root, so you shouldn't specify it, this should work:
echo $xml->stats_pixel->url2;;
You might need to encode your URL:
$xml = simplexml_load_file(rawurlencode($url));
var_dump($xml); //make sure you get a SimpleXMLElement here before using it...
Related
I have I'm trying to get the "extract" node value from an xml file/url. Below is my code, but I'm not getting any output.
<?php
$url = "https://en.wikipedia.org/w/api.php?format=xml&action=query&prop=extracts&exintro=&explaintext=&titles=unix";
$xml = simplexml_load_file($url);
echo $xml->extract ;
?>
Any help would be greatly appreciated.
$xml->extract would work if the node was a direct child of the xml file.
After looking at the api response, I am able to get and display the extract node using the full path:
$url = "https://en.wikipedia.org/w/api.php?format=xml&action=query&prop=extracts&exintro=&explaintext=&titles=unix";
$xml = simplexml_load_file($url);
echo $xml->query->pages->page->extract ;
I'm parsing XML with PHP using simplexml_load_file, then I json_encode and json_decode in order to get all the info as arrays:
$xml = simplexml_load_file('/var/www/darkglass/wp-content/themes/dark2/assets/xml/artists.xml');
$musicos = json_encode($xml);
$musicos = json_decode($musicos, true);
I'm having this problem where I want to add a HTML code inside the tag, but it only works if I add a character before the <![CDATA like the example below:
This doesn't work:
<band><![CDATA[<a class="abandlink" href="#">Cannibal Corpse</a>]]></band>
This works:
<band>.<![CDATA[<a class="abandlink" href="#">Cannibal Corpse</a>]]></band>
Any idea why is this happening?
You should use LIBXML_NOCDATA option:
$xml = simplexml_load_file('artists.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
Well, I found a workaround to this problem,
I just added substr, $band = substr($band, 1); to the variable, so it removes the first character of the variable, and it works.
I have found very little documentation on this API so I have came here with the hopes that someone knows how to use this thing. When I try this nothing shows up. For now I am just trying to display the title of the first listing. Here is my code:
<?php
$url = "http://api.oodle.com/api/v2/listings?key=MYKEY®ion=sf&category=sale/electronics&q=ipod";
$response = file_get_contents($url);
echo $response->element[0]->title;
?>
And here is a link to the XML: http://api.oodle.com/api/v2/listings?key=TEST®ion=chicago&category=vehicle/car
Thanks!
You have to parse the XML before you can access it like that.
Something like the following (untested!)
<?php
$url = "http://api.oodle.com/api/v2/listings?key=MYKEY®ion=sf&category=sale/electronics&q=ipod";
$response = file_get_contents($url);
$xmlDoc = new SimpleXMLElement($response);
echo $xmlDoc->element[0]->title;
?>
In this example I'm using SimpleXML.
In simpler terms, by doing $xmlDoc = new SimpleXMLElement($response) we're telling PHP that $response contains XML that should be parsed into structures that can be programmatically accessed.
In this case $xmlDoc becomes a SimpleXMLElement object, that you can use as per documentation: http://php.net/manual/en/class.simplexmlelement.php
I have an XML file that looks like the example on this site: http://msdn.microsoft.com/en-us/library/ee223815(v=sql.105).aspx
I am trying to parse the XML file using something like this:
$data = file_get_contents('http://mywebsite here');
$xml = new SimpleXMLElement($data);
$str = $xml->Author;
echo $str;
Unfortunately, this is not working, and I suspect it is due to the namespaces. I can dump the $xml using asXML() and it correctly shows the XML data.
I understand I need to insert namespaces somehow, but I'm not sure how. How do I parse this type of XML file?
All you need is to register the namespace
$sxe = new SimpleXMLElement($data);
$sxe->registerXPathNamespace("diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1");
$data = $sxe->xpath("//diffgr:diffgram") ;
$data = $data[0];
echo "<pre>";
foreach($data->Results->RelevantResults as $result)
{
echo $result->Author , PHP_EOL ;
}
Output
Ms.Kim Abercrombie
Mr.GustavoAchong
Mr. Samuel N. Agcaoili
See Full code In Action
I'm aware of how to drill down into the nodes of an xml document as described here:
http://www.php.net/manual/en/simplexml.examples-basic.php
but am at a loss on how to extract the value in the following example
$xmlStr = '<Error>Hello world. There is an Error</Error>';
$xml = simplexml_load_string($xmlStr);
simplexml_load_string returns an object of type SimpleXMLElement whose properties will have the data of the XML string.
In your case there is no opening <xml> and closing </xml> tags, which every valid XML should have.
If these were present then to get the data between <Error> tags you can do:
$xmlStr = '<xml><Error>Hello world. There is an Error</Error></xml>';
$xml = simplexml_load_string($xmlStr);
echo $xml->Error; // prints "Hello world. There is an Error"
What do you know. The value of the tag is just:
$error = $xml;
Thanks for looking :)