By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,767 Members | 1,098 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,767 IT Pros & Developers. It's quick & easy.

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

P: n/a
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==========
>>xml.getElementsByTagName('foo')
[<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
>>xml.getElementById("001")
===============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

Jan 31 '07 #1
Share this Question
Share on Google+
3 Replies

P: n/a
On Jan 31, 8:32 pm, "edai" <edain...@gmail.comwrote:
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.

--
Pavel Lepin

Feb 1 '07 #2

P: n/a
On Feb 1, 12:40 pm, p.le...@ctncorp.com wrote:
On Jan 31, 8:32 pm, "edai" <edain...@gmail.comwrote:
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.

--
Pavel Lepin
You are right. I was able to find a discussion on this issue at http://
http://www.subbu.org/weblogs/main/20...nd_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>

Feb 1 '07 #3

P: n/a
On Feb 1, 4:00 pm, "edai" <edain...@gmail.comwrote:
On Feb 1, 12:40 pm, p.le...@ctncorp.com wrote:
On Jan 31, 8:32 pm, "edai" <edain...@gmail.comwrote:
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/20...nd_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.)

--
Pavel Lepin

Feb 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.