XML and ID node (dtd)

  • Thread starter Jean-Philippe Martin
  • Start date
J

Jean-Philippe Martin

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
 
M

Martin Honnen

Jean-Philippe Martin said:
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?
 
J

Jean-Philippe Martin

you're right. :eek:)

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.
 
J

Johannes Koch

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().
 
M

Martin Honnen

Jean-Philippe Martin wrote:

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.
 
J

Jean-Philippe Martin

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)).appendChild(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.
 
J

Jean-Philippe Martin

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.
 
M

Martin Honnen

Jean-Philippe Martin said:
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)).appendChild(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.
 
J

Jean-Philippe Martin

It's not THE answer but it's an answer :eek:)

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.
 
M

Martin Honnen

Jean-Philippe Martin said:
It's not THE answer but it's an answer :eek:)

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/www-dom/2002JulSep/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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top