I have an XML source which appears to be using some d2LogicalModel markup, and I'm really struggling to figure out how to extract any data from it using PHP's SimpleXML.
I've included a cut-down version of the XML below: How do I extract the carParkIdentity?
How to I access a specific 's id?
Then I can figure out the rest of the data myself!
Thanks a bunch!
<d2lm:d2LogicalModel xmlns:d2lm="http://datex2.eu/schema/1_0/1_0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xalan="http://xml.apache.org/xslt" xmlns:java="http://xml.apache.org/xalan/java" modelBaseVersion="1.0" xsi:schemaLocation="http://datex2.eu/schema/1_0/1_0 http://datex2.eu/schema/1_0/1_0/DATEXIISchema_1_0_1_0.xsd">
<d2lm:exchange>...</d2lm:exchange>
<d2lm:payloadPublication lang="en" xsi:type="d2lm:SituationPublication">
<d2lm:situation id="CPN0017">
<d2lm:situationRecord id="CPN0017_1" xsi:type="d2lm:CarParks">
<d2lm:situationRecordCreationTime>2017-01-03T10:47:41</d2lm:situationRecordCreationTime>
<d2lm:situationRecordVersion>1</d2lm:situationRecordVersion>
<d2lm:carParkIdentity>Chapelfield, Chapelfield Road, N:CPN0017</d2lm:carParkIdentity>
<d2lm:carParkOccupancy>77.0</d2lm:carParkOccupancy>
<d2lm:carParkStatus>enoughSpacesAvailable</d2lm:carParkStatus>
</d2lm:situationRecord>
</d2lm:situation>
</d2lm:payloadPublication>
</d2lm:d2LogicalModel>
As simple as this piece of code:
$xml = simplexml_load_file('/PATH/TO/YOUR/FILE.XML');
foreach ($xml->xpath('//d2lm:carParkIdentity') as $child) {
echo $child;
}
Also, you are more than welcome to take a look on XPath syntax here.
Related
I have been searching for how to read one node of XML in PHP. The PHP documentation wasn't helpful because I don't understand how to use PHP. All of the tutorials I found weren't useful beacause I only need PHP to read XML(I use CSHTML for Databases and other server-side things). I have working code that can read XML as a tree if it is in a RSS format. I am trying to get the google map geocode api information, from "http://maps.googleapis.com/maps/api/geocode/xml?latlng=38.7876639,-90.8455276&sensor=false." I only want the very first "Formatted_address" node. My current code is;
<?php
$xml=("http://maps.googleapis.com/maps/api/geocode/xml?latlng=38.7876639,-90.8455276&sensor=false");
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
//get and output "<result>" elements
$x=$xmlDoc->getElementsByTagName('result');
for ($i=0; $i<=2; $i++)
{
$item=$x->result($i)->getElementsByTagName('formatted_address')
->result(0)->childNodes->result(0)->nodeValue;
echo ( $item);
}
?>
However this always returns a 500 error and I don't understand what i am doing wrong. Thank you all in advance.
Change result to item
$item = $x->item($i)->getElementsByTagName('formatted_address')
->item(0)->childNodes->result(0)->nodeValue;
As you can see DOMNodeList only has one method called item(int $index)
i am having an issue with parsing an XML file using SimpleXML and PHP.
The XML file in question is provided by a third party and includes a number of child elements (going down multiple levels) within it. I know which elements i require and can see them within the XML file, but i just can't seem to get them to print using PHP.
Example XML feed for test.xml:
<?xml version="1.0" encoding="utf-8"?>
<Element1 xmlns="" release="8.1" environment="Production" lang="en-US">
<Element2>
<Element3>
<Element4>
<Element5>it worked</Element5>
</Element4>
</Element3>
</Element2>
</Element1>
The file only includes one of each attribute so i can be very particular with the request, the code i have so far is below:
$lib=simplexml_load_file("test.xml");
$make=$lib->Element1->Element2->Element3->Element4->Element5;
print $make;
I have tried to look this up before asking, but the only solutions i can see are when the child attributes are unknown or there are multiple results for each request, which is not the case in this instance.
Any help or guidance would be greatly received.
Thanks
In your code above, $lib is Element1. So you just need to drop one of your references. This:
$make=$lib->Element1->Element2->Element3->Element4->Element5;
Should become this:
$make=$lib->Element2->Element3->Element4->Element5;
Also, SimpleXML is an awful awful awful awful interface (considering that "Simple" is in the name and there is mass confusion about how to use it). I would always recommend DOMDocument instead.
I'd strongly recommend using xpath as it will give you more flexibility e.g. Allow you to restrict results based on xml node attributes.
$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?>
<Element1 xmlns="" release="8.1" environment="Production" lang="en-US">
<Element2>
<Element3>
<Element4>
<Element5>it worked</Element5>
</Element4>
</Element3>
</Element2>
</Element1>');
$data=$xml->xpath('/Element1/Element2/Element3/Element4/Element5');
echo (string)$data[0]; //outputs 'it worked'
//this also works
$data=$xml->xpath('//Element5');
echo (string)$data[0]; //outputs 'it worked'
I need to post XML data from a textarea input to PHP so I can parse it and output a table.
I've tried a few methods and none seem to be working.
Currently I have this:
jQuery('#btnRegistrarXML').live('click', function(){
var xml;
if (jQuery('#txtRegXML').val() == ""){
AddMsg('You must paste XML from the excel export into the textarea.', 'error');
} else {
jQuery.post('registrar-xml-to-table.php', {xml:escape(jQuery('#txtRegXML').val())}, function(data){
jQuery('#regXMLasTable').empty();
jQuery('#regXMLasTable').append(data);
});
}
displayMsgs();
});
The PHP is:
$xmlraw = urldecode($_POST['xml']);
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlraw);
$dom->formatOutput = TRUE;
$xmlstr = $dom->saveXml();
$xml = simplexml_load_string($xmlstr);
echo "<p>xmlraw:</p>";
echo $xmlraw;
echo "<p>xml:</p>";
echo $xml;
foreach ($xml->document as $doc) {
echo '<p class="alert alert-error">'.$doc->title.'</p>';
}
The first echo $xmlraw is working - it outputs the XML string all on one line - the post is sending the data through properly.
The second echo $xml doesn't output anything and the foreach doesn't output anything either - something is not working in the PHP
I've also tried loading $xmlraw directly into simplexml_load_string($xmlraw) but it doesn't work - I'm assuming because it's not well formed?
The XML I'm using to test is:
<?xml version="1.0"?>
<document>
<title>
Foobar
</title>
</document>
You can paste the XML into the textarea on this ( http://tsdexter.com/webservice-testing/programs-list.php ) page and then if you inspect element underneath xmlraw: you can see that the raw xml string was echoed - however, underneath xml: there is nothing and also the foreach doesn't output anything either.
Here's a jsFiddle so you can see the HTML/JS - it doesn't actually do anything though because it can't ajax to the PHP page - so I included the PHP version above to test. http://jsfiddle.net/tsdexter/EDqQB/
Any ideas?
A few questions, which may or may not constitute an answer, but needed more space than a comment.
Why are you running urldecode on the $_POST variable? PHP should be doing that for you, unless you are double-escaping it somewhere.
Why are you loading into DOM and then SimpleXML? I know you said you tried without, but they use the same XML parser underneath, so there is no way this will help you rather than introducing more confusion.
What does var_dump(simplexml_load_string($rawxml)) give you? My guess would be FALSE, meaning an error. In which case, check you have warnings turned on (error_reporting(E_ALL); ini_set('display_errors', '1');) and detailed XML error handling turned off (libxml_use_internal_errors(false);).
In case it does give you a SimpleXMLElement object, don't spend too long looking at the var_dump output. Try one of these debugging functions instead.
I have a functionality like below and getting an error String could not be parsed as XML
$category_feed_url = "http://www.news4u.com/blogs/category/articles/feed/";
$file = file_get_contents($category_feed_url);
$xml = new SimpleXMLElement($file);
foreach($xml->channel->item as $feed)
{
echo $feed->link;
echo $feed->title;
...
why this error has occurred.
The URL points to an HTML document.
It is possible for a document to be both HTML and XML, but this one isn't.
It fails because you are trying to parse not-XML as if it was XML.
See How to parse and process HTML with PHP? for guidance in parsing HTML using PHP.
You seem to be expecting an RSS feed though, and that document doesn't resemble one or reference one. The site looks rather spammy, possibly that URI used to point to an RSS feed but the domain has now fallen to a link farm spammer. If so, you should find an alternative source for the information you were collecting.
"String could not be parsed as XML", your link is an html page.
I need to parse all data from below XML file. Please guide me how can I do it? How can I put data into a separate variables or array. Like, the value of <p:productid> tag into $pid and value of <p1:devices> into $pdevice?
Here is a sample file:
<?xml version="1.0" encoding="UTF-8"?>
<p:application>
<p1:devices>
<p1:device>First device</p1:device>
<p1:device>Second Device</p1:device>
</p1:devices>
</p:versions>
</p:application>
You can use the SimpleXML for that.
Here is a good tutorial to get you started:
Introduction To SimpleXML With PHP