-4

Possible Duplicate:
How is possible add child to the specific node

how i can add nodes to xml file like that:

<users>
<user uid="1" name="Alvin" email="example@example.com"/>
</users>

example php:

$xml_file = "example.xml";
    // WRITING TO XML FILE
        if(!empty($_GET['add'])){
        $u_name = stripslashes($_POST['u_name']);
        $u_email = stripslashes($_POST['u_email']);
    // Generate new (appended) ID
        foreach($xml as $user){
        $last_id = $user->uid;
        }
        $id = $last_id+1;
    // Add node
        $x = $xml->addChild("user");
        $x->addChild("uid",$id);
        $x->addChild("name",$u_name);
        $x->addChild("email",$u_email);
        $xml->asXML($xml_file);
    }

thank you for any help and/or advise

Community
  • 1
  • 1
zippo
  • 339
  • 3
  • 10
  • 1
    Please use the search, e.g. see here: [How is possible add child to the specific node](http://stackoverflow.com/questions/9706558/how-is-possible-add-child-to-the-specific-node) – hakre Mar 14 '12 at 17:40
  • or perhaps google. searching on "add nodes to xml with php" returns almost 8 million results. – Brian Mar 14 '12 at 17:42

2 Answers2

1

Check out SimpleXML http://php.net/manual/en/book.simplexml.php

daker
  • 3,430
  • 3
  • 41
  • 55
1

Remember to try the search function at the top of the Stackoverflow site. There are several questions already submitted and answered that are on this topic.

Check this previous question on Read and write an XML file with PHP

UPDATE: Here is a snippet from the accepted answer in the referenced question:

$pos = strpos($xml, "</videos>");

if ($pos === false) {
    $xml = substr($xml,0,$pos)."\t<video url=\"$url\" desc=\"$desc\" />".substr($xml,$pos);
}
Community
  • 1
  • 1
Thomas Bates
  • 677
  • 4
  • 15