webpage_making1
webpage_making1

Reputation: 3

Creating XML in PHP saves a blank file

im new to XML Handling in PHP i wrote this script to get variables from post and insert them into a tag with the name of the variable it self from the posted variables and the data the actual data inside this text fields i have set $id_picture to foo instead of the posted data but with same result

$xml = new DOMDocument('1.0', 'UTF-8'); 

        $id_picture = $_POST['id_picture']; 
        $xml_id_picture = $xml->createElement("id_picture");
        $xml_id_picture_node = $xml->createTextNode($id_picture); 
        $xml_id_picture->appendChild($xml_id_picture_node);
        //upload xml 
        $xml->save('xml.xml');

what im trying to achieve is save the data from the post to the first variable then i get lost on making it a xml tag and inserting the data in between

<id_picture>foo</id_picture>

Upvotes: 0

Views: 89

Answers (3)

Xildatin
Xildatin

Reputation: 146

It looks like you are creating an element, appending a node to it, but then you are not appending the element to the document.

Upvotes: 0

Musa
Musa

Reputation: 97672

You have to add the node to the document.

    $xml = new DOMDocument('1.0', 'UTF-8'); 

    $id_picture = $_POST['id_picture']; 
    $xml_id_picture = $xml->createElement("id_picture");
    $xml_id_picture_node = $xml->createTextNode($id_picture); 
    $xml_id_picture->appendChild($xml_id_picture_node);
    $xml->appendChild($xml_id_picture);//<-- here
    //upload xml 
    $xml->save('xml.xml');

http://codepad.org/6Ml2cUYe

Upvotes: 0

Marc B
Marc B

Reputation: 360562

You never inserted your new node into the main object. You need something like

$xml->appendChild($xml_id_picture);

so that your newly created id_picture node will actually show up in your document.

Upvotes: 1

Related Questions