getElementById does not work on returned value of DOMParser.parseFromString()

E

edai

I am working on a code where I am loading XML data from a file on the
server using
.......
if (xmlhttp.readyState==4) {
value=xmlhttp.responseText;
var parser=new DOMParser();
xml=parser.parseFromString(value,"text/xml")
........
when I use getElementsByTagName I get
============firebug output==========[<foo address="0x4123" id="001" name="isp1582">]

alert(xml.getElementsByTagName("module") === null) gives FALSE
============End firebug output==========
but on using getElementById I get
===============Firebug Output===============
alert(xml.getElementById("001") === null) gives TRUE===============End FirebugOutput============

I am expecting xml.getElementById("001") to return the same node as
xml.getElementsByTagName('foo')[0]
Am I missing something?
I am using firefox version 1.5.0.9
 
P

p.lepin

I am working on a code where I am loading XML data from a
file on the server using
[...]

var parser=new DOMParser();
xml=parser.parseFromString(value,"text/xml")
I am expecting xml.getElementById("001") to return the
same node as xml.getElementsByTagName('foo')[0]
Am I missing something?

I believe getElementById() does not look up an element with
the id attribute that equals to the value you've provided.
Instead, it looks up an element with the attribute
*defined* as an ID (in DTD or XML Schema--but I don't think
I've heard of browsers supporting schemata) that equals to
the value you've provided. Basically, you'll need a DTD for
the XML you're trying to work with if you want to use
getElementById().

I might be wrong, so I'd suggest doing some reading on the
subject, the relevant materials should be relatively easy
to obtain on the net.
 
E

edai

I am working on a code where I am loading XML data from a
file on the server using
[...]

var parser=new DOMParser();
xml=parser.parseFromString(value,"text/xml")
I am expecting xml.getElementById("001") to return the
same node as xml.getElementsByTagName('foo')[0]
Am I missing something?

I believe getElementById() does not look up an element with
the id attribute that equals to the value you've provided.
Instead, it looks up an element with the attribute
*defined* as an ID (in DTD or XML Schema--but I don't think
I've heard of browsers supporting schemata) that equals to
the value you've provided. Basically, you'll need a DTD for
the XML you're trying to work with if you want to use
getElementById().

I might be wrong, so I'd suggest doing some reading on the
subject, the relevant materials should be relatively easy
to obtain on the net.

You are right. I was able to find a discussion on this issue at http://
www.subbu.org/weblogs/main/2006/04/ajax_and_getele.html
For the time being I have changed my xml file to have header similar
to an xhtml file and it seems to be working.
i.e added
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
to the top and encapsulated the data in
<html xmlns="http://www.w3.org/1999/xhtml">
</html>
 
P

p.lepin

var parser=new DOMParser();
xml=parser.parseFromString(value,"text/xml")
I am expecting xml.getElementById("001") to return
the same node as xml.getElementsByTagName('foo')[0]
Am I missing something?
I believe getElementById() does not look up an element
with the id attribute that equals to the value you've
provided. Instead, it looks up an element with the
attribute *defined* as an ID (in DTD or XML Schema--but
I don't think I've heard of browsers supporting
schemata) that equals to the value you've provided.
Basically, you'll need a DTD for the XML you're trying
to work with if you want to use getElementById().

You are right. I was able to find a discussion on this
issue at
http://www.subbu.org/weblogs/main/2006/04/ajax_and_getele.html
For the time being I have changed my xml file to have
header similar to an xhtml file and it seems to be
working.

Note that, unless I'm much mistaken, while this might work,
it is not *guaranteed* to work unless the document you're
dealing with is valid according to the doctype you've
provided. While this is obviously workable as a temporary
solution, for long-term I'd recommend writing your own
simple DTD for the documents you're working with. (Dropping
getElementById() altogether is another solution.
Implementing your own getElementById() not relying on DTD
shouldn't be much of a problem either.)
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top