Jump to content

JDOM: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Addbot (talk | contribs)
m Bot: Migrating 3 interwiki links, now provided by Wikidata on d:q359595 (Report Errors)
Addbot (talk | contribs)
m Bot: Migrating 1 interwiki links, now provided by Wikidata on d:q359595
Line 72: Line 72:


{{Compu-lang-stub}}
{{Compu-lang-stub}}

[[fr:JDOM]]

Revision as of 04:22, 14 March 2013

JDOM
Stable release
2.0.3 / September 9, 2012 (2012-09-09)
Repository
Written inJava
Operating systemCross-platform
TypeXML binding
LicenseSimilar to Apache License
Websitejdom.org
JDOM in LePUS3
JDOM factories in LePUS3

JDOM is an open source Java-based document object model for XML that was designed specifically for the Java platform so that it can take advantage of its language features. JDOM integrates with Document Object Model (DOM) and Simple API for XML (SAX), supports XPath and XSLT. It uses external parsers to build documents. JDOM was developed by Jason Hunter and Brett McLaughlin starting in March 2000. It has been part of the Java Community Process as JSR 102, though that effort has since been abandoned.

Examples

Suppose the file "foo.xml" contains this XML document:

<shop name="shop for geeks" location="Tokyo, Japan">
  <computer name="iBook" price="1200$" />
  <comic_book name="Dragon Ball vol 1" price="9$" />
  <geekyness_of_shop price="priceless" />
</shop>

One can parse the XML file into a tree of Java objects with JDOM, like so:

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new FileInputStream("foo.xml"));
Element root = doc.getRootElement();
// root.getName() is "shop"
// root.getAttributeValue("name") is "shop for geeks"
// root.getAttributeValue("location") is "Tokyo, Japan"
// root.getChildren() is a java.util.List object that contains 3 Element objects.

In case you don't want to create the document object from any file or any input stream, you can create the document object against the element.

Element root = new Element("shop"); // here <shop></shop> is the root
Document doc = new Document(root);

As a converse, one can construct a tree of elements, then generate a XML file from it, like:

Element root = new Element("shop");
root.setAttribute("name", "shop for geeks");
root.setAttribute("location", "Tokyo, Japan");
Element item1 = new Element("computer");
item1.setAttribute("name", "iBook");
item1.setAttribute("price", "1200$");
root.addContent(item1);
// do the similar for other elements
XMLOutputter outputter = new XMLOutputter();
outputter.output(new Document(root), new FileOutputStream ("foo2.xml"));