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
Related
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');
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?
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);
This question already has answers here:
Getting actual value from PHP SimpleXML node [duplicate]
(4 answers)
Closed 8 years ago.
I am using simplexml_load_string for XML packets. In my scenario, the XML string I want to convert is known as k.
My problem, however, is that when I use k, tags still remain that weren't parsed (<k>, <\k>).
For example, I use
$x->k, and I get back <k>DATA I WANT HERE<\EK>.
How do I get rid of these?
What the code does: It connects to a game and logs in.
Use InnerNode to get the value without the tags:
$x->k->InnerNode
You can also do a typecast:
(string)$x->k
I tried this and seem to be getting the string.
<?php
$str = "<msg t='sys'><body action='rndK' r='-1'><k>qH~e9Gmt</k></body></msg>";
$xml = simplexml_load_string( $str );
echo $xml->body->k; // gives 'qH~e9Gmt'
?>
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I want to find country, city, latitude and longitude from IP address using php. I am using this url and it is returning data in xml format.
http://www.ipgp.net/api/xml/122.163.6.58
The data is coming like this:
<IpLookup>
<Ip>122.163.6.58</Ip>
<Code>IN</Code>
<Country>India</Country>
<Flag>http://www.ipgp.net/flags/in.png</Flag>
<City>Calcutta</City>
<Region>West Bengal</Region>
<Isp></Isp>
<Lat>22.5697</Lat>
<Lng>88.3697</Lng>
</IpLookup>
Can anybody suggest how to parse and get the result
Use the XML parser included in PHP?
Use simplexml_load_string().
I've been using this personally:
http://ipinfodb.com/
The examples are very clear and concise and the API is very fast.
Good luck.
The use of API in PHP has already described in their website. Why you use but don't read?
http://www.ipgp.net/developer-tools/
I'll suggest you to use xpath this is more easier schema for accessing the attributes.
for example for your current data i have the following:
$file = 'file.xml';
$xml = new SimpleXMLElement($file, NULL, TRUE);
$ipz = $xml->xpath("/IpLookup/Ip");
$country = $xml->xpath("/IpLookup/Country/");
foreach($ipz as $ip)
{
foreach($country as $country)
{
echo $ip.'<br/>';
echo $country.'<br/>';
}
}
this code will return you the ip and the country for your current xml. you can edit it in your own way.