xml操作

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class Test {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        try {
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><File><FileInfo><FileId>FileId1</FileId><FilePath>FilePath1</FilePath></FileInfo><FileInfo><FileId>FileId2</FileId><FilePath>FilePath2</FilePath></FileInfo></File>";
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes()));
            Document document = saxReader.read(new File("E:/fileType.xml"));
            Element rootElement = document.getRootElement();
            Iterator<Element> it = rootElement.elementIterator("FileInfo");
            while (it.hasNext()) {
                Element e = it.next();
                System.out.println(e.elementText("FileId"));
                System.out.println(e.elementText("FilePath"));
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章