分享

Document的应用

 Jeln 2009-03-26
写Document:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public class DocumentWriterTest {
 private static String fileName = "docTest.xml";
 
 private static String filePath = "doc" + File.separator + fileName;
 
 public static void main(String[] arg) {
  File file = new File(fileName);
  if (file.exists()) {
   file.mkdirs();
  }
  XMLOutputter out = new XMLOutputter();
  Element root = new Element("Root");
  Document doc = new Document(root);
  doc.setRootElement(root);
  for (int i = 0; i < 3; i++) {
   Element test = new Element("Test");
   Element test1 = new Element("Test1");
   Element test2 = new Element("Test2");
   Element test3 = new Element("Test3");
   test1.addContent(i + "\n");
   test2.addContent(Math.exp(i) + "\n");
   test3.addContent(Math.pow(i, i) + "\n");
   test.addContent(test1);
   test.addContent(test2);
   test.addContent(test3);
   root.addContent(test);
  }
  try {
   out.output(doc, new FileWriter(filePath));
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println("File path is : " + file.getPath());
 } 
}
读Document:
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DocumentReaderTest {
 
 private static String fileName = "docTest.xml";
 private static String filePath = "doc" + File.separator + fileName;
 
 private static String seperator = "//Test";
 
 public static void main(String[] arg) {
  try {
   /*InputStream in = DocumentReaderTest.class.getClassLoader().getResourceAsStream(filePath);
   Document doc = loadDocumentFile(in);*/
   Document doc = loadDocumentFile(filePath);
   List testList = getElements(doc, seperator);
   Iterator it = testList.iterator();
   int i = 0;
   
   while (it.hasNext()) {
    System.out.println("###################" + i);
    int j = 0;
    Element child = (Element) it.next();
    Iterator childIt = child.elementIterator();
    while (childIt.hasNext()) {
     Element childEle = (Element) childIt.next();
     if (childEle.getName().equalsIgnoreCase("Test1")) {
      System.out.println(i + "\n" + j + "Test1 = " + childEle.getText());
     }
     if (childEle.getName().equalsIgnoreCase("Test2")) {
      System.out.println(i + "\n" + j + "Test2 = " + childEle.getText());
     }
     if (childEle.getName().equalsIgnoreCase("Test3")) {
      System.out.println(i + "\n" + j + "Test3 = " + childEle.getText());
     }
     j ++;
    }
    i ++;
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 private static Document loadDocumentFile(String filePath) {
  SAXReader reader = new SAXReader();
  Document doc = null;
  try {
   doc = reader.read(filePath);
  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return doc;
 }
 private static List getElements(Document doc, String seperator) {
  return doc.selectNodes(seperator);
 }
 private static Document loadDocumentFile(InputStream in) {
  SAXReader reader = new SAXReader();
  Document doc = null;
  try {
   doc = reader.read(in);
  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return doc;
 }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多