Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML and ID node (dtd)

Reply
Thread Tools

XML and ID node (dtd)

 
 
Jean-Philippe Martin
Guest
Posts: n/a
 
      03-01-2004
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



 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-02-2004


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/

 
Reply With Quote
 
 
 
 
Jean-Philippe Martin
Guest
Posts: n/a
 
      03-02-2004


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

>>>
>>> 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!
 
Reply With Quote
 
Johannes Koch
Guest
Posts: n/a
 
      03-02-2004
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.)
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      03-02-2004


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/

 
Reply With Quote
 
Jean-Philippe Martin
Guest
Posts: n/a
 
      03-02-2004
Thx I will try it at home.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Jean-Philippe Martin
Guest
Posts: n/a
 
      03-02-2004
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!
 
Reply With Quote
 
Jean-Philippe Martin
Guest
Posts: n/a
 
      03-03-2004
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.


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      03-03-2004


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/

 
Reply With Quote
 
Jean-Philippe Martin
Guest
Posts: n/a
 
      03-03-2004
It's not THE answer but it's an answer )

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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Reading XML Node-by-Node Martin Honnen ASP .Net 0 11-12-2008 06:29 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
how to creating new node inside a xml node Geagleeye ASP General 2 08-03-2006 12:09 PM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
how to create a node set of elements through C++ code while executing a style sheet and process the created node set. pvssvikas@gmail.com XML 0 01-25-2006 12:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57