1

I am trying to add a child note to an XML file using PHP. I have used the same method about 5 times before and each one works as expected, however this method is using a different XML structure on a new file (which I think might be part of the problem).

The method causing me the problem is this:

function addCourseToProject($name, $project){
    $projects = self::getRoles();
    $theProject = $projects->xpath('project[name = "'.$project.'"]');
    echo $theProject->name;
    $theCourses = $theProject[0]->courses->addchild("course", $name);
    $projects->asXml("projects.xml");

}

The XML file looks like this:

<projects>
    <project>
        <name>Alpha</name>
            <courses>
                <course>Beta</course>
                <course>Charlie</course>
            </courses>
    </project>
</project>

And getRoles() looks like this:

function getRoles(){
     $roles = simplexml_load_file("projects.xml") or die("ERROR: Unable to read file");
return $roles;
}

I'm at a loss here, I have no idea why this is different from the other adding functions (see below).


Should you need it, here is an example of what my other methods look like:

function addModule($courseName, $name, $link){  
    $xml = self::getXml();#get the xml file
    $course = $xml->xpath('course[name = "'.$courseName.'"]');
    $module = $course[0]->addChild("module");
    $module->addChild("name", $name);
    $module->addChild("link", $link);
    $xml->asXml("data.xml");
    echo self::toJSON($course);
}

and the getXML() method:

function getXml(){
     $xml=simplexml_load_file("data.xml") or die("ERROR: Unable to read file");
    return $xml;
}
7
  • no, i'm looking for a project whose first child is <name>"Alpha"</name>... although, i could restructure the XML file if that would make this easier? Commented May 28, 2015 at 10:47
  • ah wait, no i can't, that would require me re-writing a number of functions :/ Commented May 28, 2015 at 10:50
  • 2
    I can't reproduce the problem, given that sample XML is valid (currently <projects> doesn't have corresponding closing tag), new course got added correctly : missingbrackets.com/codepad/editor/YM4Kp3. Could you post minimal codes demonstrating the problem? Commented May 28, 2015 at 10:55
  • sorry, what do you mean by minimal codes? Commented May 28, 2015 at 11:02
  • hmm, you seem to have solved my problem without realizing it... i was using $projects->xpath('project[name = "'.$project.'"]'); and you were using $projects->xpath('//project[name = "'.$project.'"]'); Commented May 28, 2015 at 11:10

1 Answer 1

1

Turned out that the problem was caused by the fact that <project> element is nested within the XML document. You can use descendant-or-self axis (// for the abbreviated syntax) to get nested element like so :

$theProject = $projects->xpath('//project[name = "'.$project.'"]');

or specify the complete path :

$theProject = $projects->xpath('/projects/project[name = "'.$project.'"]');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.