JDOM: [www.jdom.org]
JDOM is a full featured Java API for the SAX and DOM accessing. Collections are used heavily for the results and queries to make Java programmer life easier. The SAX and DOM parsers would be the underlaying default parsers. i.e. JAXP is checked if it exists then the Apache parser then finally the hard coded internal parser. It also provides adapters to many other parsers like Oracle Parser, IBM Parser and Apache Xerces DOM.
Main Classes [JDOM Java Docs] :
SAXBuilder : Builds a JDOM document from files, streams, readers, URLs, or a SAX InputSource
instance using a SAX parser. The builder uses a third-party SAX parser (chosen by JAXP by default, or you can choose manually) to handle the parsing duties and simply listens to the SAX events to construct a document.
SAXHandler : This will create a new SAXHandler
that listens to SAX events and creates a JDOM Document. The objects will be constructed using the default factory.
SAXOutputter : Outputs a JDOM document as a stream of SAX2 events.
DOMBuilder : Builds a JDOM org.jdom.Document
from a pre-existing DOM org.w3c.dom.Document
. Also handy for testing builds from files to sanity check SAXBuilder
.
DOMOutputter : Outputs a JDOM org.jdom.Document
as a DOM org.w3c.dom.Document
.
XSLTransformer : A convenience class to handle simple transformations. The JAXP TrAX classes have more bells and whistles and can be used with JDOMSource and JDOMResult for advanced uses. This class handles the common case and presents a simple interface. XSLTransformer is thread safe and may be used from multiple threads.
XSLTransformer transformer = new XSLTransformer(“file.xsl”);
Document x2 = transformer.transform(x); // x is a Document
Document y2 = transformer.transform(y); // y is a Document
JDOM relies on TrAX to perform the transformation. The “javax.xml.transform.TransformerFactory” Java system property determines which XSLT engine TrAX uses. Its value should be the fully qualified name of the implementation of the abstract javax.xml.transform.TransformerFactory class. Values of this property for popular XSLT processors include:
* Saxon 6.x: com.icl.saxon.TransformerFactoryImpl
* Saxon 7.x: net.sf.saxon.TransformerFactoryImpl
* Xalan: org.apache.xalan.processor.TransformerFactoryImpl
JDOMSource : A holder for an XML Transformation source: a Document, Element, or list of nodes.
public static List transform(Document doc, String stylesheet) throws JDOMException {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(stylesheet));
JDOMSource in = new JDOMSource(doc);
JDOMResult out = new JDOMResult();
transformer.transform(in, out);
return out.getResult();
}
catch (TransformerException e) {
throw new JDOMException(“XSLT Transformation failed”, e);
}
}
JDOMResult : A holder for an XSL Transformation result, generally a list of nodes although it can be a JDOM Document also. As stated by the XSLT 1.0 specification, the result tree generated by an XSL transformation is not required to be a well-formed XML document. The result tree may have “any sequence of nodes as children that would be possible for an element node”.
Sample programs :
All the examples uses the sample file “plugin.xml” in “c:\” directory.
#1 Create a document and output it via the XMLOutputter class.
package com.suresh.xml.jdom; import java.io.File; import java.io.IOException; import org.jdom.Document; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; public class TestJDOMOutputter { public static void main(String[] args) { try { // This SAXBuilder looks for the default SAXParsers, parses and builds the XML. // The default behavior is to (1) use the saxDriverClass, if it has been // set, (2) try to obtain a parser from JAXP, if it is available, and // (3) if all else fails, use a hard-coded default parser (currently // the Xerces parser). // SaxBuilder -> JAXPParserFactory -> SAXParserFactory -> // com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl or // org.apache.xerces.jaxp.SAXParserFactoryImpl SAXBuilder builder = new SAXBuilder(); Document document = builder.build(new File("c:\\plugin.xml")); XMLOutputter outputter = new XMLOutputter(); outputter.output(document, System.out); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
#2 Traverse the entire tree and print the node statistics
package com.suresh.xml.jdom; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.jdom.Comment; import org.jdom.DocType; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.ProcessingInstruction; import org.jdom.Text; import org.jdom.input.SAXBuilder; public class TestJDOMTraverseTree { public static void main(String[] args) { SAXBuilder builder = new SAXBuilder(); try { Document doc = builder.build(new File("c:\\plugin.xml")); traverseXMLTree(doc.getContent()); Element rootElement = doc.getRootElement(); String baseURI = doc.getBaseURI(); DocType docType = doc.getDocType(); // this gives the file path "file:/c:/plugin.xml" System.out.println("base URI : " + baseURI); // this is "plugin" element System.out.println("RootElement : " + rootElement.getName()); // if it has a doctype then get the info if (docType != null) { System.out.println("DocType : " + docType.getElementName() + " : " + docType.getPublicID() + " : " + docType.getSystemID() + " : " + docType.getValue()); } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static void traverseXMLTree(List<Object> contentList) { Iterator<Object> contentIter = contentList.iterator(); while (contentIter.hasNext()) { Object obj = contentIter.next(); if (obj instanceof Element) { Element element = (Element) obj; System.out.println("Element Name[" + element.getContentSize() + "] : " + element.getName()); traverseXMLTree(element.getContent()); } else if (obj instanceof ProcessingInstruction) { ProcessingInstruction pi = (ProcessingInstruction) obj; System.out.println("PI as seen in Doc : <?" + pi.getTarget() + " " + pi.getData() + "?>"); } else if (obj instanceof Text) { Text text = (Text) obj; if (text != null && text.getText() != null && text.getTextTrim().length() > 0) { System.out.println("Text : " + text.getTextTrim()); } } else if (obj instanceof Comment) { Comment comment = (Comment) obj; System.out.println("Comment : " + comment.getValue()); } } } }
Example plugin.xml file used in the above examples :
<?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.0"?> <plugin id="com.example.eclipseTools" name="Tools Plug-in" version="1.0.0" provider-name="Example" class="com.example.eclipse.EclipseToolsPlugin"> <runtime> <library name="tools/tools-2.2.0-SNAPSHOT.jar"> <export name="*"/> </library> <library name="tools/lib/velocity-1.4.jar"> <export name="*"/> </library> <library name="shared/shared-2.2.0-SNAPSHOT.jar"> <export name="*"/> </library> <library name="shared/lib/c3p0-0.9.0.4.jar"> <export name="*"/> </library> <library name="shared/lib/cglib-2.1.3.jar"> <export name="*"/> </library> <library name="shared/lib/commons-beanutils-1.6.1.jar"> <export name="*"/> </library> <library name="shared/lib/commons-collections-2.1.1.jar"> <export name="*"/> </library> <library name="shared/lib/commons-logging-1.0.4.jar"> <export name="*"/> </library> <library name="shared/lib/dom4j-1.6.1.jar"> <export name="*"/> </library> <library name="shared/lib/ehcache-1.1.jar"> <export name="*"/> </library> <library name="shared/lib/hibernate-3.1.3.jar"> <export name="*"/> </library> <library name="shared/lib/junit-3.8.1.jar"> <export name="*"/> </library> <library name="shared/lib/log4j-1.2.11.jar"> <export name="*"/> </library> <library name="shared/lib/xstream-1.2.1.jar"> <export name="*"/> </library> <library name="jta.jar"> <export name="*"/> </library> <library name="shared/lib/asm-1.5.3.jar"> <export name="*"/> </library> <library name="shared/lib/asm-attrs-1.5.3.jar"> <export name="*"/> </library> <library name="eclipseTools.jar"> <export name="*"/> </library> <library name="tools/lib/qdox-1.6.jar"> <export name="*"/> </library> <library name="shared/lib/antlr-2.7.6.jar"> <export name="*"/> </library> <library name="shared/lib/commons-lang-2.2.jar"> <export name="*"/> </library> <library name="shared/lib/jaxen-1.1.1.jar"> <export name="*"/> </library> <library name="shared/lib/ojdbc14-10.2.0.3.jar"> <export name="*"/> </library> </runtime> <requires> <import plugin="org.eclipse.ui"/> <import plugin="org.eclipse.core.resources"/> <import plugin="org.eclipse.core.runtime"/> <import plugin="org.eclipse.jdt.ui"/> <import plugin="org.eclipse.jdt.core"/> </requires> <extension point="org.eclipse.ui.preferencePages"> <page class="com.example.eclipse.preferences.SPLPreferencePage" name="Example Preferences" id="com.example"/> <page category="com.example" class="com.example.eclipse.database.preferences.PreferencePage" id="com.example.eclipseTools.database.preferences.PreferencePage" name="Database Connection Preferences"/> </extension> <extension point="org.eclipse.ui.propertyPages"> <page class="com.example.eclipse.properties.ProjectPropertyPage" id="com.example.eclipse.properties.ProjectPropertyPage" name="Example Database Properties" objectClass="org.eclipse.jdt.core.IJavaProject"/> </extension> <!-- This is a Comment --> </plugin>
thank’s for this reference. because, I am must finishing my studies. THANK’S
Have you looked at vtd-xml? it is a lot faster and memory efficient than DOM4J and JDOM