通過JDOM實現XML與String的相互轉換

tao先生發表於2015-12-21

利用JDOM實現XML與String之間的相互轉換:

package com.util.xml;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 * 通過JDOM解析XML檔案 將字串格式的XML檔案轉換為XML的document
 * 
 */

// 將String轉換成XML
public class XMLUtils {

    public static Document str2XML(String string) throws Exception {

        SAXBuilder buider = new SAXBuilder();

        Document document = buider.build(new StringReader(string));

        return document;

    }
    
    //將XML轉換成String輸出
    public static String xml2Str(String file) throws Exception {

        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(new FileInputStream(new File(file)));

        Format format = Format.getCompactFormat();
        format.setEncoding("UTF-8");// 設定xml檔案的字元為UTF-8,解決中文問題
        XMLOutputter xmlout = new XMLOutputter();
        
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        xmlout.output(document, bo);
        return bo.toString().trim();
        
    }

    // 將XML以鍵值對的形式輸出,對於有兩層以上的XML檔案
    public static Map<String, String> paraseXML(String file) throws Exception {

        Map<String, String> map = new HashMap<String, String>();

        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(new File(file));

        Element root = document.getRootElement();

        List<Element> list = root.getChildren();
        for (Iterator<Element> iterator = list.iterator(); iterator.hasNext();) {

            Element firstChild = iterator.next();
            List<Element> secondElements = firstChild.getChildren();

            for (Iterator<Element> iterator2 = secondElements.iterator(); iterator2
                    .hasNext();) {

                Element secondElement = iterator2.next();
                map.put(secondElement.getName(), secondElement.getText());
            }

        }

        return map;
    }
    
    
    public static void main(String[] args) throws Exception {

        String str = "<students><Student><name>zhangsan</name><age>18</age><name>lisi</name><age>28</age></Student></students>";
        Document document = str2XML(str);
        Element root = document.getRootElement();
        System.out.println(root.getName());
        System.out.println(root.getChildText("name"));

        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()
                .setIndent("     "));
        FileOutputStream fos = new FileOutputStream(new File(
                "date\\string2xml.xml"));
        outputter.output(document, fos);
        fos.close();
        
        String xmlContent = xml2Str("date\\string2xml.xml");
        System.out.println("XML的內容為: "+ "\n" + xmlContent);
        
        Map<String, String> xmlMap = new HashMap<String, String>();
        xmlMap = paraseXML("date\\string2xml.xml");
        Set<String> keysSet = xmlMap.keySet();
        for(String key: keysSet){
            
            String value = xmlMap.get(key);
            System.out.println(key + " = " + value);
        }
        

    }

}

相關文章