2019/04/16

Java JDOM2 Read / Write

XML如下:
<?xml version="1.0" encoding="UTF-8"?>
<list>
 <Student><name>王小明</name><en>50</en><ch>90</ch><math>80</math></Student>
<Student><name>陳小東</name><en>67</en><ch>70</ch><math>90</math></Student></list>


程式碼如下:
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

    static class Student{
        private String name;
        private int ch;
        private int en;
        private int math;

        public Student(String name, int ch, int en, int math){
            this.name=name;
            this.ch=ch;
            this.en=en;
            this.math=math;
        }
    }

    public static void main(String []args) throws JDOMException, IOException {
        write();
        read();
    }

    static void write() throws  IOException {
        Map<Integer, Student> map = new HashMap<Integer, Student>();
        map.put(1, new Student("王小明", 90,50,80));
        map.put(2, new Student("陳小東", 70,67,90));
        Element root = new Element("list");
        Document document = new Document(root);
        for(Student student: map.values()){
            Element nameElement = new Element("name");
            nameElement.setText(student.name);
            Element chElement = new Element("ch");
            chElement.setText(String.valueOf( student.ch));
            Element enElement = new Element("en");
            enElement.setText(String.valueOf(student.en));
            Element mathElement = new Element("math");
            mathElement.setText(String.valueOf( student.math));
            Element stduentElement = new Element("Student");
            stduentElement
                    .addContent(nameElement)
                    .addContent(enElement)
                    .addContent(chElement)
                    .addContent(mathElement);
            document.getRootElement().addContent(stduentElement);
        }
        String path = Test.class.getClassLoader().getResource("").getPath();
        File file = new File(String.format("%s/%s", path, "test.xml"));
        file.createNewFile();
        OutputStream stream = new FileOutputStream(file.getPath());
        XMLOutputter xmlOutputter = new XMLOutputter(Format.getCompactFormat());
        xmlOutputter.output(document, stream);
        stream.flush();
        stream.close();
        stream=null;
        file=null;
        xmlOutputter=null;
    }

    static void read() throws JDOMException, IOException {
        SAXBuilder builder = new SAXBuilder();
        Document rootDocument= builder.build(Test.class.getClassLoader().getResource("test.xml"));
        Element rootElement = rootDocument.getRootElement();
        List<Element> elementList = rootElement.getChildren("Student");
        for (Element element: elementList){
            String name = element.getChild("name").getValue();
            int ch =Integer.parseInt( element.getChild("ch").getValue());
            int math = Integer.parseInt(element.getChild("math").getValue());
            int en = Integer.parseInt(element.getChild("en").getValue());
            System.out.printf("Name: %s Ch %d math %d en %d\n", name,ch,math,en);
        }
    }
    
}

執行結果:

沒有留言:

張貼留言