PHP – XML: Retrieving Attribute Values

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

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");
05 foreach($xml->children() as $cars){
06     foreach($cars->children() as $car => $data){
07       echo $data->manufacture['country'];
08       echo "<br />";
09     }
10 }
12 ?>

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

Bookmark and Share Tag: xml, node, attribute Category: PHP BasicPost : March 23rd 2008 Read: 24,377

By Rz Rasel Posted in Php

5 comments on “PHP – XML: Retrieving Attribute Values

  1. Pingback: URL

  2. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there’s even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That’s right AUTOMATICALLY, just watch this 4minute video for more information at. WordPress Seo Plugin

  3. I’m starting a new online website directory and was wanting to know if I can submit your website? I’m trying to mature my directory little by little by hand so that it retains top quality. I will make sure and put your website in the best category and I’ll also use, “PHP – XML: Retrieving Attribute Values | arzrasel” as your anchor text. Make sure you let me know if this is acceptable with you by emailing me at: Many thanks

  4. The goal of the Office of Science Graduate Student Research
    (SCGSR) program is to prepare graduate students for science, technology, engineering, or
    mathematics (STEM) careers critically important to the DOE Office
    of Science mission, by providing graduate thesis research opportunities at DOE laboratories.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s