开发者

How to extract XML response i php

开发者 https://www.devze.com 2023-01-23 17:16 出处:网络
I am using USPS(http://www.usps.com) for rate calculation between 2 zip codes. USPS accepts xml and returns response in xml.开发者_StackOverflow社区 I want extract response data of the xml by php. sho

I am using USPS(http://www.usps.com) for rate calculation between 2 zip codes. USPS accepts xml and returns response in xml.开发者_StackOverflow社区 I want extract response data of the xml by php. show me a easy way to extract xml in php. thanks in advance.


Use PHP's simplexml http://php.net/manual/en/book.simplexml.php

You can create an object from an xml string :

$xml_string = file_get_contents('path/to/xml/url'); 
// Convert API feed to xml object 
$xml = simplexml_load_string($xml_string);

You can access any value by traversing the object for example :

$name = $xml->person->name;

with the xml looking like this :

<?xml version="1.0" encoding="utf-8"?>
<some_root>
<person>
<name>My_name</name>
</person>
</some_root>

Its not called simple xml for any other reason. Have a good look at the documentation, theres really a lot of cool stuff relating to xml and the handling of that.


Use XMLReader class.

Documentation for it you can find at http://pl2.php.net/manual/en/book.xmlreader.php

0

精彩评论

暂无评论...
验证码 换一张
取 消