Reputation: 15
I am trying to make my PHP update an existing XML-file, but it doesn't work completely as it suppost to. The PHP-code suppose to add a childnode with elements within the root-element of the xml-file. But somehow the formatting is not working correctly and it puts all the newly added xml-elements in one line. If i remove the lastChild-option from the php it puts the newly added xml-elements outside the root but formats the xml correct.
Example existing XML-file:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<testing>
<test>test</test>
<test1>test1</test1>
<test2>test2</test2>
<test3>test3</test3>
</testing>
<testing>
<test>test</test>
<test1>test1</test1>
<test2>test2</test2>
<test3>test3</test3>
</testing>
<testing>
<test>test</test>
<test1>test1</test1>
<test2>test2</test2>
<test3>test3</test3>
</testing>
</root>
Example PHP-code:
$file = 'DEFAULT_DATE_CONVERSION4.xml';
$xml = new DOMDocument();
$xml->load($file);
$xml2 = $xml->documentElement->getElementsByTagName('datetimes');
$app = $xml->createElement('testing');
$testtext = "test";
$app->appendChild($xml->createElement('test', $testtext));
$app->appendChild($xml->createElement('test1', $testtext));
$app->appendChild($xml->createElement('test2', $testtext));
$app->appendChild($xml->createElement('test3', $testtext));
$xml->lastChild->appendChild($app);
$xml->formatOutput = true;
$xml->saveXML();
$xml->save("test.xml");
Result:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<testing>
<test>test</test>
<test1>test1</test1>
<test2>test2</test2>
<test3>test3</test3>
</testing>
<testing>
<test>test</test>
<test1>test1</test1>
<test2>test2</test2>
<test3>test3</test3>
</testing>
<testing>
<test>test</test>
<test1>test1</test1>
<test2>test2</test2>
<test3>test3</test3>
</testing>
<testing><test>test</test><test1>test</test1><test2>test</test2><test3>test</test3></testing></root>
Expected result:
<?xml version="1.0" encoding="UTF-8"?>
<datetimes>
<testing>
<test>test</test>
<test1>test1</test1>
<test2>test2</test2>
<test3>test3</test3>
</testing>
<testing>
<test>test</test>
<test1>test1</test1>
<test2>test2</test2>
<test3>test3</test3>
</testing>
<testing>
<test>test</test>
<test1>test1</test1>
<test2>test2</test2>
<test3>test3</test3>
</testing>
<testing>
<test>test</test>
<test1>test</test1>
<test2>test</test2>
<test3>test</test3>
</testing>
</datetimes>
Upvotes: 1
Views: 460
Reputation: 57121
You should get what your after by flagging to not preserve whitespace (preserveWhiteSpace
) and then setting to format output (formatOutput
) when loading the document...
$file = 'DEFAULT_DATE_CONVERSION4.xml';
$xml = new DOMDocument();
$xml->formatOutput = true;
$xml->preserveWhiteSpace = FALSE;
$xml->load($file);
$app = $xml->createElement('testing');
$testtext = "test";
$app->appendChild($xml->createElement('test', $testtext));
$app->appendChild($xml->createElement('test1', $testtext));
$app->appendChild($xml->createElement('test2', $testtext));
$app->appendChild($xml->createElement('test3', $testtext));
$xml->lastChild->appendChild($app);
$xml->save("test.xml");
Upvotes: 1