Welcome to WebmasterWorld Guest from 54.145.94.234

Forum Moderators: open

Message Too Old, No Replies

firefox getElementByID does not work for XML file

     
5:00 pm on Mar 26, 2009 (gmt 0)

New User

10+ Year Member

joined:Apr 3, 2006
posts: 24
votes: 0


I am trying to parse and XML file but am finding getElementByID returning Null. Sample code is as below.
------------------XML File t.xml ----------------
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE opt SYSTEM "xyz.dtd">
<opt id="opt0">
<tasks id="tsk0">
<comments>created</comments>
<context>,</context>
<dueBy>1231619104</dueBy>
<logTime>1231619104</logTime>
<person>me</person>
<status>open</status>
<task>me</task>
</tasks>
<tasks id="tsk1">
<comments>created</comments>
<context>@</context>
<dueBy>1231619048</dueBy>
<logTime>1231619048</logTime>
<person>me</person>
<status>open</status>
<task>TODO check with </task>
</tasks>
</opt>
----------------------------------------------

------------------ DTD Decleration xyz.dtd -----------
<!ELEMENT comments ( #PCDATA ) >

<!ELEMENT context ( #PCDATA ) >

<!ELEMENT dueBy ( #PCDATA ) >

<!ELEMENT logTime ( #PCDATA ) >

<!ELEMENT opt ( tasks+ ) >
<!ATTLIST opt id ID #REQUIRED >

<!ELEMENT person ( #PCDATA ) >

<!ELEMENT status ( #PCDATA ) >

<!ELEMENT task ( #PCDATA ) >

<!ELEMENT tasks ( comments, context, dueBy, logTime, person, status, task ) >
<!ATTLIST tasks id ID #REQUIRED >

-------------------------------------------
----------------------HTML File t.html ----------
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="x.js" ></script>
</head>
<body onload="onInit()">
<div id="content"></div>
</body>
</html>

------------------ Javascript x.js -------------
function loadFile(filename){//{{{

xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.load(filename);
return xmlDoc;
}//}}}
function onInit(){//{{{

ddiv=document.getElementById("content");
xml=loadFile("./t.xml");
var task= xml.getElementById("tsk1");
}
---------------------------------------------

in the above sample code the line task= xml.getElementById("tsk1"); returns null. On browsing the xml tree in firefox-firebug I find the isId property of the id="tsk1" attribute is set to false....

Requesting a second set of eyes to help me spot the issue.

2:36 am on Mar 27, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


The problem you're having may be caused by an omission in the DTD. I'll need to check the manual...

to start, search for this problem in the Mozilla docs and see if anything emerges

2:41 am on Mar 27, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0



Defining an attribute as being of type ID
We can use DTD to define an attribute as being of type ID, so that getElementById will work. The following example defines a partial doctype for RSS so that the id attribute of an <item> element is of type ID:

<!DOCTYPE rss SYSTEM "-//CAKE//DTD RSS Test//EN"
[
<!ATTLIST item id ID #IMPLIED>
]>This approach works in Opera and Firefox, but not Safari or Internet Explorer.


source [reference.sitepoint.com]
4:52 pm on Apr 3, 2009 (gmt 0)

New User

10+ Year Member

joined:Apr 3, 2006
posts:24
votes: 0


I had already defined ID in the DTD (see above)
<!ATTLIST tasks id ID #REQUIRED >
I tried changing it to
<!ATTLIST tasks id ID #IMPLIED >
That too did not work....
I finally wrote a function to loop over all the tag elements and return the element with the matching value for the node with the name Id