Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

XML node ID

Thread view: 
Jean-Philippe Martin - 01 Mar 2004 16:55 GMT
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
William Brogden - 01 Mar 2004 19:06 GMT
> Hi all,
>
[quoted text clipped - 8 lines]
> How can I tell it ? and does it change anything to the result of the above
> method ?

Don't work directly with the Document - instead get the "root" Element
with getDocumentElement() and work with that

Bill
Jean-Philippe Martin - 01 Mar 2004 17:21 GMT
> Don't work directly with the Document - instead get the "root" Element
> with getDocumentElement() and work with that
>
> Bill

There is no getElementById(String) with the type Element.

We can only search by tagname. And I would like to search by Id

I define some attribute as ID in my DTD.

Thx.
Jean-Philippe Martin - 02 Mar 2004 23:42 GMT
In fact, with a parsed file wich contains elements, the methods works
fine, but if we add element (with id) to the Document then the methods
stucks...

You can try it.

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);
    }
  }
}

Use it with a such file passe in argument

Code:
<?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 add a node after "Element god =
xmlDocument.getElementById("Kibo"); "
with this method. You won't be able to search for Butch with the same
method after.

Code:
static void addNode(Document doc) {
  Element elem = doc.createElement("god");
  elem.setAttribute("home", "GamerZ");
  elem.setAttribute("name", "Butch");
  ((Element)
doc.getElementsByTagName("gods").item(0)).appendChild(elem);

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.