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!