Jump to content

print attributes of child node in dom xml


sdowney1

Recommended Posts

How can I print the attributes of a child node?

I have been trying and looking and cant get it.

I need the attributes of subfield printed after datafield

so a line would look like

datafield plus attributes plus subfield attributes plus subfield

 

=906 __$a0$bcbc$corignew etc...

 

<?php
//child nodes http://www.w3schools.com/php/php_xml_dom.asp
//http://books.google.com/books?id=ygQRKDEsLecC&pg=PA196&lpg=PA196&dq=print+php+xml+child+node+attributes&source=bl&ots=TCAhH4kpCt&sig=yJUPXesIkRUrNXdNE9Yr5ul7Wdw&hl=en&ei=U51kTeKQKsL48AbN4MDTBg&sa=X&oi=book_result&ct=result&resnum=10&ved=0CF4Q6AEwCQ#v=onepage&q=print%20php%20xml%20child%20node%20attributes&f=false
//http://stackoverflow.com/questions/4598409/printing-content-of-a-xml-file-using-xml-dom

$doc = new DOMDocument();
$doc->preserveWhiteSpace = FALSE;

$doc->load( 'loc.xml' );
$root = $doc->documentElement;
$type = $root->nodeType;
//print $doc->nodeName."\n";
//print $root->nodeName."\n";
//print $doc->nodeValue."\n";
//print $root->nodeValue."\n";


$librecords = $doc->getElementsByTagName( "record" );

foreach( $librecords as $record ){

   $leader = $record->getElementsByTagName( "leader" );
   $controlfields = $record->getElementsByTagName( "controlfield" ); 
   $datafields = $record->getElementsByTagName( "datafield" ); 
   $subfields = $record->getElementsByTagName( "subfield" );

   $leader = $leader->item(0)->nodeValue;  
   echo '=LDR '.$leader.'<BR>';

   foreach( $controlfields as $controlfield ){
      $tag = $controlfield->getAttribute('tag');  
      $cf_value = $controlfield->firstChild->nodeValue;
      echo "=".$tag." ".$cf_value.'<BR>';  }

   foreach( $datafields as $datafield ){
      $tag = $datafield->getAttribute('tag');  
      $ind1 = $datafield->getAttribute('ind1'); 
      $ind2 = $datafield->getAttribute('ind2'); 
      if ($ind1 ==" ") {$ind1 = "_";}
      if ($ind2 ==" ") {$ind2 = "_";}
      echo "=".$tag." ".$ind1.$ind2; 

      //if($datafield->hasChildNodes()){echo 'This node has children!<br />';}
      foreach ($datafield->childNodes AS $item)  {  
      //  $arr = $item->attributes; 
        print $arr;
      print 'nodeName '.$item->nodeName . " = " . $item->nodeValue." | " ;} //. "<br />";  }
  
  //   foreach ($datafield->children() as $node) {
  //      $arr = $node->attributes();   // returns an array
  //      print ("code=".$arr["code"]);     // get the value of this attribute
//       print ("  Company=".$node->subfield);
//        print ("<p><hr>");
//}
//     foreach( $subfields as $subfield){
      
//     foreach($subfield->attributes as $attr){

     // $attributes = $subfield->attributes;

     
  //      print $attr->nodeName;
    //    print $attr->nodeValue;
   //     //print $attributes->nodeName;
        //print $attributes->nodeValue;

   //    print $subfield->nodeName;
    //   print $subfield->nodeValue;

//}}
    
   // print $item->nodeValue  ;  
//       print $item->attributes("code");
   //       $valueID = $item->getAttribute('code'); 
   //      print $item->nodeName->attributes("code");
//          echo $item->nodeValue.getAttribute("code");
      //      print $item->getAttribute('code')
//http://www.devshed.com/c/a/PHP/Accessing-Attributes-and-Cloning-Nodes-with-the-DOM-XML-Extension-in-PHP-5/1/

      echo '<BR>'; 
     
     }





   foreach( $subfields as $subfield ){
      $code = $subfield->getAttribute('code');  
      $sf_value = $subfield->firstChild->nodeValue;
      echo '$'.$code.$sf_value.'<BR>'; }

echo '<BR>';

}
?>

   

 

  <leader>01026ngm a22002773a 4500</leader>
  <controlfield tag="001">16429180</controlfield>
  <controlfield tag="005">20100823131409.0</controlfield>
  <controlfield tag="007">vffcjaho|</controlfield>
  <controlfield tag="008">100823s2010    xxu060            mleng  </controlfield>
  <datafield tag="906" ind1=" " ind2=" ">
    <subfield code="a">0</subfield>
    <subfield code="b">cbc</subfield>
    <subfield code="c">orignew</subfield>
    <subfield code="d">u</subfield>
    <subfield code="e">ncip</subfield>
    <subfield code="f">20</subfield>
    <subfield code="g">y-movingim</subfield>
  </datafield>
  <datafield tag="955" ind1=" " ind2=" ">
    <subfield code="b">qm12 2010-08-23</subfield>
  </datafield>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.