Connecting Tech Pros Worldwide Forums | Help | Site Map

Open a XML and parse it with DOM

Carles Company Soler
Guest
 
Posts: n/a
#1: Nov 23 '06
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



Martin Honnen
Guest
 
Posts: n/a
#2: Nov 23 '06

re: Open a XML and parse it with DOM


Carles Company Soler wrote:
Quote:
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).


--

Martin Honnen
http://JavaScript.FAQTs.com/
Closed Thread


Similar JavaScript / Ajax / DHTML bytes