Open a XML and parse it with DOM

  • Thread starter Carles Company Soler
  • Start date
C

Carles Company Soler

Hi,
how can I open an xml document and parse it with DOM? For example I want to
access it via getElementById and all this. I'm using Mozilla Firefox if
that's important (it's an application using XUL and SVG).

Thanks
 
M

Martin Honnen

Carles said:
how can I open an xml document and parse it with DOM? For example I want to
access it via getElementById and all this. I'm using Mozilla Firefox if
that's important (it's an application using XUL and SVG).

With Mozilla you have two options, you can create a dummy DOM XML
document e.g.

var xmlDocument = document.implementation.createDocument('', 'dummy',
null);
xmlDocument.onload = function (evt) {
// now access document contents here e.g.
alert('Found ' + xmlDocument.getElementsByTagName('*').length +
'eleemnts.');
};
xmlDocument.load('file.xml');

or you can use XMLHttpRequest to load the file and then access the
responseXML property.

Note that getElementById in XML documents does only work if the DTD
defines the attributes of type ID and the parser reads the DTD or has
that information hardcoded for certain elements (e.g. XHTML or SVG
elements).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top