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
Related
Hi I want to parse in php
and read only one option of each line. How can I do this?
I tried:
<?php
$xml = file_get_contents("file.xml");
echo $xml;
?>
But this read the full xml. I need only one option of the xml
Try this
<? php $xml = simplexml_load_file("path/to/your/file/file.xml"); ?>
Convert your object into array like this
$array= (array) $xml;
after that you can do this
$option = $array['OptionName'];
Take a look at simplexml_load_file and turn your xml into a workable object where you can loop through and find whichever properties you need.
$xml = simplexml_load_file('file.xml');
foreach($xml->ParkingData as $k => $v)
echo $v->ParkingFacility->ParkingName ." - Vacant Spaces: ". $v->ParkingFacility->VacantSpaces."<br />";
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<stw:ThumbnailResponse xmlns:stw="http://www.shrinktheweb.com/doc/stwresponse.xsd">
<stw:Response>
<stw:ThumbnailResult>
<stw:Thumbnail Exists="true">http://imagelink.com</stw:Thumbnail>
<stw:Thumbnail Verified="false">delivered</stw:Thumbnail>
</stw:ThumbnailResult>
<stw:ResponseStatus>
<stw:StatusCode>refresh</stw:StatusCode>
</stw:ResponseStatus>
<stw:ResponseTimestamp>
<stw:StatusCode>1413812009</stw:StatusCode>
</stw:ResponseTimestamp>
<stw:ResponseCode>
<stw:StatusCode>HTTP:200</stw:StatusCode>
</stw:ResponseCode>
<stw:CategoryCode>
<stw:StatusCode></stw:StatusCode>
</stw:CategoryCode>
<stw:Quota_Remaining>
<stw:StatusCode>132</stw:StatusCode>
</stw:Quota_Remaining>
<stw:Bandwidth_Remaining>
<stw:StatusCode>999791</stw:StatusCode>
</stw:Bandwidth_Remaining>
</stw:Response>
</stw:ThumbnailResponse>';
$dom = new DOMDocument;
$dom->loadXML($xml);
$result = $dom->getElementsByTagName('stw:Thumbnail')->item(0)->nodeValue;
$status = $dom->getElementsByTagName('stw:Thumbnail')->item(0)->nodeValue;
echo $result;
Having the above code should output http://imagelink.com and $status should hold "delivered" - but none of these work instead I am left with the error notice that:
Trying to get property of non-object
I have tried different xml parsing alternatives like simplexml (but that did not work when the tag names have : in it ) and i tried looping through the each scope in the xml (ThumbNailresponse, response and then thumbnailresult) without luck.
How can i get the values inside stw:Thumbnail?
You need to specify a namespace and the method DOMDocument::getElementsByTagName can't handle it. In the manual:
The local name (without namespace) of the tag to match on.
You can use DOMDocument::getElementsByTagNameNS instead:
$dom = new DOMDocument;
$dom->loadXML($xml);
$namespaceURI = 'http://www.shrinktheweb.com/doc/stwresponse.xsd';
$result = $dom->getElementsByTagNameNS($namespaceURI, 'Thumbnail')->item(0)->nodeValue;
Using simple xml you could use ->children() method on this one:
$xml = simplexml_load_string($xml_string);
$stw = $xml->children('stw', 'http://www.shrinktheweb.com/doc/stwresponse.xsd');
echo '<pre>';
foreach($stw as $e) {
print_r($e);
// do what you have to do here
}
This code actually runs just fine for me ---
Typically, that sort of error means you may've made a typo on your $dom object - double check it and try again.
Also, it is notable that you'll want to change the item(0) to item(1) when you're setting your $status variable.
$result = $dom->getElementsByTagName('stw:Thumbnail')->item(0)->nodeValue;
$status = $dom->getElementsByTagName('stw:Thumbnail')->item(0)->nodeValue;
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 used PHP with simplexml to parse RSS using standard elements before like <title> <pubDate> etc. But how would I parse something custom to the feed like <xCal:location> or <xCal:dtstart> that uses an xCal data element?
Something like $item->xCal:dtstart will error out. How would I collect this data element?
A sample of a feed like this: http://www.trumba.com/calendars/vd.rss?mixin=236393%2c236288
Try like this:
$feedUrl = 'http://www.trumba.com/calendars/vd.rss?mixin=236393%2c236288';
$rawFeed = file_get_contents($feedUrl);
$xml = new SimpleXmlElement($rawFeed);
$ns = $xml->getNamespaces(true);
//print_r($ns);
$xCal = $xml->channel->children($ns['xCal']);
echo ($xCal->version)."<br />";
foreach($xml->channel->item as $item)
{
//print_r($item);
$itemxTrumba=$item->children($ns['x-trumba']);
echo $itemxTrumba->masterid."<br />";
}
//print_r($xCal);
The "something custom" is an XML namespace. Search for existing answers regarding SimpleXML and namespaces.
Basically, what you need is the ->children() method: $item->children('xCal', true)->dtStart
I really need help with using namespaces. How do I get the following code to work properly?
<?php
$mytv = simplexml_load_string(
'<?xml version="1.0" encoding="utf-8"?>
<mytv>
<mytv:channelone>
<mytv:description>comedy that makes you laugh</mytv:description>
</mytv:channelone>
</mytv>'
);
foreach ($mytv as $mytv1)
{
echo 'description: ', $mytv1->children('mytv', true)->channelone->description;
}
?>
All I'm trying to do is get the content inside the name element.
when ever yu are using the namespaces in xml yu should define the namespaces what ever you use..! in the code what i posted you can see how you can define the namespace you are using..
you need to display the description specific to the namespace isn't it..? correct me if I'm wrong., and please post yur purpose properly so that i can understand your problem..
Use this code and see if you can get some idea..
$xml ='<mytv>
<mytv:channelone xmlns:mytv="http://mycompany/namespaces/mytvs">
<mytv:description >comedy that makes you laugh</mytv:description>
</mytv:channelone>
</mytv>';
$xml = simplexml_load_string($xml);
$doc = new DOMDocument();
$str = $xml->asXML();
$doc->loadXML($str);
$bar_count = $doc->getElementsByTagName("description");
foreach ($bar_count as $node)
{
echo $node->nodeName." - ".$node->nodeValue."-".$node->prefix. "<br>";
}
here., the value, "$node->prefix" will be the namespace of the tag containing "description".
getElementsByTagName("description") is used to get all the elements in the xml containing description as tags...!! and then later using the "$node->prefix" you compare with the specific namespace as required for you and then print..