XMLDocument.getElementById()
Getting element with specified ID (eg. id="id1"
) from XML file received via xmlHttpRequest is really hard!
Let’s take the following JavaScript code:
1 | var ajax = new XMLHttpRequest(); |
and test.xml
:
1 | <xml> |
You know what would happen? Script will fail (in Firefox and Opera; Chrome will do fine) in this line:
1 | doc.getElementById('id1').appendChild() |
It’s obvious that you cannot convert doc.getElementById('id1')
to object, right?
If not - go ahead.
Your browser does not know (or does not want to know), that id=""
is really a unique identifier.
Everything will start to work when, at the beginning of an XML file, we put:
1 | <!DOCTYPE xml [ <!ATTLIST tenelement id ID #IMPLIED> ]> |
Until… we would try to send this document to the server:
1 | var ajax2 = new XMLHttpRequest(); |
Browser will send the DOCTYPE, but without the most important thing ([ <!ATTLIST tenelement id ID #IMPLIED> ]
).
To make it work, I had to modify the received file in save.php by adding a missing part of a DOCTYPE.
Someone knows a better solution?