Home >Backend Development >PHP Tutorial >怎样输出XML所有的同名节点内容?

怎样输出XML所有的同名节点内容?

WBOY
WBOYOriginal
2016-06-06 20:18:361547browse

现有的PHP代码输出XML节点时,只能输出第一个同名节点内容"100". 请问怎样才能输出所有节点的内容呢?

现有代码:
/* $xmlstring 原内容:


-
id*cn
100
101
102
103
105

*/

$xmldoc = new DOMDocument();
$xmldoc->loadXML($xmlstring);
$users = $xmldoc->getElementsByTagName('GetUserInfo');
foreach ($users as $user) {

<code>$html .= 'outputCustomer:'.get_txt($user,'Customer').'<br>';
$html .= 'outputGeo:'.get_txt($user,'GeocallCLI').'<br>';</code>

}
echo $html;

function get_txt($parent, $name) {

<code>$nodes = $parent->getElementsByTagName($name);
return $nodes->item(0)->nodeValue;</code>

}
?>

另问怎样只输出 idcn节点中号前面部份, 即 id 而不是 id*cn

回复内容:

现有的PHP代码输出XML节点时,只能输出第一个同名节点内容"100". 请问怎样才能输出所有节点的内容呢?

现有代码:
/* $xmlstring 原内容:


-
id*cn
100
101
102
103
105

*/

$xmldoc = new DOMDocument();
$xmldoc->loadXML($xmlstring);
$users = $xmldoc->getElementsByTagName('GetUserInfo');
foreach ($users as $user) {

<code>$html .= 'outputCustomer:'.get_txt($user,'Customer').'<br>';
$html .= 'outputGeo:'.get_txt($user,'GeocallCLI').'<br>';</code>

}
echo $html;

function get_txt($parent, $name) {

<code>$nodes = $parent->getElementsByTagName($name);
return $nodes->item(0)->nodeValue;</code>

}
?>

另问怎样只输出 idcn节点中号前面部份, 即 id 而不是 id*cn

自己顶上, 哪位大侠能解答下么
我顶

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn