在DomXML和PHP中通过属性值获取节点

在DomXML和PHP中通过属性值获取节点,php,xml,Php,Xml,我试图选择一个特定的节点来编辑它的内容和属性,但是我无法选择一个注意节点(参见下面的PHP代码) XML文档 <?xml version="1.0" encoding="UTF-8"?> <notices lastID="0001"> <!-- <notice id="0000" endDate="31-12-2025"> Content of the notice. </notice> --&

我试图选择一个特定的节点来编辑它的内容和属性,但是我无法选择一个
注意
节点(参见下面的PHP代码)

XML文档

<?xml version="1.0" encoding="UTF-8"?>
<notices lastID="0001">
    <!--
    <notice id="0000" endDate="31-12-2025">
        Content of the notice.
    </notice>
    -->
    <notice id="0001" endDate="13-01-2013" active="1">
        One amazing notice.
    </notice>

</notices>
每次我陷入
if
语句时,我的
$notice
都不返回任何值。
我尝试了xpath查询(
//注意[@id=“{$id}”]
),但没有成功

为了澄清,我的问题是
$element->getAttribute('id')
似乎不起作用


我还尝试了使用SimpleXML

PHP

$document = new DOMDocument;
$document = dom_import_simplexml($this->xml);

$notices= $document->getElementsByTagName('notice');
#var_dump($notices);

foreach($notices as $element)
{
    if($element->getAttribute('id') == $id)
    {
        $notice = $element;
        var_dump($notice); //Returns absolutely nothing (I guess the if returns false).
    }
}

$notice->removeAttribute("endDate");
$notice->setAttribute("endDate",$endDate);
$document = new DOMDocument;
$document = simplexml_import_dom($this->xml);

$notice = "";

foreach($document->notices->children() as $element)
{
    $attributes = $element->attributes();
    if($attributes['id'] == $id)
    {
        $notice= $element;
        var_dump($notice);
    }
}
$avis->removeAttribute("endDate");
$avis->setAttribute("endDate",$endDate);
$document = simplexml_import_dom($this->xml);
$notice = "";

$xpathResults = $document->xpath('//notice');


foreach($xpathResults as $element)
{
    $elementID = $element->id[0];
    $domXML = dom_import_simplexml($element);

    if((string) $noticeID == $id)
    {
        $notice = $domXML;

    }
}
SimpleXML给了我以下消息:
节点不再存在
在下一行:

foreach($document->notices->children()作为$element)
我终于得到了它

PHP

$document = new DOMDocument;
$document = dom_import_simplexml($this->xml);

$notices= $document->getElementsByTagName('notice');
#var_dump($notices);

foreach($notices as $element)
{
    if($element->getAttribute('id') == $id)
    {
        $notice = $element;
        var_dump($notice); //Returns absolutely nothing (I guess the if returns false).
    }
}

$notice->removeAttribute("endDate");
$notice->setAttribute("endDate",$endDate);
$document = new DOMDocument;
$document = simplexml_import_dom($this->xml);

$notice = "";

foreach($document->notices->children() as $element)
{
    $attributes = $element->attributes();
    if($attributes['id'] == $id)
    {
        $notice= $element;
        var_dump($notice);
    }
}
$avis->removeAttribute("endDate");
$avis->setAttribute("endDate",$endDate);
$document = simplexml_import_dom($this->xml);
$notice = "";

$xpathResults = $document->xpath('//notice');


foreach($xpathResults as $element)
{
    $elementID = $element->id[0];
    $domXML = dom_import_simplexml($element);

    if((string) $noticeID == $id)
    {
        $notice = $domXML;

    }
}

您是否检查了XML文档是否已正确加载?不确定这是否是答案,但尝试使用
DOMDocument::load()
加载文件,您不需要创建它的新实例
DOMDocument
。您可以直接使用xpath来实现。您可能需要找出它不适用于您的原因,可能
$id
包含了一些不同的内容,或者您的xpath表达式已损坏。使用变量,如果它不起作用,您可以使用var_转储这些变量并查看发生了什么: