0

I'm working on project, one of its functions is to add data to XML file that will be extracted later, the extracting part is working well but not the adding part. I created a new php file that contains the code of XML adding part, but still not working. Here is the php file:

<?
$xml = simplexml_load_file('test.xml');
$msg = $xml->addChild('msg');
$msg->addChild('name', 'newName');
$msg->addChild('text', 'myText');
?>

Here is the test.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<msgs>

</msgs>

The page shows no php error and the xml still the same. I have been looking for solution for hours but couldn't find. Please help!

2
  • 2
    a) Do you use SimpleXMLElement::asXML($filename) somewhere in that script? b) Does your php instance recognize the short tag <?? Commented Feb 5, 2016 at 1:06
  • Short open tag is enabled, but i didn't used asXML. Commented Feb 5, 2016 at 1:26

2 Answers 2

1

simplexml_load_file() returns a SimpleXMLElement to which you're adding elements, you now need to serialise that instance back to a file:

$msg->asXML('output.xml');

Or if you just want to capture the XML as a string:

$xmlString = $msg->asXML();
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you answered while i was writing my answer, thanks anyway :D
1

The problem was that it does what its supposed to do but not saving, adding this $xml->asXML('test.xml'); to the end of php will make it save in the XML file. Found it by reading the filename part of asXML doc. here asXML()

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.