使用PHP DOM获取子节点的值 [英] Get the values of the child nodes with PHP DOM

查看:57
本文介绍了使用PHP DOM获取子节点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用的XML示例:

Here is an example XML I use:

    <LISTING diffgr:id="LISTING1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
    <ID>ACCAMAQU0470001P</ID>
    <DATECREA>2013-01-28T09:45:21+01:00</DATECREA>
    <DATEMAJ>2014-01-09T17:41:25+01:00</DATEMAJ>
    ...
    </LISTING>
    ...

在PHP代码中,我在这里:

In the PHP code, here I am:

$document_xml = new DOMDocument();
$document_xml->loadXML($retour['any']);
$elements = $document_xml->getElementsByTagName('LISTING');

while ($elements->item($i)) {
    $element = $elements->item($i); // On obtient le nœud 
    $list = $element->childNodes; // On récupère les nœuds avec childNodes
    $idtest = $element->getElementsByTagName('DATEMAJ');
    $idElem = $element->getElementsByTagName('ID');
    foreach($idElem as $idSirtaq){
        $idList[] = $idSirtaq->firstChild->nodeValue;
    }        
    foreach ($idtest as $test) {   
        //HERE
        ...
    }
    ...
 }

我将获取节点"ID"和"DATEMAJ"的值.我知道要使用 $ test-> firstChild-> nodeValue 来获取"DATEMAJ"值,而不是如何获取节点"ID"的值.

I would to get the value of nodes "ID" and "DATEMAJ". I know to get the "DATEMAJ" value, with $test->firstChild->nodeValue, but not how to retrieve the value of the node "ID".

推荐答案

尝试

$document_xml = new DOMDocument();
$document_xml->loadXML($xml);
$elements = $document_xml->getElementsByTagName('LISTING');
foreach ($elements as $node) {
    $idtest = $node->getElementsByTagName('DATEMAJ');
    $idElem = $node->getElementsByTagName('ID');
    $idList[] = $idElem->item(0)->nodeValue;
 }

此处

这篇关于使用PHP DOM获取子节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆