XML node extraction Problems

Source: Internet
Author: User
When the existing PHP code outputs an XML node, it can only output the content of the first node with the same name & quot; 100 & quot;. How can I output the content of all nodes? For the existing code, see: & amp; lt ;? Php * $ xmlstring original content: ** & amp; lt ;? XML version & quot; 1.0 & quot ;? & Amp; gt; & amp; lt ;! DOCTYPEGetUserInfo & amp; gt;-& amp ;... when the existing PHP code outputs an XML node, only the content of the first node with the same name "100" can be output ". how can I output the content of all nodes?

For the existing code, see:
/* $ Xmlstring original content:
**

-
Id * cn
100
101
102
103
105
**
*/

$ Xmldoc = new DOMDocument ();
$ Xmldoc-> loadXML ($ xmlstring );
$ Users = $ xmldoc-> getElementsByTagName ('getuserinfo ');
Foreach ($ users as $ user ){
$ Html. = 'outputcustomer: '. get_txt ($ user, 'customer ').'
';
$ Html. = 'outputgeo: '. get_txt ($ user, 'geocallcli ').'
';
}
Echo $ html;

Function get_txt ($ parent, $ name ){
$ Nodes = $ parent-> getElementsByTagName ($ name );
Return $ nodes-> item (0)-> nodeValue;
}
?>

Reply content:

When the existing PHP code outputs an XML node, it can only output the content of the first node with the same name "100". How can I output the content of all nodes?

For the existing code, see:
/* $ Xmlstring original content:
**

-
Id * cn
100
101
102
103
105
**
*/

$ Xmldoc = new DOMDocument ();
$ Xmldoc-> loadXML ($ xmlstring );
$ Users = $ xmldoc-> getElementsByTagName ('getuserinfo ');
Foreach ($ users as $ user ){
$ Html. = 'outputcustomer: '. get_txt ($ user, 'customer ').'
';
$ Html. = 'outputgeo: '. get_txt ($ user, 'geocallcli ').'
';
}
Echo $ html;

Function get_txt ($ parent, $ name ){
$ Nodes = $ parent-> getElementsByTagName ($ name );
Return $ nodes-> item (0)-> nodeValue;
}
?>

Last line of code

return $nodes->item(0)->nodeValue;

Because only item (0) is returned)
So only one 100
You only need to check the length of $ nodes and output it cyclically.
Code


  
  
   
    id*cn
   
   
    100
   
   
    101
   
   
    102
   
   
    103
   
   
    105
   
  ';$xmldoc = new DOMDocument();$xmldoc->loadXML($xmlstring);$users = $xmldoc->getElementsByTagName('GetUserInfo');$html  = '';foreach ($users as $user) {    $html .= get_txt($user, 'Customer') . '';    $html .= get_txt($user, 'GeocallCLI') . '';}echo $html;function get_txt($parent, $name){    $nodes = $parent->getElementsByTagName($name);        $data = '';    for ($i = 0; $i < $nodes->length; $i++) {        $data .= 'output' . $name . ':' . $nodes->item($i)->nodeValue . '
'; } return $data;}?>

Code example. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.