XML:

<Category name="Books">
        <Item fieldname="Author" datatype="string" value="AAA" />
        <Item fieldname="ISBN" datatype="string" value="BBB" />
        <Item fieldname="Publisher" datatype="string" value="CCC" />
      </Category>

how can i use child node to extract the value of fieldname author = AAA
fieldname ISBN = BBB
....

    Use [man]simplexml_load_string/man to load the data as a [man]SimpleXMLElement[/man] object.

    If you're unsure of how to use the object, easiest way to learn its structure is to do a [man]var_dump/man of it.

      i would like to use domdocument. is that a way for it??

      if im not wrong, i have to first select getElementsByTagName() then use childNodes??

      $elem = $xmlDoc->getElementsByTagName('Entity')->item(0);
      $children = $elem->childNodes;
      foreach ($children as $child) {
      echo $child->getAttribute('Item'); //im not getting any result here
      }

      but im not getting any result..

        nuttynibbles;10942514 wrote:

        i would like to use domdocument. is that a way for it??

        if im not wrong, i have to first select getElementsByTagName() then use childNodes??

        $elem = $xmlDoc->getElementsByTagName('Entity')->item(0);
        $children = $elem->childNodes;
        foreach ($children as $child) {
        echo $child->getAttribute('Item'); //im not getting any result here
        }

        but im not getting any result..

        There is no attribute 'Item'.

        'Item' is the name of the node.

        Try, getAttribute('value')

        I think the SimpleXML library is way easier to work with...
        http://www.php.net/manual/en/simplexmlelement.attributes.php

          Hey JohnPeace,

          it works. Tks dude.

          i was actually working using another method but it wasn't looping through the 2nd and 3rd row:

          $elem = $xmlDoc->getElementsByTagName("Category");
              $i = 0;
                foreach($elem as $x){
                  if($x->getElementsByTagName('Item')->item($i)->getAttribute('fieldname')=="Author")
                    echo "Author: ".$x->getElementsByTagName('Item')->item($i)->getAttribute('value')."<br />";
                  if($x->getElementsByTagName('Item')->item($i)->getAttribute('fieldname')=="ISBN")
                    echo "ISBN: ".$x->getElementsByTagName('Item')->item($i)->getAttribute('value')."<br />";
                  if($x->getElementsByTagName('Item')->item($i)->getAttribute('fieldname')=="Publisher")
                    echo "Publisher: ".$x->getElementsByTagName('Item')->item($i)->getAttribute('value')."<br />";
          
              $i++;
            }

          any idea why?? for curiosity sake =)

            Write a Reply...