homepage Welcome to WebmasterWorld Guest from 54.226.21.57
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
firefox getElementByID does not work for XML file
edaindia

5+ Year Member



 
Msg#: 3879436 posted 5:00 pm on Mar 26, 2009 (gmt 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.

 

httpwebwitch

WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3879436 posted 2:36 am on Mar 27, 2009 (gmt 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

httpwebwitch

WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3879436 posted 2:41 am on Mar 27, 2009 (gmt 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]

edaindia

5+ Year Member



 
Msg#: 3879436 posted 4:52 pm on Apr 3, 2009 (gmt 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved