This article focuses on how to use Jdom to manipulate XML files in an application, requiring the reader to have a basic Java language base.
Because of its portability, XML has become an essential link in the application development. We often write some of the configuration files (properties files) of an application in XML format (or, of course, using a property file instead of an XML file), and the application operates on it through the XML access class. Operations on XML can be done in several ways, such as SAX, DOM, JDOM, JAXP, and JDOM are commonly used by developers because they are simpler and more practical.
This article is divided into two parts, the first part describes how to read the configuration in the XML file into the application, and the second part describes how to use Jdom to output the configuration to the XML file.
The following is a section of the XML configuration file named Contents.xml:
<?xml version= "1.0"?>
<book>
<title>java and Xml</title>
<contents>
<chapter title= "Introduction" >
<topic>xml matters</topic>
<topic>what ' s important</topic>
<topic>the essentials</topic>
<topic>what's next?</topic>
</chapter>
<chapter title= "Nuts and Bolts" >
<topic>the basics</topic>
<topic>Constraints</topic>
<topic>Transformations</topic>
<topic>and more...</topic>
<topic>what's next?</topic>
</chapter>
</contents>
</book>
The following program accesses the Contents.xml by using the Saxbuilder class in jdom to display the individual elements on the output console with the program named: Saxbuildertest.java, which reads as follows:
------------------------------------------------------------------
Import Java.io.File;
Import Java.util.Iterator;
Import java.util.List;
Import org.jdom.Document;
Import org.jdom.Element;
Import Org.jdom.input.SAXBuilder;
public class Saxbuildertest {
private static String TitleName;
Private String Chapter;
Private String topic;
public static void Main (string[] args) {
try {
Saxbuilder builder = new Saxbuilder ();
Document document = Builder.build (new File ("Contents.xml"));
Element root = Document.getrootelement ();
Element title = Root.getchild ("title");
TitleName = Title.gettext ();
System.out.println ("BookTitle:" + titlename);
Element contents = Root.getchild ("contents");
List chapters = Contents.getchildren ("chapter");
Iterator it = Chapters.iterator ();
while (It.hasnext ()) {
Element chapter = (element) It.next ();
String chaptertitle = Chapter.getattributevalue ("title");
System.out.println ("Chaptertitle:" + chaptertitle);
List topics = Chapter.getchildren ("topic");
Iterator iterator = Topics.iterator ();
while (Iterator.hasnext ()) {
Element topic = (Element) Iterator.next ();
String topicname = Topic.gettext ();
System.out.println ("topicname:" + topicname);
}
}
catch (Exception ex) {
}
}
}
The program first initializes a Saxbuilder object and builds the document object from its build () method. Note: Jdom itself does not have a parser parser for XML files, it uses parser in JAXP for XML parsing. The top element root (the book label in the XML file) is then obtained by Getrootelement (), then the elements are obtained by Getchild (), and then the text is obtained by GetText. GetChildren () Gets the object of the list type, which is iterated by the iterator class.
The results of the program running are as follows:
Booktitle:java and XML
Chaptertitle:introduction
Topicname:xml matters
Topicname:what ' s Important
Topicname:the Essentials
Topicname:what ' s Next?
Chaptertitle:nuts and Bolts
Topicname:the Basics
Topicname:constraints
Topicname:transformations
Topicname:and More ...
Topicname:what ' s Next?
Often, we need to output the configuration data in the application to a file, and here are examples of how to use Jdom to output the configuration to an XML file.
The following example first reads the configuration to be used in the application from a configuration file, and then outputs it to an XML file.
The configuration file name is: Conf.properties, which reads as follows:
BookTitle = Java and XML
Chaptertitle = Introduction
Topicname = XML Matters
The program name is: Xmloutputtertest.java, the contents are as follows:
----------------------------------------------------------
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.util.Enumeration;
Import java.util.Properties;
Import org.jdom.Document;
Import org.jdom.Element;
Import Org.jdom.output.XMLOutputter;
public class Xmloutputtertest {
public static void Main (string[] args) {
try {
FileInputStream InputStream = new FileInputStream ("Conf.properties");
Properties prop = new properties ();
Prop.load (InputStream);
Enumeration EMU = Prop.propertynames ();
element root = new Element ("Properties");
Root.addcontent ("n");
Document doc = new document (root);
while (Emu.hasmoreelements ()) {
String propertyname = (string) emu.nextelement ();
String propertyvalue = Prop.getproperty (PropertyName);
element element = new Element (PropertyName);
Element.settext (PropertyValue);
Root.addcontent (Element);
Root.addcontent ("n");
}
Xmloutputter outputter = new Xmloutputter ();
FileOutputStream fileoutput = new FileOutputStream ("Output.xml");
Outputter.output (Doc, fileoutput);
catch (Exception ex) {
}
}
}
----------------------------------------------------------
The program reads the Conf.properties file first, obtains the enumeration iteration object through the PropertyNames (), obtains the PropertyName and obtains the PropertyValue, creates the element type root object, The document class object is created and then added to the root object through Addcontent (). Finally, the Xmloutputter class object is created and exported to the XML file through output ().