Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php xml dom getElementById和DOMXpath查询无法获取元素_Php_Xml_Domdocument_Getelementbyid_Domxpath - Fatal编程技术网

php xml dom getElementById和DOMXpath查询无法获取元素

php xml dom getElementById和DOMXpath查询无法获取元素,php,xml,domdocument,getelementbyid,domxpath,Php,Xml,Domdocument,Getelementbyid,Domxpath,我在创建替换XML文件中问答元素的函数时遇到一些问题。我在学习PHPDOM时做了很多研究,但我一直在做这个函数。问题是,当我试图按属性id获取父元素行时,它返回null,或者当我使用xpath时,它返回空对象。我不知道在得到正确的父元素后,函数是否能正常工作,但我认为这是下一步 我的XML如下所示: <?xml version="1.0" encoding="UTF-8"?> <root> <row id="1"> <question>

我在创建替换XML文件中问答元素的函数时遇到一些问题。我在学习PHPDOM时做了很多研究,但我一直在做这个函数。问题是,当我试图按属性id获取父元素行时,它返回null,或者当我使用xpath时,它返回空对象。我不知道在得到正确的父元素后,函数是否能正常工作,但我认为这是下一步

我的XML如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <row id="1">
    <question>Wat doet de vuilnisman?</question>
    <answer>Hij tilt de vuilnisbak in de vuilnisauto.</answer>
  </row>
  <row id="2">
    <question>Wat zegt de tandarts vaak?</question>
    <answer>U mag nu spoelen.</answer>
  </row>
</root>

维尼斯曼在哪里?
Hij tilt de vuilnisbak位于vuilnisauto。
你在哪里?
你是玛格努斯波伦。
和我的php函数:

//modifies rows
function modifyRows($file, $rowIds, $rowText) {
    $xmlDoc = new DOMDocument();
    $xmlDoc->preserveWhiteSpace = false;
    $xmlDoc->formatOutput = true;   
    $xmlDoc->load($file);
    foreach($rowIds as $rowId) {
        $parent = $xmlDoc->getElementById($rowId);
        for($i=0;$i<count($rowText);$i++) {
            $newQ = $xmlDoc->createElement('question');
            $qText = $xmlDoc->createTextNode($rowText[$i]);
            $qText = $newQ->appendChild($qText);
            $newA = $xmlDoc->createElement('answer');
            $aText = $xmlDoc->createTextNode($rowText[$i++]);
            $aText = $newA->appendChild($aText);
        }
        $oldQ = $parent->getElementsByTagName('question');
        $oldA = $parent->getElementsByTagName('answer');        
        $parent->replaceChild($newQ, $oldQ);
        $parent->replaceChild($newA, $oldA);
    }   
    $xmlDoc->save($file);
}
//修改行
函数modifyRows($file、$rowIds、$rowText){
$xmlDoc=新的DOMDocument();
$xmlDoc->preserveWhiteSpace=false;
$xmlDoc->formatOutput=true;
$xmlDoc->load($file);
foreach($rowIds作为$rowId){
$parent=$xmlDoc->getElementById($rowId);
对于($i=0;$icreateElement('question');
$qText=$xmlDoc->createTextNode($rowText[$i]);
$qText=$newQ->appendChild($qText);
$newA=$xmlDoc->createElement('answer');
$aText=$xmlDoc->createTextNode($rowText[$i++]);
$aText=$newA->appendChild($aText);
}
$oldQ=$parent->getElementsByTagName('question');
$oldA=$parent->getElementsByTagName('answer');
$parent->replaceChild($newQ,$oldQ);
$parent->replaceChild($newA,$oldA);
}   
$xmlDoc->save($file);
}

$rowIds是一个数组,它包含在一个数组中修改的行数,该数组包含问答字符串,如数组(0=>“问题”,1=>“答案”)等。通过添加或删除行,XML文件内容可以变大或变小。我已经做了“工作”功能。

我已经解决了thanx到hakre和互联网的问题。所以对于有相同问题的人,我就是这样解决的

    function getElementById($id) {
        $xpath = new DOMXPath($this->xmlDoc);
        return $xpath->query("//*[@id='$id']")->item(0);
    }

您的问题是?如何通过属性id获取父元素,因为这是我解释的问题。getElementById和用于通过id获取元素的xpath查询不返回元素。如果xpath和getElementById已经不返回您想要从中获取父元素的元素,那么您的问题应该是如何获取元素本身首先考虑获取父元素。这就是我问的原因。Getelementbyid出了什么问题?该函数应该正常工作,除非该ID设置为其他属性名称(您的示例XML看起来不是这样)。我希望xpath和getelementbyid返回作为父元素本身。因此,我可以修改该元素的子元素。我觉得有点奇怪,我无法通过其id获取元素“row”