XML由于其可移植性,已经成为应用开发中必不可少的环节。我们经常会把应用程序的一些配置文件(属性文件)写成XML的格式(当然,也可以用property文件而不用XML文件),应用程序通过XML的访问类来对其进行操作。对XML进行操作可以通过若干种方法,如:SAX, DOM, JDOM, JAXP等,JDOM由于其比较简单实用而被开发人员普遍使用。
本文主要分两部分,第一部分介绍如何把XML文件中的配置读入应用程序中,第二部分介绍如何使用JDOM将配置输出到XML文件中。
以下是一段XML配置文件,文件名为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>
- 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_r();
- Element title = root. getChild_r("title");
- titlename = title. getText_r();
- System.out.println("BookTitle: " + titlename);
- Element contents = root. getChild_r("contents");
- List chapters = contents. getChildren_r("chapter");
- Iterator it = chapters.iterator();
- while (it.hasNext()) {
- Element chapter = (Element) it.next();
- String chaptertitle = chapter. getAttributeValue_r("title");
- System.out.println("ChapterTitle: " + chaptertitle);
- List topics = chapter. getChildren_r("topic");
- Iterator iterator = topics.iterator();
- while (iterator.hasNext()) {
- Element topic = (Element) iterator.next();
- String topicname = topic. getText_r();
- System.out.println("TopicName: " + topicname);
- }
- }
- } catch (Exception ex) {
- }
- }
- }
然后通过 getRootElement_r()获得顶层的元素root(XML文件中book标签),继而通过 getChild_r()获取各元素,然后通过getText取得文本内容。 getChildren_r()得到的是List类型的对象,通过Iterator类对其进行迭代。程序运行的结果如下:
- 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?
下面的例子先把应用程序中要使用到的配置从一个配置文件中读入,然后输出到XML文件中。
配置文件名为:conf.properties,内容如下
- BookTitle = Java and XML
- ChapterTitle = Introduction
- TopicName = XML Matters
- 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_r(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) {
- }
- }
- }