<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Believe it,do it!

    Ideal is the beacon. Without ideal, there is no secure direction; without direction ,there is no life.
    理想是指路明燈。沒有理想,就沒有堅(jiān)定的方向;沒有方向,就沒有生活。
    CTRL+T eclipse
    posts - 35, comments - 3, trackbacks - 0, articles - 0
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    xml文件操作(利用dom4j)

    Posted on 2008-09-27 15:10 三羽 閱讀(346) 評論(0)  編輯  收藏 所屬分類: 收 藏 夾
    代碼:
    package dom_xml;

    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.Attribute;
    import java.util.List;
    import java.util.Iterator;

    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;
    import java.io.*;
    import org.dom4j.DocumentException;
    import org.dom4j.io.SAXReader;
    import org.dom4j.DocumentHelper;

    public class XmlDom4J {

    /**
      * 生成xml文件;
      *
      */
    public void createXMLFile(){
      //使用 DocumentHelper 類創(chuàng)建一個(gè)文檔實(shí)例。 DocumentHelper 是生成 XML 文檔節(jié)點(diǎn)的 dom4j API 工廠類。
      Document document=DocumentHelper.createDocument();
     
      //使用 addElement() 方法創(chuàng)建根元素 catalog 。addElement() 用于向 XML 文檔中增加元素。
      Element catalogElement = document.addElement("catalog");
      //在 catalog 元素中使用 addComment() 方法添加注釋“An XML catalog”。
      catalogElement.addComment("An XML Catalog");
      //在 catalog 元素中使用 addProcessingInstruction() 方法增加一個(gè)處理指令。
      catalogElement.addProcessingInstruction("target","text");
     
      //在 catalog 元素中使用 addElement() 方法增加 journal 元素。
      Element journal=catalogElement.addElement("journal");
      //使用 addAttribute() 方法向 journal 元素添加 title 和 publisher 屬性。
      journal.addAttribute("title", "XML Zone");
      journal.addAttribute("publisher", "IBM Devoloperment");
     
      //添加節(jié)點(diǎn)journal的子節(jié)點(diǎn)article,并設(shè)置其屬性;
      Element articleElement=journal.addElement("article");
      articleElement.addAttribute("level", "Intermediate");
      articleElement.addAttribute("date", "December-2008");
     
      //添加節(jié)點(diǎn)articleElement的子結(jié)點(diǎn)title,并使用 setText() 方法設(shè)置其元素的文本。
      Element titleElement=articleElement.addElement("title");
      titleElement.setText("又是下雨天");
     
      //添加節(jié)點(diǎn)articleElement的子結(jié)點(diǎn)author.添加子結(jié)點(diǎn)的子結(jié)點(diǎn)firstname、lastname,并設(shè)置其文件;
      Element authorElement=articleElement.addElement("author");
         Element  firstNameElement=authorElement.addElement("firstname");
         firstNameElement.setText("Marcello");
         Element lastNameElement=authorElement.addElement("lastname");
         lastNameElement.setText("Vitaletti");
        
         //可以使用 addDocType()  方法添加文檔類型說明。
       
        
         XMLWriter output;
      try {
       OutputFormat format=new OutputFormat();
       format.setEncoding("gb2312");
       output = new XMLWriter(
         new FileWriter(new File("catalog.xml")),format);
       output.write(document);
       output.close();
      } catch (IOException e) {
       e.printStackTrace();
      } 
    }

    /**
      * 修改xml文件指定節(jié)點(diǎn)的屬性;
      * @param inputXml xml文件流
      * @oldAttributeValue 原屬性;
      * @attributeValue 要修改成的值;
      * @param XPath 要修改節(jié)點(diǎn)屬性的表達(dá)式;如:"http://article/@level" 則表示修改節(jié)點(diǎn)level(父節(jié)點(diǎn)為article)的屬性
      * 特別說明:@后面表示的是屬性;
      */
    public Document modifyXMLNodeAttributeByName(File inputXml, String XPath,String oldAttributeValue,String attributeValue) {
      if(XPath.indexOf("@")<0){
       System.out.println("參數(shù)XPath無效,請?jiān)谝薷牡膶傩郧凹尤?@'");
       return null;
      }
      SAXReader saxReader = new SAXReader();
      Document document=null;
      try {
       document = saxReader.read(inputXml);
       List list = document.selectNodes(XPath);
       Iterator iter = list.iterator();
       while (iter.hasNext()) {
        Attribute attribute = (Attribute) iter.next();
        if (attribute.getValue().equals(oldAttributeValue))//把原屬性修改為新的屬性;
         attribute.setValue(attributeValue);
       }
       
      } catch (DocumentException e) {   
       e.printStackTrace();
      }
      return document;
     
    }

    /**
      * 修改指定節(jié)點(diǎn)的屬性值;
      * @param inputXml xml文件流
      * @param XPath 要修改節(jié)點(diǎn)屬性的表達(dá)式;如:"http://article/@level" 則表示修改節(jié)點(diǎn)level(父節(jié)點(diǎn)為article)的屬性
      * @param attributeValue 屬性新值;
      */
    public Document modifyXMLNodeAttributeByName(File inputXml, String XPath,String attributeValue) {
      if(XPath.indexOf("@")<0){
       System.out.println("參數(shù)XPath無效,請?jiān)谝薷牡膶傩郧凹尤?@'");
       return null;
      }
      SAXReader saxReader = new SAXReader(); 
      Document document=null;
      try {
       document = saxReader.read(inputXml); 
       List list = document.selectNodes(XPath);   
       Iterator iter = list.iterator();
       while (iter.hasNext()) {
        Attribute attribute = (Attribute) iter.next();   
        //把原屬性修改為新的屬性;
        attribute.setValue(attributeValue);
       }
       
      } catch (DocumentException e) {   
       e.printStackTrace();
      }
      return document;
    }

    /**
      * 獲取某一節(jié)點(diǎn)的屬性值;
      * @param inputxml xml文件;
      * @param XPath
      * @return
      */
    public String[] getNodeAttributeValue(File inputxml,String XPath){
      String nodeAttri="";//儲(chǔ)存節(jié)點(diǎn)屬性值;
      if(XPath.indexOf("@")<0){
       return null;
      }
      SAXReader saxReader=new SAXReader();
      Document document=null;
      try{
       document=saxReader.read(inputxml);
       List list=document.selectNodes(XPath);
       Iterator it=list.iterator();
       while(it.hasNext()){
        Attribute attri=(Attribute)it.next();
        nodeAttri+=attri.getValue()+",";
       }
      }catch(Exception e){
       e.printStackTrace();
      }
      if(nodeAttri.length()>0){
       nodeAttri=nodeAttri.substring(0, nodeAttri.length()-1);
      }
      return nodeAttri.split(",");
    }

    /**
      * 修改指定節(jié)點(diǎn)的文本值;
      * @param inputXml
      * @param XPath 要修改節(jié)點(diǎn)屬性的表達(dá)式;如:"http://article/@level" 則表示article節(jié)點(diǎn)下的所有l(wèi)evel節(jié)點(diǎn)的文本;
      * @param newText 新的文本值;
      */
    public Document modifyXMLNodeTextByName(File inputXml,String XPath,String newText){
      if(XPath.indexOf("@")>=0){
       System.out.println("參數(shù)XPath無效!");
       return null;
      }
      SAXReader saxReader = new SAXReader();
      Document document=null;
      try {
       document=saxReader.read(inputXml);
       List list= document.selectNodes(XPath);
       Iterator iter = list.iterator();
       while(iter.hasNext()){   
        Element elementText=(Element)iter.next();   
        elementText.setText(newText);   
       }
      } catch (DocumentException e) {   
       e.printStackTrace();
      }
      return document;
    }

    /**
      *  替換指定節(jié)點(diǎn)文本的值。
      * @param inputXml xml文件流
      * @param XPath 要修改節(jié)點(diǎn)屬性的表達(dá)式;如:"http://article/level" 則表示article節(jié)點(diǎn)下的所有l(wèi)evel節(jié)點(diǎn)的文本;
      * @param oldText 原文本
      * @param newText 新文本;
      */
    public Document modifyXMLNodeTextByName(File inputXml,String XPath,String oldText,String newText){
      if(XPath.indexOf("@")>=0){
       System.out.println("參數(shù)XPath無效!");
       return null;
      }
      SAXReader saxReader = new SAXReader();
      Document document=null;
      try {
       document=saxReader.read(inputXml);
       List list= document.selectNodes(XPath);
       Iterator iter = list.iterator();
       while(iter.hasNext()){
        Element elementText=(Element)iter.next();
        if(elementText.getText().equals(oldText))
        elementText.setText(newText);
       }
      } catch (DocumentException e) {   
       e.printStackTrace();
      }
      return document;
    }
    /**
      * 獲取某一節(jié)點(diǎn)的文本內(nèi)容;
      * @param inputxml xml文件;
      * @param XPath
      * @return
      */
    public String[] getNodeTextValue(File inputxml,String XPath){
      String nodeTextValue="";//儲(chǔ)存節(jié)點(diǎn)屬性值;
      if(XPath.indexOf("@")>=0){
       return null;
      }
      SAXReader saxReader=new SAXReader();
      Document document=null;
      try{
       document=saxReader.read(inputxml);
       List list=document.selectNodes(XPath);
       Iterator it=list.iterator();
       while(it.hasNext()){
        Element text=(Element)it.next();
        nodeTextValue+=text.getText()+",";
       }
      }catch(Exception e){
       e.printStackTrace();
      }
      if(nodeTextValue.length()>0){
       nodeTextValue=nodeTextValue.substring(0, nodeTextValue.length()-1);
      }
      return nodeTextValue.split(",");
    }



    /**
      * 保存xml文件;
      * @param document xml文件流;
      * @param filePath 文件存儲(chǔ)的全路徑(包括文件名)
      * @code 儲(chǔ)存的編碼;
      */
    public void saveXmlFile(Document document,String filePath,String code){
      if(document==null){
       return ;
      }
      XMLWriter output;
      try {
       OutputFormat format=new OutputFormat();
       format.setEncoding(code);
       output = new XMLWriter(new FileWriter(new File(filePath)),format);
       output.write( document );
       output.close();
      } catch (IOException e) {   
       e.printStackTrace();
      } 
    }
     
      // 測試;
    public static void main(String[] args){
      XmlDom4J dom4jParser=new XmlDom4J(); 
      //生成XML
      //dom4jParser.createXMLFile();
      File file=new File("D:/MyWork/operateXMLfile/catalog.xml");
      //dom4jParser.saveXmlFile(document, "F://test.xml", "GBK");
     
      /*String[] attrArray=dom4jParser.getNodeAttributeValue(file, "http://article/@level");
      if(attrArray!=null){
       for(int i=0;i<attrArray.length;i++){
        System.out.println("Attribute is :"+attrArray[i]);
       }
      }*/
     
      String[] nodeText=dom4jParser.getNodeTextValue(file, "http://article/title");
      if(nodeText!=null){
       for(int i=0;i<nodeText.length;i++){
        System.out.println("NODE TEXT IS:"+nodeText[i]);
       }
      }
     
    }
    }
    xml文件定義如下:
    復(fù)制內(nèi)容到剪貼板
    代碼:
    <?xml version="1.0" encoding="gb2312"?>
    <catalog><!--An XML Catalog--><?target text?>
    <journal title="XML Zone" publisher="IBM Devoloperment">
    <article level="小學(xué)四年級(jí)"  date="December-2008"><title>又是下雨天</title>
    <author><firstname>Marcello</firstname><lastname>Vitaletti</lastname></author></article>
    <article level="大學(xué)四年級(jí)"  date="2008-04-01"><title>太陽出來了</title>
    <author><firstname>Marcello</firstname><lastname>Vitaletti</lastname></author></article>
    </journal></catalog>
    主站蜘蛛池模板: 久久精品国产亚洲精品| 天天操夜夜操免费视频| 亚洲女同成av人片在线观看| 在线观看亚洲专区| 国产成人一区二区三区免费视频 | 成人黄页网站免费观看大全| 亚洲免费网站在线观看| 曰曰鲁夜夜免费播放视频| 亚洲人成免费电影| 香蕉视频在线观看免费国产婷婷 | 亚洲色图.com| 亚洲免费福利在线视频| 亚洲欧美乱色情图片| 国产网站免费观看| 国产乱子伦精品免费视频| 亚洲国产三级在线观看| 9277手机在线视频观看免费| 亚洲免费视频网址| 亚洲AV无码一区二区三区国产| 一级毛片不卡免费看老司机| 久久国产精品亚洲综合| 国产在线观看片a免费观看| 久久亚洲欧美国产精品| 亚洲人成网77777色在线播放| 一个人免费视频在线观看www | 亚洲av成人一区二区三区| 老司机永久免费网站在线观看| 一级A毛片免费观看久久精品| 亚洲AV无码一区二区三区DV| 日本免费xxxx色视频| 国产成人高清亚洲一区久久| 亚洲宅男天堂在线观看无病毒| 在线a免费观看最新网站| 亚洲精品人成网线在线播放va| 亚洲熟妇中文字幕五十中出| 亚洲一区免费视频| 一级黄色免费网站| 亚洲欧洲精品国产区| 一区二区三区亚洲视频| 100部毛片免费全部播放完整| 四虎精品免费永久免费视频|