LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2012, 10:09 PM   #1
boloo
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Rep: Reputation: Disabled
Reading XML files with php


Hello All,

I am trying to parse xml file with php. I was able to do with firstchild element but not third level. Here what it looks like
Code:
<root>
<cat>
     <item1>item1</item1>
     <item2>item2</item2>
     <item3>
         <description>
            <name>somename</name>
            <value>some value value </value>
         </description>
         <description>
              <name>some another name</name>
              <value>some another value</value>
         </description>
     </item3>    
</cat>
<cat>
     <item1>item1</item1>
     <item2>item2</item2>
     <item3>
         <description>
            <name>somename</name>
            <value>some value value </value>
         </description>
         <description>
              <name>some another name</name>
              <value>some another value</value>
         </description>
     </item3>    
</cat>
</root>
So I have following code
Code:
      $doc = new DomDocument();
      $doc -> load("xmlfile.xml");
      $cats = $doc->getElementsByTagName('cat');
      foreach($cats as $cat){
              $item1=$cat->getElementsByTagName('item1')->item(0)->nodeValue;
              $item2=$cat->getElementsByTagName('item2')->item(0)->nodeValue;
      }
Then when it comes to item3, I don't know what to do. I tried with
Code:
      $cat->getElementsByTagName('description')
, then it returned both 'name' and 'value' inside the description tag. Say if I wanted to get only 'value' or 'name' inside the description tag, how can I do?

Thanks very much in advance.

Last edited by colucix; 02-11-2012 at 01:43 AM. Reason: Moved to the more appropriate Programming forum.
 
Old 02-11-2012, 04:31 AM   #2
boloo
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
got it working ,
$item3=$cat->getElementsByTagName('item1')->item(0);
$desc=item3->getElementsByTagName('description')->item(0);
$name=$desc->getElementsByTagName('name')->item(0)->nodeValue;
 
Old 02-11-2012, 09:11 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
I prefer to use SimpleXML to parse XML in PHP:
PHP Code:
#!/usr/bin/php
<?PHP

function subnodes($xml$name$list = array())
{
    foreach (
$xml->children() as $element => $xmlnode)
        if (
$element == $name)
            
$list[] = $xmlnode;
        else
            
$list nodes($xmlnodes$name$list);

    return 
$list;
}

for (
$arg 1$arg $argc$arg++) {

    
$xml = @simplexml_load_file($argv[$arg]);
    if (
$xml === FALSE) {
        
fprintf(STDERR"%s: Cannot load XML file.\n"$argv[$arg]);
        exit(
1);
    }

    for (
$cat 0$xml->cat[$cat] !== NULL$cat++) {
        echo 
"cat "$cat":\n";

        foreach (
$xml->cat[$cat]->item1 as $item1)
            echo 
"\titem1: '"$item1"'\n";

        foreach (
$xml->cat[$cat]->item2 as $item2)
            echo 
"\titem2: '"$item2"'\n";

        foreach (
$xml->cat[$cat]->item3 as $item3) {
            echo 
"\titem3:\n";
            foreach (
$item3->description as $desc) {
                echo 
"\t\tdescription:\n";
                foreach (
$desc->name as $name)
                    echo 
"\t\t\tname: '"$name"'\n";
                foreach (
$desc->value as $value)
                    echo 
"\t\t\tvalue: '"$value"'\n";
            }
        }
    }
}

?>
Above, $xml=simplexml_load_file(filename); loads the XML file or URL, yielding the root node in $xml.

It is a DOM model, where $xml->name is an array of name nodes. When converted to or interpreted as a string, it yields the text that XML node (but not child nodes) contains.

In particular, the first name node in the first description node in the first item3 node of the first cat node is output by e.g.
PHP Code:
echo $xml->cat[0]->item3[0]->description[0]->name[0]; 
(which, as I wrote above, evaluates to the contents as a string, but is really an SimpleXMLElement object). Or, more verbosely,
PHP Code:
$cat $xml->cat[0];
$item3 $cat->item3[0];
$desc $item3->description[0];
echo 
$desc->name[0]; 
You can use foreach() to loop over the arrays, just as I did in my PHP example code above.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I work with large XML files in PHP? RavenLX Programming 16 02-19-2009 01:33 PM
LXer: The smart way to Process XML files with PHP LXer Syndicated Linux News 0 11-10-2007 12:30 AM
Pb Reading php files tbesrour SUSE / openSUSE 0 12-22-2006 08:49 AM
PHP directory security - reading other people's files krasl Linux - Security 2 03-16-2006 07:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration