SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    india
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question reading tagname and text in xml using dom model and php

    hi
    i have been trying to read the tag names and textvalue of the subchild of an xml tag.
    the xml tag is the child of the root.
    the subchilds textvalue is displayed but tagnames are not coming.
    if any knows how to read the textvalue and tag names ofa xml document using dom help out

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, what version of PHP are we talking about? Secondly, can you post the XML you have along with whatever script you've arrived at just now?

    I'll have a look for you, though I no longer able to test the script as I don't have PHP

  3. #3
    SitePoint Member
    Join Date
    Jan 2005
    Location
    india
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    i am using php5 and the script is as follows
    Code:
      $i=0;
      $j=0;
     function walkTree($node)
     
     {   
       if($node->nodeType == XML_ELEMENT_NODE)
         { 
          $arr[$i]=$node->nodeName;
    	  $i++;
         }
    	if($node-> nodeType== XML_TEXT_NODE)
    	{
    	    
    	   $arr[$i][$j]=$node->nodeValue;
    	   $j++;
    	}
    	if($node->hasChildNodes()){
    	       walkTree($node->childNodes);
    	} 
    	
    	return $arr;
     }
    the xml data is as follows
    Code:
    <?xml version="1.0" ?> 
    <Calculator>
    	<Operation>sum</Operation> 
    	<Data>
    		<FirstNum>11</FirstNum> 
    		<SecondNum>21</SecondNum> 
    	</Data>
    </Calculator>
    i am new at coding php scripting so please make as descriptive as possible

    thanks

  4. #4
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Global varables

    Hi, should more or less look like:
    PHP Code:
    $i0;
    $j0;
    $arr = array();
    $txt = array();
    function 
    walkTree($node)
     {   
       global 
    $i$j$arr$txt
       if(
    $node->nodeType == XML_ELEMENT_NODE)
         { 
          
    $i++;
          
    $arr[$i] =$node->nodeName;
          
    $txt[$i] = array();
           
    $j 0;
         }
       if(
    $node-> nodeType== XML_TEXT_NODE)
       {
           
    $txt[$i][$j]=$node->nodeValue;
           
    $j++;
       }
       if(
    $node->hasChildNodes()){
               
    walkTree($node->childNodes);
        } 
     }
    print_r ($arr);
    print_r ($txt); 
    If you want to share variable between main() and your function then need to be global.
    You cannot put the same time text and subarray in the same element oof an array. It seems you need to array for nodes names and the other for texts content.
    Bye
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •