Creating and modifying XML file

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
User avatar
axymark
Posts: 23
Joined: November 29th, 2013, 7:04 am

November 27th, 2014, 5:28 am

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.
User avatar
axymark
Posts: 23
Joined: November 29th, 2013, 7:04 am

November 27th, 2014, 1:26 pm

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...
Post Reply