2017年2月11日 星期六

JDOM

※xml.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>top1
    top2
    <first fa="f1" fb="1f">
        first-1
        <second1 sa="s1">aaa</second1>
        <second2>bbb</second2>
        <second3>ccc</second3>
    </first>
    top3
    <first fa="f2" fb="2f">
        first-2
        <second1 sa="s2">ddd</second1>
        <second2>eee</second2>
        <second3>fff</second3>
    </first>
    top4
    <first fa="f3" fb="3f">
        first-3
        <second1 sa="s3">ggg</second1>
        <second2>hhh</second2>
        <second3>iii</second3>
    </first>
</root>




※maven

<dependency>
    <groupId>jdom</groupId>
    <artifactId>jdom</artifactId>
    <version>1.1</version>
</dependency>




※讀寫

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
    
public class JDOMTest {
    public static void main(String[] args) throws JDOMException, IOException {
        write();
        read();
    }
    
    private static void read() throws IOException, JDOMException {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build("src/main/java/xml.xml");
    
        Element root = doc.getRootElement();
        List<Element> list = root.getChildren();
        System.out.println(list.size());
        for (Element e : list) {
            System.out.println("===== 取元素和屬性 =====");
            System.out.println(e.getText().trim());
            List<Attribute> attrs = e.getAttributes();
            for (Attribute a : attrs) {
                System.out.println(a.getValue());
            }
            System.out.println("===== 取子元素和屬性 =====");
            System.out.println(e.getChildText("second1"));
            System.out.println(e.getChild("second1").getAttribute("sa").getValue());
            System.out.println(System.getProperty("line.separator"));
        }
    }
    
    private static void write() throws IOException {
        Element root = new Element("root");
        Element first = new Element("first");
        Element second1 = new Element("second1");
        Element second2 = new Element("second2");
        Element second3 = new Element("second3");
    
        root.setText("top1" + System.getProperty("line.separator") + "top2");
        first.setText("first-1");
        second1.setText("aaa");
        second2.setText("bbb");
        second3.setText("ccc");
    
        first.setAttribute("fa", "f1");
        first.setAttribute("fb", "1f");
        second1.setAttribute(new Attribute("sa", "s1"));
    
        root.addContent(first);
        first.addContent(second1);
        first.addContent(second2);
        first.addContent(second3);
    
        Document doc = new Document(root);
    
        File file = new File("D:/" + File.separator + "xml.xml");
        OutputStream fout = new FileOutputStream(file, true);
    
        XMLOutputter out = new XMLOutputter();
        out.setFormat(out.getFormat().setEncoding("UTF-8"));
        out.output(doc, fout);
        System.out.println("XML已生成");
    }
}




沒有留言:

張貼留言