返回顶部

收藏

JDOM: Properties To XML

更多

From the book: Hardcore JavaTitle: Java and XMLThird Edition: December 2006ISBN 10: 0-596-10149-XISBN 13: 9780596101497http://www.oreilly.com/

PropsToXML.java

package javaxml3;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class PropsToXML {

    /**
     * Provide a static entry point for running.
     */
    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Usage: java javaxml3.PropsToXML "
                    + "[properties file] [XML file for output]");
            System.exit(0);
        }

        try {
            PropsToXML propsToXML = new PropsToXML();
            propsToXML.convert(args[0], args[1]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * This will take the supplied properties file, and convert that file to an
     * XML representation, which is then output to the supplied XML do*****ent
     * filename.
     * 
     * @param propertiesFilename
     *            file to read in as Java properties.
     * @param xmlFilename
     *            file to output XML representation to.
     * @throws <code>IOException</code> - when errors occur.
     */
    public void convert(String propertiesFilename, String xmlFilename)
            throws IOException {

        // Get Java Properties object
        FileInputStream input = new FileInputStream(propertiesFilename);
        Properties props = new Properties();
        props.load(input);

        // Convert to XML
        convertToXML(props, xmlFilename);
    }

    /**
     * This will handle the detail of conversion from a Java
     * <code>Properties</code> object to an XML do*****ent.
     * 
     * @param props
     *            <code>Properties</code> object to use as input.
     * @param xmlFilename
     *            file to output XML to.
     * @throws <code>IOException</code> - when errors occur.
     */
    private void convertToXML(Properties props, String xmlFilename)
            throws IOException {

        // Create a new JDOM Document with a root element "properties"
        Element root = new Element("properties");
        Document doc = new Document(root);

        // Get the property names
        Enumeration propertyNames = props.propertyNames();
        while (propertyNames.hasMoreElements()) {
            String propertyName = (String) propertyNames.nextElement();
            String propertyValue = props.getProperty(propertyName);
            createXMLRepresentation(root, propertyName, propertyValue);
        }

        // Output document to supplied filename
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        FileOutputStream output = new FileOutputStream(xmlFilename);
        outputter.output(doc, output);

    }

    /**
     * This will convert a single property and its value to an XML element and
     * textual value.
     * 
     * @param root
     *            JDOM root <code>Element</code> to add children to.
     * @param propertyName
     *            name to base element creation on.
     * @param propertyValue
     *            value to use for property.
     */
    private void createXMLRepresentation(Element root, String propertyName,
            String propertyValue) {

        int split;
        String name = propertyName;
        Element current = root;
        Element test = null;

        while ((split = name.indexOf(".")) != -1) {
            String subName = name.substring(0, split);
            name = name.substring(split+1);

            // Check for existing element 
            if ((test = current.getChild(subName)) == null) {
                Element subElement = new Element(subName);
                current.addContent(subElement);
                current = subElement;
            } else {
                current = test;
            }
        }

        // When out of loop, what's left is the final element's name
        Element last = new Element(name); 
        last.setText(propertyValue);
        //Attribute attribute = new Attribute("value", propertyValue);
        //last.setAttribute(attribute);
        current.addContent(last);

    }

}

标签:jQuery,XML,JDOM

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. Hiro 发表 2009-09-21 03:33:02 XPath:语法及示例,前端学习收集
  2. 歪脖骇客 发表 2016-05-05 03:53:37 使用jQuery解析XML
  3. Hiro 发表 2009-07-10 03:52:23 jQuery:书籍展示效果
  4. 博主 发表 2011-04-16 00:00:00 jQuery上下浮动滚动[4-22更新]
  5. 博主 发表 2013-06-01 09:27:00 XML文件比较工具
  6. 博主 发表 2013-08-24 00:00:00 jQuery原理窥探
  7. iForget, i write 发表 2014-10-21 03:31:18 jquery $.ajax() 中 datatype 为 json 时的问题
  8. JiaxiangZheng 发表 2014-12-07 16:00:00 Promise规范及jQuery Deferred实现
  9. www.tonitech.com的站长 发表 2014-07-25 07:19:16 ECSHOP中transport.js和jquery冲突的解决方法
  10. 博主 发表 2014-11-26 00:00:00 jQuery 取得 iframe 中元素的几种方法(转载)
  11. chirs <> 发表 2008-05-09 09:36:41 Javascript与CSS在IE和Firefox中的误区及区别
  12. 博主 发表 2015-04-02 00:00:00 How to use jQuery mobile?

发表评论