Jump to content

JDOM: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Citation bot (talk | contribs)
Added date. | Use this bot. Report bugs. | Suggested by Dominic3203 | Category:Programming language topic stubs | #UCB_Category 280/451
 
(41 intermediate revisions by 30 users not shown)
Line 1: Line 1:
{{howto|date=May 2011}}
{{Infobox Software
{{Infobox Software
| name = JDOM
| name = JDOM
| caption =
| caption =
| developer =
| developer =
| latest release version = 1.1.1
| latest release version = 2.0.6.1
| latest release date = 26.07.2009
| latest release date = {{release date|2021|12|09}}
| operating system = [[Cross-platform]]
| operating system = [[Cross-platform]]
| programming language = [[Java (programming language)|Java]]
| programming language = [[Java (programming language)|Java]]
| genre = [[XML]]
| genre = [[XML data binding|XML binding]]
| license = [[OpenSource]] (apache like)
| license = Similar to [[Apache License]]
| website = http://jdom.org
| website = {{URL|http://jdom.org}}
}}
}}


[[Image:jdom.png|thumb|right|200px|JDOM in [[Lepus3|LePUS3]]]]
'''JDOM''' is an [[open source]] [[Java (programming language)|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 [[XSL Transformations|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. The name JDOM is a [[pseudo-acronym]] for Java Document Object Model.
[[Image:Jdom-factories.png|thumb|right|300px|JDOM factories in [[Lepus3|LePUS3]]]]

'''JDOM''' is an [[open-source software|open-source]] [[Java (programming language)|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.<ref>{{Cite web |title=JDOM |url=https://mvnrepository.com/artifact/org.jdom/jdom |access-date=October 14, 2024 |website=Maven Repository}}</ref> JDOM integrates with [[Document Object Model]] (DOM) and [[Simple API for XML]] (SAX), supports [[XPath]] and [[XSL Transformations|XSLT]].<ref>{{Cite web |title=How to read XML file in Java – (JDOM Parser) |url=https://mkyong.com/java/how-to-read-xml-file-in-java-jdom-example/ |access-date=October 14, 2024 |website=Mkyong.com|date=21 December 2009 }}</ref> It uses external parsers to build documents. JDOM was developed by Jason Hunter and Brett McLaughlin starting in March 2000.<ref>{{Cite web |title=artima - A Design Review of JDOM |url=https://www.artima.com/articles/a-design-review-of-jdom |access-date=2024-10-14 |website=www.artima.com}}</ref> It has been part of the [[Java Community Process]] as JSR 102, though that effort has since been abandoned.<ref>{{Cite web |title=The Java Community Process(SM) Program - JSRs: Java Specification Requests - detail JSR# 102 |url=https://www.jcp.org/en/jsr/detail?id=102 |access-date=2024-10-14 |website=www.jcp.org}}</ref>


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


<source lang="xml">
<syntaxhighlight lang="xml">
<shop name="shop for geeks" location="Tokyo, Japan">
<shop name="shop for geeks" location="Tokyo, Japan">
<computer name="iBook" price="1200$" />
<computer name="iBook" price="1200$" />
Line 23: Line 27:
<geekyness_of_shop price="priceless" />
<geekyness_of_shop price="priceless" />
</shop>
</shop>
</syntaxhighlight>
</source>


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


<source lang="java">
<syntaxhighlight lang="java">
SAXBuilder builder = new SAXBuilder();
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new FileInputStream("foo.xml"));
Document doc = builder.build(new FileInputStream("foo.xml"));
Line 35: Line 39:
// root.getAttributeValue("location") is "Tokyo, Japan"
// root.getAttributeValue("location") is "Tokyo, Japan"
// root.getChildren() is a java.util.List object that contains 3 Element objects.
// root.getChildren() is a java.util.List object that contains 3 Element objects.
</syntaxhighlight>
</source>


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.
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.


<source lang="java">
<syntaxhighlight lang="java">
Element root = new Element("shop"); // here <shop></shop> is the root
Element root = new Element("shop"); // here <shop></shop> is the root
Document doc = new Document(root);
Document doc = new Document(root); // create a new document with the supplied element as the root
</syntaxhighlight>
</source>


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


<source lang="java">
<syntaxhighlight lang="java">
Element root = new Element("shop");
Element root = new Element("shop");
root.setAttribute("name", "shop for geeks");
root.setAttribute("name", "shop for geeks");
Line 54: Line 58:
item1.setAttribute("price", "1200$");
item1.setAttribute("price", "1200$");
root.addContent(item1);
root.addContent(item1);
// do the similar for other elements
// perform similar steps for other elements
XMLOutputter outputter = new XMLOutputter();
XMLOutputter outputter = new XMLOutputter();
outputter.output(new Document(root), new FileOutputStream ("foo2.xml"));
outputter.output(new Document(root), new FileOutputStream ("foo2.xml"));
</syntaxhighlight>
</source>


==References==
asd
{{reflist}}


== External links ==
== External links ==
{{Official Website}}


[[Category:Java (programming language) libraries]]
* [http://www.jdom.org/ JDOM home page]
* [http://www-128.ibm.com/developerworks/java/library/j-jdom/ Simplify XML Programming with JDOM]

[[Category:Java platform|Document Object Model]]
[[Category:Java specification requests|Document Object Model]]
[[Category:Java specification requests|Document Object Model]]
[[Category:XML-based standards]]
[[Category:XML-based standards]]


{{Compu-lang-stub}}


{{Compu-lang-stub}}
[[ru:JDOM]]
[[de:JDOM]]
[[es:JDOM]]

Latest revision as of 13:17, 29 November 2024

JDOM
Stable release
2.0.6.1 / December 9, 2021 (2021-12-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.[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

[edit]

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

[edit]
  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.
[edit]

Official website Edit this at Wikidata