Dom4j解析XML

          Posted on 2009-06-17 21:52 eric_xu 閱讀(191) 評論(0)  編輯  收藏 所屬分類: Java
          package sjtu.rfid;

          import java.io.File;

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

          public class ParserXML {
              
          public void Parser(File inputXml){
                  
          try{
                      SAXReader saxReader 
          = new SAXReader();
                      Document document 
          = saxReader.read(inputXml);
                      
                      Element root 
          = document.getRootElement();
                      
                      System.out.println(root.element(
          "username").getText());
                      System.out.println(root.element(
          "password").getText());
                      
                  }
          catch(DocumentException e){
                      e.printStackTrace();
                  }
              }
              
              
          public static void main(String[] args){
                  ParserXML parser 
          = new ParserXML();
                  parser.Parser(
          new File("config.xml"));
              }
          }

          package sjtu.rfid;

          import java.io.File;
          import java.io.FileWriter;
          import java.io.IOException;

          import org.dom4j.Document;
          import org.dom4j.DocumentHelper;
          import org.dom4j.Element;
          import org.dom4j.io.XMLWriter;

          public class CreateXML {

              
          public void createXML(){
                  Document document 
          = DocumentHelper.createDocument();
                  
                  Element configElement 
          = document.addElement("config");
                  configElement.addComment(
          "This is my configueration.");
                  
                  Element usernameElement 
          = configElement.addElement("username");
                  Element passwordElement 
          = configElement.addElement("password");
                  
                  usernameElement.addAttribute(
          "id""97");
                  
                  usernameElement.setText(
          "Eric");
                  passwordElement.setText(
          "1234");
                  
                  
          try {
                      XMLWriter output 
          = new XMLWriter(new FileWriter(new File(
                              
          "d:/config.xml")));
                      output.write(document);
                      output.close();
                  } 
          catch (IOException e) {
                      System.out.println(e.getMessage());
                  }
              }
              
              
          public static void main(String[] args) {
                  CreateXML create 
          = new CreateXML();
                  create.createXML();
              }

          }

          Nested exception
          XML格式錯誤

          posts - 37, comments - 5, trackbacks - 0, articles - 0

          Copyright © eric_xu

          主站蜘蛛池模板: 通州市| 信宜市| 绥江县| 禹州市| 岗巴县| 新平| 南澳县| 江孜县| 广昌县| 临夏市| 林州市| 重庆市| 井冈山市| 陆川县| 长顺县| 建水县| 龙胜| 贵德县| 乌鲁木齐市| 漳平市| 舟山市| 资溪县| 建德市| 曲松县| 武汉市| 香港| 长顺县| 阳谷县| 桐乡市| 蒲江县| 漠河县| 陕西省| 长治县| 泾川县| 保康县| 博客| 景德镇市| 福泉市| 宁波市| 长葛市| 慈利县|