PHP. XML parsing from variable [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I've such piece of code:
$xml=”<response>
<version>1.2</version>
<merchant_id></merchant_id>
<order_id> ORDER_123456</order_id>
<amount>1.01</amount>
<currency>UAH</currency>
<description>Comment</description>
<status>success</status>
<code></code>
<transaction_id>31</transaction_id>
<pay_way>card</pay_way>
<sender_phone>+3801234567890</sender_phone>
<goods_id>1234</goods_id>
<pays_count>5</pays_count>
</response>";
Could I parse it? How to do it? I had never work with XML.
E.g how to take ?

You should use simplexml_load_string function.
Parse as follow:
echo simplexml_load_string($xml)->version;
There is plenty functions you could use have a look at the link above.

Try saving your XML to a file and use simplexml to parse it in php.
See tutorial here: http://blog.teamtreehouse.com/how-to-parse-xml-with-php5

You should use DomDocument. It is much more flexible and useful than SimpleXML.
Parse as follows:
$doc = new DOMDocument();
$doc->loadXML($xml);
$price = $doc->getElementsByTagName('amount');
$currency = $doc->getElementsByTagName('currency');

Related

How to read in XML strings as variables? [duplicate]

This question already has answers here:
A simple program to CRUD node and node values of xml file [closed]
(2 answers)
Closed 8 years ago.
Try this query:
http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da
How do I simply read in the suggestions as variables? I can't seem to find anything that explains this.
You can use SimpleXmlIterator. That's really easy to use and you will be able to perform a foreach on the object you will get.
Library source
For example with file_get_contents or replace with curl if you prefer:
$feed = new SimpleXmlIterator(file_get_contents('http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da'));
foreach ($feed->suggestion as $suggestion) {
echo $value;
}

Extracting XML data to php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I'm trying to extract data from XML file (http://freegeoip.net/xml/google.com). You can see the content of the file looks something like:
<Response>
<Ip>74.125.235.3</Ip>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>CA</RegionCode>
<RegionName>California</RegionName>
<City>Mountain View</City>
<ZipCode>94043</ZipCode>
<Latitude>37.4192</Latitude>
<Longitude>-122.0574</Longitude>
<MetroCode>807</MetroCode>
<AreaCode>650</AreaCode>
</Response>
I want to take the information stored in the <latitude> and <longitude> tags, and store them in separate variables. The problem is, I've little idea how to do this, and was wondering if anyone could show me how to parse XML files with php?
$string_data = "<your xml response>";
$xml = simplexml_load_string($string_data);
$latitude = (string) $xml->Latitude;
$longitude = (string) $xml->Longitude;
echo $latitude.' '.$longitude;
It's easy, use PHP's SimpleXML Library:
$xml = simplexml_load_file("http://freegeoip.net/xml/google.com");
echo $xml->Ip; // 173.194.38.174
echo $xml->CountryCode; // US
echo $xml->ZipCode; // 94043
// etc...
The PHP manual has a whole section on PHP parsing:
http://php.net/manual/en/book.simplexml.php
http://php.net/manual/en/book.xml.php
For simplicity, you could also use xml_parse_into_struct()
Here's a pretty good example, using SimpleXML:
http://blog.teamtreehouse.com/how-to-parse-xml-with-php5

Load and parse XML file with PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have to create a webservice which goes to a specific URL that returns a XML-file as response and interprets/parses this file in order to save its contents to a MySQL database.
I've heard about the SimpleXML but I'm not sure how to get the websites response into a file whose path is needed in order to parse the document.
Can somebody at least explain me how to reach the goal of downloading the XML and saving it to a file? (best with some PHP code)
I will then (hopefully) find out by myself how to parse it and store its contents.
Here's an example of what my XML will look like (for privacy reasons I can't publish the real URL I'm using...)
Here's a couple of pointers..
To download a file and save it, the easiest way I have found is this:
<?php
file_put_contents('saved.xml', file_get_contents('http://www.xmlfiles.com/examples/simple.xml'));
You can then open the file with the simpleXML library like so:
$xml = simplexml_load_file('saved.xml');
var_dump($xml);
Hope that gives you enough info to get started.
See simpleXML for info on the simpleXML library.
You can download and save the xml to a local file by doing this:
$xmlstring = file_get_contents("http://domain.com/webservice/xmlfile.xml");
file_put_contents("path/localxmlfile.xml", $xmlstring);
To parse the xml file I suggest you to use DOMDocument class in combination with the DOMXPath class to query/search for specific elements.
DOMDocument: http://php.net/manual/de/class.domdocument.php
DOMXPath: http://php.net/manual/de/class.domxpath.php
Hopefully you can find your answer on below link. Seems related topic.
How do you parse and process HTML/XML in PHP?

XML Parser using PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have this xml content. I need to get the value from the fullName attribute. I have tried it using lib.xml parser. But it is not working. Could you please help me to achieve this? Thanks in advance.
<?xml version="1.0" encoding="UTF-8"?><currentUserDetails firstName="3rdParty" fullName="3rdParty Access" lastName="Access" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="" xmlns=""/>
Use simplexml:
$xml = simplexml_load_string($x); // assuming XML in $x
echo $xml['fullName'];
See it working: http://codepad.viper-7.com/7KVHcU
$xmlFile = "http://www.domaine.com/city.xml";
$data = simplexml_load_file($xmlFile);
print_r($data);

How to detect new line from the parsed XML with simpleload_xml_string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP SimpleXML doesn't preserve line breaks in XML attributes
I have following XML
$xmldatas = '<layer text="name
id"></layer>';
I have parse this XML with
$xml = simplexml_load_string($xmldatas);
But when I checked the $xml, the \n is been replaced with space. I want the new line remains as it is after the xml parsing.
But how can I do that ?
Thanks
I don't think that xml will accept a new line character in the tag option text.
If you are generating the xml maybe you want to do something like this?
$xmldatas = '<layer><text>name
id</text></layer>';

Categories