php - 怎样输出XML所有的同名节点内容?
PHP中文网
PHP中文网 2017-04-10 16:46:53
0
1
596

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

现有代码:
<?php
/* $xmlstring 原内容:
<?xml version="1.0"?>
<!DOCTYPE GetUserInfo>
-<GetUserInfo>
<Customer>id*cn</Customer>
<GeocallCLI>100</GeocallCLI>
<GeocallCLI>101</GeocallCLI>
<GeocallCLI>102</GeocallCLI>
<GeocallCLI>103</GeocallCLI>
<GeocallCLI>105</GeocallCLI>
</GetUserInfo>
*/

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

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

}
echo $html;

function get_txt($parent, $name) {

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

}
?>

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

PHP中文网
PHP中文网

认证高级PHP讲师

répondre à tous(1)
阿神

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

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!