4

I have some problems making a function to replace question and answer elements in my XML file. I did alot of research learning php dom but I'm running stuck at making this function. The problem is that when I'm trying to get the parent element row by attribute id it returns null or when I use xpath it returns an empty object. I don't know if the function will work correct after I get the the right parent element but that's the next step I think.

my XML looks like this:

<?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>

and my php function:

//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);
}

$rowIds is an array which contains the numbers of the rows that where modified ans rowText in an array which contains the question and answer strings like array(0=>"question",1=>"answer") and so on. The XML file content can get bigger or smaller by adding or removing rows. I have already made "working" functions for that.

Zeebats
  • 480
  • 7
  • 22
  • And your question is? – hakre Apr 25 '13 at 11:10
  • How do I get this parent element by attribute id, because that is the problem I explainen. The getElementById and the xpath query for getting an element by id do not return the element. – Zeebats Apr 25 '13 at 11:23
  • If already the xpath and getElementById do not return the element you want to get the parent from, then your question should be how to get the element itself first *bevore* thinking about obtaining the parent. That is why I'm asking. What does went wrong with Getelementbyid? That function should normally work unless the ID is set to a different attribute-name (which does not look to be the case with your example XML). – hakre Apr 25 '13 at 11:25
  • I want the xpath and getelementbyid return as the parent itself. so I can modify the children of that element. I think it's a bit strange that I cant get the element "row" by its id. – Zeebats Apr 25 '13 at 11:33
  • Sure you can. I mean this actually works. And the parent then is `parentElement` in DOMDocument IIRC and for xpath this is `..` like in the file-system. However it seems your problem is located elsewhere because you already can not get the element with the id, don't you? – hakre Apr 25 '13 at 11:34
  • I got an removeRow function as wel. This function is based on the same rowId array. but this function works great. – Zeebats Apr 25 '13 at 11:41
  • Well probably you're doing too much in one function. If both functions could re-use the part that works, wouldn't it be great? There would be less places where errors can occur and less places to debug. – hakre Apr 25 '13 at 11:42
  • Also what I see in your code is that you do not add the element you create. Probably that is your issue? Are the elements missing you want to add there? – hakre Apr 25 '13 at 11:43
  • when I run the function I get the error: Call to a member function getElementsByTagName() on a non-object. If that is what you mean by not adding. I think this error ocures because $parent is null when I check if it is null – Zeebats Apr 25 '13 at 11:50
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28888/discussion-between-hakre-and-user2119398) – hakre Apr 25 '13 at 11:51

1 Answers1

13

I have solved the problem thanx to hakre and the internet. So for people with the same problem this is how I solved it.

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