473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XML and ID node (dtd)

Hi all,

I parse a XML file to get a DOM Document.

But when I use the method Document.getElementsByID(String Id) each time I
receive null.

My XML follow a DTD. But I don't know where to tell to my parser to use this
DTD.

How can I tell it ? and does it change anything to the result of the above
method ?

thx in advance, Jean-Philippe

Jul 20 '05 #1
10 3402


Jean-Philippe Martin wrote:
I parse a XML file to get a DOM Document.

But when I use the method Document.getElementsByID(String Id) each time I
receive null.
There is no method getElementsByID in the W3C DOM. There is method
document.getElementById
My XML follow a DTD. But I don't know where to tell to my parser to use this
DTD.

How can I tell it ? and does it change anything to the result of the above
method ?


Which parser are you using? How do you use the parser, with a
programming language, for instance Java?

--

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

Jul 20 '05 #2

Jean-Philippe Martin wrote:
I parse a XML file to get a DOM Document.

But when I use the method >>Document.getElementsByID(String Id) each time I receive null.

There is no method getElementsByID in the W3C DOM. There >>is method
document.getElementById
you're right. :o)

My XML follow a DTD. But I don't know where to tell to >>my parser to use this DTD.

How can I tell it ? and does it change anything to the >>result of the above method ?


Which parser are you using? How do you use the parser, >>with a
programming language, for instance Java?


I've use the tutorial example available on java.sun.com

Here a the begining of the source code to open and parse the file with
Java.

****************************************
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try{
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(FileName));
} catch(....)

****************************************

After that point I'm able to use the "doc" variable.

Thanks in advance.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
Jean-Philippe Martin wrote:

[Document.getElementsByID(String Id)]
****************************************
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try{
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(FileName));
} catch(....)

****************************************

After that point I'm able to use the "doc" variable.


I think, you have to enable validation to successfully use getElementByID().
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)
Jul 20 '05 #4


Jean-Philippe Martin wrote:

Which parser are you using? How do you use the parser, >>with a
programming language, for instance Java?

I've use the tutorial example available on java.sun.com

Here a the begining of the source code to open and parse the file with
Java.

****************************************
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try{
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(FileName));
} catch(....)

****************************************

After that point I'm able to use the "doc" variable.


With the following example Java program

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Test20040302 {
public static void main (String[] args) {
try {
DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document xmlDocument = docBuilder.parse(args[0]);
if (xmlDocument != null) {
Element god = xmlDocument.getElementById("Kibo");
if (god != null) {
System.out.println(god.getAttribute("home"));
}
else {
System.out.println("Element not found.");
}
}
}
catch (Exception e) {
System.out.println(e);
}
}
}

when I pass in the filename of the following XML on the command line
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE gods [
<!ELEMENT gods (god)+>
<!ELEMENT god EMPTY>
<!ATTLIST god
name ID #REQUIRED
home CDATA #IMPLIED>
]>
<gods>
<god name="Kibo" home="http://www.kibo.com/" />
<god name="Xibo" />
</gods>

then getElementById("Kibo") finds the right element.
--

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

Jul 20 '05 #5
Thx I will try it at home.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #6
Your example works fine, but have you tried to add an element with a
method ?

for example this method:

static void addNode(Document doc) {
Element elem = doc.createElement("god");
elem.setAttribute("home", "AvProc");
elem.setAttribute("name", "Butch");
((Element)
doc.getElementsByTagName("gods").item(0)).appendCh ild(elem);

}

if you use it in your example, by instance after the search of "kibo"'s
home, you won't be able to find "Butch"'s home with a
getElementById("Butch").

In my application, I construct the xml file dynamically with this kind
of method.

Thx, I hope you see the problem.

Best regards Jean-Philippe.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #7
Here is a copy/paste of the Java api about this methods:

getElementById
public Element getElementById(java.lang.String elementId)
Returns the Element whose ID is given by elementId. If no such element
exists, returns null. Behavior is not defined if more than one element has
this ID.
Note: The DOM implementation must have information that says which
attributes are of type ID. Attributes with the name "ID" are not of type ID
unless so defined. Implementations that do not know whether attributes are
of type ID or not are expected to return null.

Parameters:
elementId - The unique id value for an element.
Returns:
The matching element.
Since:
DOM Level 2

How can I inform the DOM implementation ???

Thx for your help.
Jul 20 '05 #8


Jean-Philippe Martin wrote:
Your example works fine, but have you tried to add an element with a
method ?

for example this method:

static void addNode(Document doc) {
Element elem = doc.createElement("god");
elem.setAttribute("home", "AvProc");
elem.setAttribute("name", "Butch");
((Element)
doc.getElementsByTagName("gods").item(0)).appendCh ild(elem);

}

if you use it in your example, by instance after the search of "kibo"'s
home, you won't be able to find "Butch"'s home with a
getElementById("Butch").

In my application, I construct the xml file dynamically with this kind
of method.


That seems odd to me, but I can confirm that problem with Java 1.4.1_02.
I consider that a bug, I think if the implementation reads in the DTD
and getElementById finds elements parsed from a serialized XML document
then it should also find elements created with the DOM.
I googled around to find out if anyone else run into this but I haven't
found anything about that.
There are other Java DOM parsers than the one the Sun JRE comes with,
maybe XercesJ from http://xml.apache.org/ doesn't have the problem.
--

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

Jul 20 '05 #9
It's not THE answer but it's an answer :o)

Thx for your time ! You're one of the few persons who try to help me.

I will try with another parser.I will post the result next time.

Have a nice day.

Jean-Philippe.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #10


Jean-Philippe Martin wrote:
It's not THE answer but it's an answer :o)

Thx for your time ! You're one of the few persons who try to help me.

I will try with another parser.I will post the result next time.


It seems that the Java implementations somehow fail to have
getElementById work on dynamically created and inserted elements:
http://lists.w3.org/Archives/Public/...lSep/0005.html

DOM Level 3 introduces some new methods to specify an attribute is of
type id but I still think if the DTD defines that then a DOM Level 2
implementation should do.
--

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

Jul 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robert Mark Bram | last post by:
Hi All! In the code below, I am reading in an xhtml document and attempting to use selectNodes to find a <p id="rmb"> node.. But the result is: 2 - */* 0 - */p Can anyone suggest what I...
1
by: Kumar | last post by:
Hi All I have to add elements to an existing XML Document which conforms to a DTD. Is there an some API which throws an exception when I append a node to this document if it is not a valid...
5
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
6
by: SHC | last post by:
Hi all, I created an application from the Console Application (.NET) of VC++ .NET 2003, and I did "Build" the application of the attached .cpp file, volcanoes.xml and geology.dtd on my VC++ .NET...
5
by: Logos | last post by:
I'm having trouble with this in IE; annoyingly it works beautifully in FF. My keywords are not specific enough to narrow my search to useful entries on google, way too many hits, so here I am...
7
by: Arancaytar | last post by:
(Note: I am a Javascript newbie. I can handle PHP and Java, but this is unfamiliar territory.) For a wordcount feature, I need to collect the complete text content of a 'div' element inside a...
2
by: paragpdoke | last post by:
Hello Everyone. I'm new to XML and was trying to get my first DTD configured. I could not find an example of a constraint on the value of a node using the DTD. I'm trying to build a very simple...
2
by: =?iso-8859-1?q?KLEIN_St=E9phane?= | last post by:
Hi, I've a xml svg file and I would like to update it with Python. First, I would like to fetch one dom node with getElementByID. I've one issue about this method. This is my example : ...
4
by: agda.karlberg | last post by:
Hello, I need to remove the DTD reference from an xml document, the reason for this is that we want to validate against a schema instead (which we have locally). It takes up to a minute to fetch...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.