XML Parser using PHP [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 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);

Related

How can I parse XML node with mixed value in PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
How can I parse this xml, and get url text and title node value seperated:
<root>
<test>
<gate>
<url>http://google.com<title>Google</title></url>
</gate>
</test>
</root>
Is this possible with DomDocument or SimpleXML ?
Try with this:
$data = simplexml_load_file("file.xml");
echo $data->root->test->gate->url

XML : PHP echo is empty [duplicate]

This question already has an answer here:
Get root node of XML doc using simplexml
(1 answer)
Closed 8 years ago.
Having an issue echo'ing xml tags.
PHP:
$xml = $insureFormResult->returned;
$xml1 = new SimpleXMLElement($xml);
$result = $xml1->xpath("response")[0];
echo $result;
If I echo $xml it gives me:
<?xml version="1.0" encoding="utf-8"?>
<response>
<errors>
<error code="7">Your details are already in our system and have been forwarded to our insurance partners who will contact you shortly</error>
</errors>
</response>
The xml will always have one response tag. I also want to know how to echo the tag with id 'code'. I tried the php above but there's no result echoed.
Any kind of help will be appreciated!
EDIT
It wasn't working because of the version of PHP on my server.
I'm not really familiar with PHP, but the xpath to access the error with attribute code with value 7 is:
/response/errors/error[#code='7']

PHP. XML parsing from variable [duplicate]

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');

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

xml parsing with simple xml [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Problem with simpleXML and entity not being defined
I have this tag with an entity in an xml file:
<comune>Forli'</comune>
Simple xml in php fail to parse the file:
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: parser error : Entity 'igrave' not defined
How can i do?
I tried this a small example and it worked for me
XML:
<?xml version="1.0" encoding="UTF-8"?>
<comune>
<comune>Forli'</comune>
</comune>
PHP:
$xml = simplexml_load_file('test.xml');
foreach($xml->children() as $child){
echo '<pre>';
print_r((string)$child);
echo '</pre>asd';
}
OUTPUT:
Forli'
Check this solution for help

Categories