phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP - XML: Retrieving Attribute Values

Tag: xml, node, attribute   Category: PHP Basic
post: 23 Mar 2008 read: 585


PHP XML Tips - Part 4: Below, sample how to retrieve attribute value.

First, create xml document like this:


<?xml version="1.0"?>
<datas>
  <cars> 
    <car>
      <manufacture country="japan">Honda</manufacture>
	  <type>Honda Jazz</type>
    </car>
    <car>
      <manufacture country="korea">Nissan</manufacture>
	  <type>Nissan Livina</type>
    </car>	
  </cars>
</datas>

Save as "cars.xml" within www/test/xml. Then create reader like this:


<?php
$xml = simplexml_load_file("cars.xml") 
       or die("Error: Cannot create object");
	   
foreach($xml->children() as $cars){
	foreach($cars->children() as $car => $data){
	  echo $data->manufacture['country'];
	  echo "<br />";
	}
}

?>

As you see, to read attribute value, we can use such as $data->manufacture['country'].



Series this article:
PHP - XML: Read from String
PHP - XML: Read from a File
PHP - XML: Retrieving Node Values
PHP - XML: Retrieving Attribute Values
PHP - XML: Processing XML
PHP - XML: Creating XML Document
PHP - XML: Adding XML Nodes
PHP - XML: Removing Node
PHP - XML: Replacing Node
PHP - XML: Filtering XML Nodes with XPath
PHP - XML: Filtering XML Nodes by Namespace

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting