JDom’s mission and some examples

Posted on June 26, 2008. Filed under: Java, Linux, Programming |

This is a jdom mission from www.jdom.org

There is no compelling reason for a Java API to manipulate XML to be complex, tricky, unintuitive, or a pain in the neck. JDOMTM is both Java-centric and Java-optimized. It behaves like Java, it uses Java collections, it is completely natural API for current Java developers, and it provides a low-cost entry point for using XML.

While JDOM interoperates well with existing standards such as the Simple API for XML (SAX) and the Document Object Model (DOM), it is not an abstraction layer or enhancement to those APIs. Rather, it provides a robust, light-weight means of reading and writing XML data without the complex and memory-consumptive options that current API offerings provide.

Following are some examples of howto use jdom in your java programs.

· Parsing with JDOM

import java.io.FileInputStream;
import
java.io.IOException;
import
java.io.FileNotFoundException;
import
org.jdom.Document;
import
org.jdom.JDOMException;
import
org.jdom.adapters.XercesDOMAdapter;
import
org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;

public class JDOMCreateExample {

private static DOMBuilder builder = null;

public static void main(String args[]) throws IOException, FileNotFoundException {

XercesDOMAdapter xercAdapter = new XercesDOMAdapter();
org.w3c.dom.Document w3Dom = xercAdapter.getDocument(
new FileInputStream(“games.xml”), false);

builder = new DOMBuilder(“org.jdom.adapters.XercesDOMAdapter”);

Document doc = builder.build(w3Dom);
}
}

· Make up and write an XML document, using JDOM

import org.jdom.Document;
import
org.jdom.Element;
import org.jdom.output.XMLOutputter;

/** Make up and write an XML document, using JDOM
* @author Ian Darwin, http://www.darwinsys.com/
* @version $Id: DocWriteJDOM.java,v 1.4 2004/02/09 03:34:10 ian Exp $
*/
public class DocWriteJDOM {

public static void main(String[] av) throws Exception {
DocWriteJDOM dw =
new
DocWriteJDOM();
Document doc = dw.makeDoc();
// Create an output formatter, and have it write the doc.

new XMLOutputter().output(doc, System.out);
}

/** Generate the XML document */
protected Document makeDoc() throws Exception {
Document doc =
new Document(new Element(“Poem”
));
doc.getRootElement().
addContent(
new Element(“Stanza”
).
addContent(
new Element(“Line”
).
setText(
“Once, upon a midnight dreary”
)).
addContent(
new Element(“Line”
).
setText(
“While I pondered, weak and weary”)));

return doc;
}
}

// demo xml file
/*
<?xml version=”1.0″?>
<people>
<person>
<name>Ian Darwin</name>
<email>http://www.darwinsys.com/</email&gt;
<country>Canada</country>
</person>
<person>
<name>Another Darwin</name>
<email type=”intranet”>afd@node1</email>
<country>Canada</country>
</person>
</people>
*/

· Accessing Attributes Using JDOM

import java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import
org.jdom.Element;
import
org.jdom.adapters.XercesDOMAdapter;
import org.jdom.input.DOMBuilder;

public class JDOMCreateExample {

private static DOMBuilder builder = null;

public static void main(String args[]) throws Exception, FileNotFoundException {

XercesDOMAdapter xercAdapter = new XercesDOMAdapter();
org.w3c.dom.Document w3Dom = xercAdapter.getDocument(
new FileInputStream(“games.xml”), false);

builder = new DOMBuilder(“org.jdom.adapters.XercesDOMAdapter”);

Document doc = builder.build(w3Dom);

List childs = doc.getRootElement().getChildren(“game”);

Iterator itr = childs.iterator();
while
(itr.hasNext()) {
Element child = (Element) itr.next();
System.out.println(child.getName() +
“ = ”
+ child.getText());
System.out.println(child.getAttributeValue(
“genre”
));
}
}
}

· Adding an Element Using JDOM

import org.jdom.Document;
import org.jdom.Element;

public class MainClass {
public static void main(String args[]) {

Document doc = new Document(new Element(“games”));

Element newGame = new Element(“game”).setText(“Final Fantasy”);

doc.getRootElement().addContent(newGame);
}
}

· XPath with JDOM

import java.io.File;
import
java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom.Document;
import
org.jdom.Element;
import
org.jdom.Namespace;
import
org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class MainClass {
public static void main(String[] args) throws
Exception {
Namespace NS_GUEST = Namespace.getNamespace(
“g”, “uri:c:guest”
);
Namespace NS_SCHEDULE = Namespace.getNamespace(
“s”, “uri:c:schedule”);

SAXBuilder builder = new SAXBuilder();
Document document = builder.build(
new File(“tds_ns.xml”));

XPath xPath = XPath.newInstance(“/s:schedule/s:show[@date=$date]/g:guest”);
xPath.addNamespace(NS_SCHEDULE);
xPath.addNamespace(NS_GUEST);

String formattedDate = new SimpleDateFormat(“MM.dd.yy”).format(new Date(2006, 5, 14));
xPath.setVariable(
“date”
, formattedDate);
Element guest = (Element) xPath.selectSingleNode(document);

System.out.println(guest.getChildText(“name”, NS_GUEST));
}
}

 

 

 

Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

2 Responses to “JDom’s mission and some examples”

RSS Feed for John's Blog Comments RSS Feed

Original post by Dmitri Gromov

Have you looked at vtd-xml? it is a lot faster and memory efficient than DOM4J and JDOM

vtd-xml


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: