Wednesday, December 3, 2014
Saturday, November 29, 2014
PHP - XML: Retrieving Attribute Values
Example of XML: Retrieving Attribute Values by PHP
First, create xml document like this:
01 | <?xml version= "1.0" ?> |
02 | <datas> |
03 | <cars> |
04 | <car> |
05 | <manufacture country= "japan" >Honda</manufacture> |
06 | <type>Honda Jazz</type> |
07 | </car> |
08 | <car> |
09 | <manufacture country= "korea" >Nissan</manufacture> |
10 | <type>Nissan Livina</type> |
11 | </car> |
12 | </cars> |
13 | </datas> |
Save as "cars.xml" within www/test/xml. Then create reader like this:
01 | <?php |
02 | $xml = simplexml_load_file( "cars.xml" ) |
03 | or die ( "Error: Cannot create object" ); |
04 | |
05 | foreach ( $xml ->children() as $cars ){ |
06 | foreach ( $cars ->children() as $car => $data ){ |
07 | echo $data ->manufacture[ 'country' ]; |
08 | echo "<br />" ; |
09 | } |
10 | } |
11 |
12 | ?> |
Subscribe to:
Posts (Atom)