PHP - get value of an XML attribute -


source.xml

<property>     <name>a</name>     <description>aaa</description>     <example value="b" description="bbb">b1</example>     <example value="c" description="ccc">c1</example>    </property>  

search.php

$xmldoc=new domdocument(); $xmldoc->load("source.xml"); $x=$xmldoc->getelementsbytagname('property');     $z=$x->item(0); $c=$z->childnodes; ($j=4;$j<($cd->length);$j++) {  echo ("<div>" . $c->item($j)->attributes()->description . "</div>");  echo ("<div>" . $c->item($j)->childnodes["description"] . "</div>");  echo ("<div>" . $c->item($j)["description"] . "</div>"); } 

i want return attribute description. have tried lot, nothing working correctly.

<?php  $xmldoc=new domdocument(); $xmldoc->load('source.xml');  $properties = $xmldoc->getelementsbytagname('property');     $first_property = $properties->item(0); echo "$first_property->tagname\n";  $first_property_children = $first_property->childnodes; echo $first_property_children->length;  /* ($j=4;$j<($cd->length);$j++) {  echo ("<div>" . $c->item($j)->attributes()->description . "</div>");  echo ("<div>" . $c->item($j)->childnodes["description"] . "</div>");  echo ("<div>" . $c->item($j)["description"] . "</div>"); }  */ ?>  --output:-- property 9 

9??!! wtf?! here's xml file looks like:

<property>\n  #<==see this??     <name>a</name>\n  #<==and here??     <description>aaa</description>\n     <example value="b" description="bbb">b1</example>\n     <example value="c" description="ccc">c1</example> \n   </property>  

when hit return in text file, invisible newline character entered file. every 1 of newlines considered xml enclosed in invisible text node. because there 5 text nodes created 5 newlines inside property tag, 4 other visible nodes, there total of 9 children in property tag.

in loop, land on visible elements, start @ childnodes[1] (skipping first newline text node), , skip every other childnode:

for ($j=1; $j<($property_children->length); $j += 2) {      echo ("<div>" . $property_children->item($j)->nodevalue . "</div>\n");      //echo ("<div>" . $first_property_children->item($j)->nodevalue . "</div>");     //echo ("<div>" . $first_property_children->item($j)->childnodes["description"] . "</div>");     //echo ("<div>" . $first_property_children->item($j)["description"] . "</div>"); }  ?>  --output:-- <div>a</div> <div>aaa</div> <div>b1</div> <div>c1</div> 

that gives text of visible tags.

instead of looping, can figure out position of description tag in childnodes array. here description tag located in childnodes array:

                    +- spaces @ start of next line                     |              0      v           1            2                    3 <property><text>\n    </text><name>a</name><text>\n   </text><description>aaa</description>.... 

so, can write:

echo $property_children->item(3)->nodevalue;  //nodevalue gives text   --output:-- aaa 

but having count nodes , take account invisible text nodes created whitespace inside tag of hassle poor programmers, there other ways of obtaining child node:

<?php  if ($property = simplexml_load_file('source.xml') )  {     //grab child description tags in array:     echo $property->description[0] . "\n";      //doing same thing xpath(which treats xml directory structure on computer):     echo $property->xpath('./description')[0] . "\n";  }  ?>   --output:-- aaa aaa 

see basic simplexml usage in php docs.

i want return attribute description.

an attribute? mean description attributes example tags?

you can treat tag array , use attribute name subscript:

<?php  if ($property = simplexml_load_file('source.xml') )  {      $example_tags = $property->example;      foreach($example_tags $example_tag) {         echo $example_tag['description'];     }  }  ?> --output:-- bbb ccc 

here xpath:

<?php  if ($property = simplexml_load_file('source.xml') )  {      $description_attrs = $property->xpath('./example/@description');      foreach($description_attrs $description_attr) {         echo "$description_attr\n";     }   }  ?>  --output:-- bbb ccc 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -