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
Related
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.
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 am trying to import a XML file via API into my php script which then will parse said XML file and extract a string. Ive searched across the webspace for an answer, and though I have found a ton of resources I still cannot get this script to work.
The XML file that I am loading will look something like this
<api version="2">
<currentTime>2012-07-28</currentTime>
<result>
<rowset name="accounts" key="accountID" columns="accountID,accountKey,balance">
<row accountID="555555555" accountKey="6666" balance="7777777777.23"/>
</rowset>
</result>
<cachedUntil>2012-07-28</cachedUntil>
</api>
I am trying to get my php script to fetch the value of the attribute balance.
This is the code that I have put together so far:
<?php
$apiurl = "api.some-arbitrary-api-site.com;
$xml = simplexml_load_file($apiurl);
print_r($xml);
$balance = $xml->balance;
print_r($balance);
?>
This returns:
SimpleXMLElement Object ( )
Also following some other web tutorials I have tried this change
$balance = $xml->row->attributes()->balance;
print_r($balance);
Which spits out
Warning: main() [function.main]: Node no longer exists in C:\xampp\htdocs\EVE\progress\import.php on line 22
Warning: main() [function.main]: Node no longer exists in C:\xampp\htdocs\EVE\progress\import.php on line 22
What am I doing wrong? The end result is for the page to load this API and fetch the balance once every two days and store the data to be used in a chart rendered using highchart.
Any help will be appreciated!
Thanks
Musa answered the question
I don't know simplexml but shouldn't it be
$xml->result->rowset->row->attributes()->balance – Musa
I was being an idiot and didnt check the xml over again when typing in the path.
Thanks
I want to perform an if check on an XML file I have loaded in using simplexml_load_file.
I have accessed the data when the output is as I want it to be, although as it's web services sometimes they may be a response which I need to cater for. The below is the XML I want to be able to check on:
<?xml version="1.0" encoding="utf-8"?>
<ProductSizes />
Can somebody help me out? I am using the below:
$xmlstock = simplexml_load_file($xmlfeed);
Assuming that the xml source looks something like the following when actually populated:
<?xml version="1.0" encoding="utf-8"?>
<ProductSizes>
<ProductSize />
<ProductSize />
<ProductSize />
</ProductSizes>
You can get a count using the count() api call:
$xmlstock = simplexml_load_file($xmlfeed);
$hasProductSizes = ($xmlstock->ProductSize->count() > 0) ? true : false;
More info on the count() API via PHP docs.
I have a PHP client that has to query datasets from a service that uses .NET. So, I was trying to make a query by passing a POST field called 'query'. Inside this variable, I have a string version of XML document. What's happening now is that whenever I passed this 'query' to the server, it will throw an exception at me 'Data at the root level is invalid.' and point to the last line of my XML document.
Any tips?
Here's the XML
<?xml version="1.0" encoding="utf-8"?>
<Predicate>
<ComparisonPredicate name="like" isEnabled="True" id="" DistinctIndividualQueries="False">
<FacetOperand reference="TriggeredAlerts.Alert.Name"/>
<Comparison operation="like"/>
<ValueOperand type="System.String" value="Sample"/>
</ComparisonPredicate>
</Predicate>
Here's the var_dump()
string(335) "<?xml version="1.0" encoding="utf-8"?>
<Predicate>
<ComparisonPredicate name="like" isEnabled="True" id="" DistinctIndividualQueries="False">
<FacetOperand reference="TriggeredAlerts.Alert.Name"/>
<Comparison operation="like"/>
<ValueOperand type="System.String" value="TestAlert"/>
</ComparisonPredicate>
</Predicate>
"
I just figured it out and the solution was to use fsocksopen instead of curl. I do not have an accurate answer for that but when I was using curl, it added some extra stuffs to my content; my content length was significantly longer than it is actually supposed to be.