JDOM

JDOM
Stable release
2.0.6.1 / December 9, 2021 (2021-12-09)
Written inJava
Operating systemCross-platform
TypeXML binding
LicenseSimilar to Apache License
Websitejdom.org
Repository
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.[1] JDOM integrates with Document Object Model (DOM) and Simple API for XML (SAX), supports XPath and XSLT.[2] It uses external parsers to build documents. JDOM was developed by Jason Hunter and Brett McLaughlin starting in March 2000.[3] It has been part of the Java Community Process as JSR 102, though that effort has since been abandoned.[4]

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 do not 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);  // create a new document with the supplied element as the root

As a converse, one can construct a tree of elements, then generate an XML file from it, as in the following example:

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);
// perform similar steps for other elements
XMLOutputter outputter = new XMLOutputter();
outputter.output(new Document(root), new FileOutputStream ("foo2.xml"));

References

  1. ^ "JDOM". Maven Repository. Retrieved October 14, 2024.
  2. ^ "How to read XML file in Java – (JDOM Parser)". Mkyong.com. 21 December 2009. Retrieved October 14, 2024.
  3. ^ "artima - A Design Review of JDOM". www.artima.com. Retrieved 2024-10-14.
  4. ^ "The Java Community Process(SM) Program - JSRs: Java Specification Requests - detail JSR# 102". www.jcp.org. Retrieved 2024-10-14.

Official website Edit this at Wikidata


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.