接收来自 xml 文件的响应时如何获取不同的标记值

我有一个 xml 文件和一个 php 文件。我收到了来自 xml 文件的结果,但我无法获得标签的不同值。我想要的是来自各个标签的数据。知道该怎么做它?这是xml文件:


<?xml version="1.0" encoding="utf-8"?>

<users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">



<user>

<id>1</id>

<username>neem99</username>

<password>dbhcasvc</password>

<email>vgwdevwe@hfvuejd.com</email>

</user>

</users>

示例 php 文件:


$xp = new DOMXPath( $dom );

echo var_dump($xp);

$col = $xp->query( $query );

echo var_dump($col);

$array = array();

if( $col->length > 0 ){

    foreach( $col as $node) echo $node->nodeValue

}



result :   1 neem99 dbhcasvc vgwdevwe@hfvuejd.com 


隔江千里
浏览 102回答 2
2回答

回首忆惘然

DOMXpath::evaluate()允许使用返回标量值的 Xpath 表达式。string()通过返回第一个节点的文本内容将节点列表转换为字符串。演示:$document = new DOMDocument();$document->loadXML($xml);$xpath = new DOMXpath($document);// get first user idvar_dump($xpath->evaluate('string(/users/user/id)'));//iterate all user nodesforeach ($xpath->evaluate('/users/user') as $user) {&nbsp; &nbsp; // get its username&nbsp; &nbsp; var_dump($xpath->evaluate('string(username)', $user));}

叮当猫咪

我会喜欢:$doc = new DOMDocument; @$doc->load('yourFileName.xml');$user = $doc->getElementsByTagName('user');foreach($user as $u){&nbsp; echo 'nodeName:'.$u->nodeName.'; nodeValue:'.$u->nodeValue.PHP_EOL;}
打开App,查看更多内容
随时随地看视频慕课网APP