Title: JDOM
1JDOM
-
- JDOM itself is not a parser
- it is a wrapper,
- and requires the presence of an underlying parser
.
2Installing
- http//www.jdom.org
- Place all of my Java tools under
c\java\j2sdkee1.3 - Set JAVA_HOMEC\java\jdk1.3
- Run C\java\jdom-1.0gtbuild
3Batch file
- set JAVA_HOMEc\programfiles\java\jdk1.5.0_05
- set JDOM_HOMEc\program files\java\jdk1.5.0_05\jd
om-1.0 - set CLASSPATHCLASSPATHJDOM_HOME\build\classe
sJAVA_HOME\lib\tools.jarJDOM_HOME\lib\xerces
.jarJDOM_HOME\lib\jaxp.jar
4Package Structure
- The package structure is as follows-
- org.jdom has the following packages
- org.jdom.input.
- org.jdom.output.
- org.jdom.adapters.
- org.jdom.transform.
5Parsing a XML document using JDOM
- At the start of your code, include the following
packages- - org.jdom.Document.
- org.jdom.Element.
- org.jdom.input.SAXBuilder or DOMBuilder.
- org.jdom.JDOMException.
6Parsing a XML document using JDOM
- After importing the packages mentioned above.
- A Document object is required to parse a XML
document. - A document object is an object representation of
the XML document you would want to parse. - Document objects can be obtained either from
scratch or from an existing XML document.
7Parsing a XML document using JDOM
- A Document object may be constructed from scratch
in the following manner. - If you already have a XML file , then you can do
the following.
8Parsing a XML document using JDOM
- Using the document Object obtained from the
previous slide , get the root element of the
document. - Getting the root element using the document
object can be done in the following manner. - Element rootelementdoc.getRootElement()
9Parsing a XML document using JDOM
- Once the root element is obtained, it is very
easy to traverse the XML document. - Getting the children of the root element can be
done using the following manner- - List childrenroot.getChildren()
- The List used above is nothing but the List class
that belongs to Java's util package. - Now one can iterate through the List that is
obtained above and get individual child elements
out.
10Parsing a XML document using JDOM
- If one knows the name of the individual child
elements, then the method to be used is- - Element.getName(). This method will return a
String that will represent the name of the child
element. - Once you have the element that you want, you can
retrieve the value within the tag of the element
by using the following method- - Element.getValue()
11Parsing a XML document using JDOM
- There is also a way of combining the methods
while checking for a particular element. - For example-
- Suppose you have a retrieved an element name
using the method Element.getName(), and you want
to check if the name equals comment.Then you
would form your statement is the following
manner- - Element.getName().equals(comment)
12Parsing a XML document using JDOM
- To retrieve attributes within an element tag, one
can use the following method- - Element.getAttributeValue(name of the
attribute) - One can also retrieve the list of attributes
within an element tag by using the method- - Element.getAttributes()
- This method will then return a List of
attributes, within that element.
13Parsing a XML document using JDOM
- An element can also have text content. The text
content can be obtained by using the following
method- - Element.getText()
- This method returns a String object , which
represents the text content of the element.
14 Parsing a XML document using JDOM
- The text content can be changed by using the
following method- - Element.setText()
- Parent of an element can be obtained by using
method. - Element.getParent()
- The above method will return the parent element
of a particular element.
15Example1.java
- import org.jdom.
- public class example1
-
- public static void main(String args)
-
- Element root new Element("myRootElement")
- DocType dt new DocType("myRootElement")
- Document doc new Document(root, dt)
- root.setText("This is a root element")
- System.out.println(doc)
-
-
16Adding Elements, Attributes, and Character Data
- import org.jdom.
- public class example2
-
- public static void main(String args)
-
- Element root new Element("myRootElement")
- DocType dt new DocType("myRootElement")
- Document doc new Document(root, dt)
- root.setText("This is a root element")
- Element el new Element("el")
- el.setText("This is el!")
- root.addContent(el)
- System.out.println(doc)
-
-
17Emitting the JDOM XML
- use the JDOM XMLOutputter object
- XMLOutputter outputternew XMLOutputter(
)outputter.output(doc, system.out) - XMLOutputter(Format format)
18Example2b.java
- import org.jdom.
- import org.jdom.output.XMLOutputter
- public class example2a
-
- public static void main(String args)
-
- Element root new Element("myRootElement")
- DocType dt new DocType("myRootElement")
- Document doc new Document(root, dt)
- root.setText("This is a root element")
- Element el new Element("el")
- el.setText("This is el!")
- root.addContent(el)
- try
- XMLOutputter outputter new XMLOutputter(
) - outputter.output(doc, System.out)
-
- catch (java.io.IOException e)
19Format
- getCompactFormat() Â Â Â Â Â Â Â Â Â Â Returns a new
Format object that performs whitespace
normalization, uses the UTF-8 encoding, doesn't
expand empty elements, includes the declaration
and encoding, and uses the default entity escape
strategy. - See word file
20Attributes
- Create a new Attribute instance by calling the
Attribute constructor, - passing in the name of the attribute and the
attribute value. - Then add the Attribute to an Element by calling
setAttribute.
21The JDOM Namespace Class
- Create a Namespace object
- Namespace ns Namespace.getNamespace("james",
"www.jamesbritt.com") - Use the Namespace to cerate a new Element
- Element el3 new Element("el3", ns)
- Now use that same Namespace object to supply a
URI, for another Element - String nsURI ns.getURI()
- Element el4 new Element("el4", nsURI)
22Adding other nodes types
- Creating a PI, or processing instruction, as
quite like creating an attribute - ProcessingInstruction pi new
ProcessingInstruction("target", someData) - doc.addContent(pi)
- Comment
- Comment com new Comment("Docs need more
comments!") - doc.addContent(com)
23Saving your JDOM (ex3a)
- Outputter.output() takes two parameters the name
of the file to write to, and a JDOM Document
object. - It creates an output stream using the file name
to write the XML to the file.
24Getting the Document as a String (ex4)
- Using saxbuilder to parse XML doc
- XMLOutputter op new XMLOutputter(format)
- String myXml op.outputString(doc)