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.