Page 1 of 1

Creating and modifying XML file

Posted: November 27th, 2014, 5:28 am
by axymark
Does anyone have some examples on how to create an XML file and modify it (adding elements and such)?

I know the basics but i've mostly dealt with modification of existing XML files in the past, not creating one from the scratch.

Also, i figured .appendChild(), but when i read XML file, append some element and write the modified XMl into a file, i lose the declaration for some reason.

This:

Code: Select all

<?xml version="1.0"?>
<root>
    <something1></something1>
</root>
Becomes this:

Code: Select all

<root>
    <something1></something1>
    <something2></something2>
</root>
Notice the missing declaration at line 1.

Any help appreciated.

Re: Creating and modifying XML file

Posted: November 27th, 2014, 1:26 pm
by axymark
Figured out everything except the way to keep the declaration.
If someone knows how to read it out, do tell...

Went with getting the first line as you would from a simple text file, checking if it starts with "<?xml", and if it does, saving it out and writing it into file before the modified xml stuff...