天天看點

dom4j的基本操作

原文: http://www.java2000.net/p5724

  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStreamWriter;
  7. import java.io.Writer;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import org.dom4j.Document;
  11. import org.dom4j.DocumentException;
  12. import org.dom4j.DocumentHelper;
  13. import org.dom4j.Element;
  14. import org.dom4j.io.OutputFormat;
  15. import org.dom4j.io.SAXReader;
  16. import org.dom4j.io.XMLWriter;
  17. import org.xml.sax.EntityResolver;
  18. import org.xml.sax.InputSource;
  19. import org.xml.sax.SAXException;
  20. public class MyTest {
  21.   Document doc = null;
  22.   public MyTest() throws DocumentException, IOException, SAXException {
  23.     Document doc = loadXML("class.xml"); // 載入XML文檔
  24.     System.out.println(doc.asXML());
  25.     printDoc(doc); // 列印XML文檔
  26.     storeDoc(doc, "new.xml"); // 把XML文檔存入硬碟
  27.     doc = valideDoc("class.xml"); // 校驗dtd XML文檔
  28.     printDoc(doc);
  29.     doc = validateDocBySxd("classSchema.xml"); // 校驗Schema文檔
  30.     printDoc(doc);
  31.     String url = getClass().getResource("/xml/dom4j/wkjava/class.xsd").toString();
  32.     doc = validateDocBySxd("classSchema.xml", url); // 校驗Schema文檔(倆參數)
  33.     printDoc(doc);
  34.     doc = createDoc(); // 建立Schema文檔
  35.     storeDoc(doc, "root.xml");
  36.     doc = validateDocBySxd("classSchema.xml");
  37.     updateZip(doc, "102202"); // 在文檔中修改原屬
  38.     printDoc(doc);
  39.     doc = validateDocBySxd("classSchema.xml");
  40.     printNames(doc); // 列印文檔中所有學生名字
  41.     System.out.println(getStudentCount(doc));
  42.   }
  43.   public static void main(String[] args) {
  44.     try {
  45.       new MyTest();
  46.     } catch (FileNotFoundException e) {
  47.       e.printStackTrace();
  48.     } catch (DocumentException e) {
  49.       e.printStackTrace();
  50.     } catch (IOException e) {
  51.       e.printStackTrace();
  52.     } catch (SAXException e) {
  53.       e.printStackTrace();
  54.     }
  55.   }
  56.   public Document loadXML(String xmlfile) throws FileNotFoundException, DocumentException {
  57.     SAXReader reader = new SAXReader();
  58.     doc = reader.read(new FileInputStream(xmlfile));
  59.     return doc;
  60.   }
  61.   public void printDoc(Document doc) throws IOException {
  62.     Writer out = new OutputStreamWriter(System.out, "gb2312");
  63.     OutputFormat format = OutputFormat.createPrettyPrint();
  64.     XMLWriter writer = new XMLWriter(out, format);
  65.     writer.write(this.doc);
  66.     out.flush();
  67.   }
  68.   public void storeDoc(Document doc, String filename) throws IOException {
  69.     Writer out = new OutputStreamWriter(new FileOutputStream(filename), "utf-8");
  70.     OutputFormat format = OutputFormat.createPrettyPrint();
  71.     XMLWriter writer = new XMLWriter(out, format);
  72.     writer.write(this.doc);
  73.     printDoc(doc);
  74.     out.close();
  75.   }
  76.   public Document valideDoc(String xmlfile) throws DocumentException, IOException {
  77.     EntityResolver resolver = new EntityResolver() {
  78.       public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
  79.         if (publicId.equals("//class from weiking")) {
  80.           InputStream in = new FileInputStream("class.dtd");
  81.           return new InputSource(in);
  82.         }
  83.         return null;
  84.       }
  85.     };
  86.     SAXReader reader = new SAXReader(true);
  87.     reader.setEntityResolver(resolver);
  88.     Document doc = reader.read(new FileInputStream(xmlfile));
  89.     return doc;
  90.   }
  91.   public Document validateDocBySxd(String xmlfile) throws SAXException, DocumentException, IOException {
  92.     SAXReader reader = new SAXReader(true);
  93.     reader.setFeature("http://apache.org/xml/features/validation/schema", true);
  94.     Document doc = reader.read(new FileInputStream(xmlfile));
  95.     return doc;
  96.   }
  97.   public Document validateDocBySxd(String xmlfile, String SchemaUrl) throws SAXException, FileNotFoundException,
  98.       DocumentException {
  99.     SAXReader reader = new SAXReader(true);
  100.     reader.setFeature("http://xml.org/sax/features/validation", true);
  101.     reader.setFeature("http://apache.org/xml/features/validation/schema", true);
  102.     reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
  103.     reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", SchemaUrl);
  104.     Document doc = reader.read(new FileInputStream(xmlfile));
  105.     return doc;
  106.   }
  107.   public Document createDoc() {
  108.     Document doc = DocumentHelper.createDocument();
  109.     Element root = doc.addElement("root");
  110.     Element author2 = root.addElement("author").addAttribute("name", "Toby").addAttribute("location", "Germany").addText(
  111.         "Tobias Rademacher");
  112.     Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK").addText(
  113.         "James Strachan");
  114.     return doc;
  115.   }
  116.   public void updateZip(Document doc, String zip) {
  117.     String xpath = "/Class/Teacher/zip";
  118.     Element e = (Element) doc.selectSingleNode(xpath);
  119.     e.setText(zip);
  120.   }
  121.   public void printNames(Document doc) {
  122.     String xpath = "/Class/Students/Student/name";
  123.     List list = doc.selectNodes(xpath);
  124.     for (Iterator i = list.iterator(); i.hasNext();) {
  125.       Element e = (Element) i.next();
  126.       System.out.println(e.element("last").getText() + e.valueOf("first"));
  127.     }
  128.   }
  129.   public int getStudentCount(Document doc) {
  130.     int count = 0;
  131.     String xpath = "count(/Class/Students/Student)";
  132.     count = doc.numberValueOf(xpath).intValue();
  133.     // String value = doc.valueOf(xpath);
  134.     // count = Integer.parseInt(value);
  135.     return count;
  136.   }
  137. }