posts - 54,  comments - 1,  trackbacks - 0

           

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

          import org.jdom.Document;
          import org.jdom.Element;
          import org.jdom.JDOMException;
          import org.jdom.Text;
          import org.jdom.input.SAXBuilder;
          import org.jdom.output.Format;
          import org.jdom.output.XMLOutputter;
          import org.jdom.xpath.XPath;

          /**
           * 使用JDom操作XML工具
           * 
           * @author ZhuJun Create on 2005-8-17
           
          */

          public class XmlUtil {

           
          /**
            * 解析XML文件,生成Document對象
            * 
            * @param fileName
            * @return @throws
            *         JDOMException
            * @throws IOException
            * 
            * @author ZhuJun Create on 2005-8-17
            
          */

           
          public static Document parseXml(String fileName) throws JDOMException,
             IOException 
          {
            SAXBuilder builder 
          = new SAXBuilder();
            
          return builder.build(fileName);
           }


           
          /**
            * 輸出XML文件
            * 
            * @param docXML
            * @param strFilename
            * 
            * @author ZhuJun Create on 2005-8-17
            
          */

           
          public static void OutputXML(Document doc, String strFilename) {
            XMLOutputter fmt 
          = new XMLOutputter();
            fmt.setFormat(Format.getPrettyFormat());
            
          try {
             FileWriter fwXML 
          = new FileWriter(strFilename);
             fmt.output(doc, fwXML);
             fwXML.close();
            }
           catch (IOException e) {
             e.printStackTrace();
            }

           }


           
          /**
            * 取得節(jié)點(diǎn)的值
            * 
            * @param xPath
            * @return @throws
            *         JDOMException
            * 
            * @author ZhuJun Create on 2005-8-17
            
          */

           
          public static String getNodeValue(Document doc, String xPath)
             throws JDOMException 
          {
            
          return ((Text) XPath.selectSingleNode(doc.getRootElement(),
              
          new StringBuffer(xPath).append("/text()").toString()))
              .getTextNormalize();
           }


           
          /**
            * 取得節(jié)點(diǎn)的屬性值
            * 
            * @param xPath
            * @param attrName
            * @return @throws
            *         JDOMException
            * 
            * @author ZhuJun Create on 2005-8-17
            
          */

           
          public static String getAttributeValue(Document doc, String xPath,
             String attrName) throws JDOMException 
          {
            Element element 
          = (Element) XPath.selectSingleNode(
              doc.getRootElement(), xPath);
            
          return element.getAttributeValue(attrName);
           }

          }
          posted on 2005-08-18 14:12 ZhuJun 閱讀(344) 評論(0)  編輯  收藏 所屬分類: 開發(fā)手記

          蜀中人氏,躬耕于珠海

          <2005年8月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(2)

          隨筆分類(71)

          隨筆檔案(54)

          博客

          文檔

          站點(diǎn)

          論壇

          搜索

          •  

          積分與排名

          • 積分 - 51232
          • 排名 - 974

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 宜兰市| 海阳市| 黔江区| 桃园县| 南丰县| 阿克陶县| 铜梁县| 东源县| 荔波县| 平谷区| 富裕县| 襄城县| 遵义市| 遂宁市| 铜川市| 黔西县| 宜城市| 海南省| 萨迦县| 班戈县| 万宁市| 樟树市| 梨树县| 南投县| 北碚区| 青阳县| 定边县| 临沭县| 馆陶县| 那坡县| 孟村| 青岛市| 卢龙县| 桓台县| 湟中县| 那曲县| 巨鹿县| 岱山县| 田林县| 安龙县| 礼泉县|