java學(xué)習(xí)

          java學(xué)習(xí)

           

          XPath解析,寫入,修改xml文件

          public static void test06() {
            InputStream is = null;
            try {
             is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
             //創(chuàng)建文檔處理對象
             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
             //通過DocumentBuilder創(chuàng)建doc的文檔對象
             Document doc = db.parse(is);
             //創(chuàng)建XPath
             XPath xpath = XPathFactory.newInstance().newXPath();
             //第一個參數(shù)就是xpath,第二參數(shù)就是文檔
             NodeList list = (NodeList)xpath.evaluate("http://book[@category='WEB']", doc,XPathConstants.NODESET);
             for(int i=0;i<list.getLength();i++) {
              //遍歷輸出相應(yīng)的結(jié)果
              Element e = (Element)list.item(i);
              System.out.println(e.getElementsByTagName("title").item(0).getTextContent());
             }
            } catch (ParserConfigurationException e) {
             e.printStackTrace();
            } catch (SAXException e) {
             e.printStackTrace();
            } catch (IOException e) {
             e.printStackTrace();
            } catch (XPathExpressionException e) {
             e.printStackTrace();
            } finally {
             try {
              if(is!=null) is.close();
             } catch (IOException e) {
              e.printStackTrace();
             }
            }
           }
           
           @Test
           public static void test07() {
            try {
             XMLStreamWriter xsw = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
             xsw.writeStartDocument("UTF-8","1.0");
             xsw.writeEndDocument();
             String ns = "http://11:dd";
             xsw.writeStartElement("nsadfsadf","person",ns);
             xsw.writeStartElement(ns,"id");
             xsw.writeCharacters("1");
             xsw.writeEndElement();
             xsw.writeEndElement();
             xsw.flush();
             xsw.close();
            } catch (XMLStreamException e) {
             e.printStackTrace();
            } catch (FactoryConfigurationError e) {
             e.printStackTrace();
            }
           }
           
           @Test
           public static void test08() {
            InputStream is = null;
            try {
             is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
             //創(chuàng)建文檔處理對象
             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
             //通過DocumentBuilder創(chuàng)建doc的文檔對象
             Document doc = db.parse(is);
             //創(chuàng)建XPath
             XPath xpath = XPathFactory.newInstance().newXPath();
             Transformer tran = TransformerFactory.newInstance().newTransformer();
             tran.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
             tran.setOutputProperty(OutputKeys.INDENT, "yes");
             //第一個參數(shù)就是xpath,第二參數(shù)就是文檔
             NodeList list = (NodeList)xpath.evaluate("http://book[title='Learning XML']", doc,XPathConstants.NODESET);
             //獲取price節(jié)點
             Element be = (Element)list.item(0);
             Element e = (Element)(be.getElementsByTagName("price").item(0));
             e.setTextContent("333.9");
             Result result = new StreamResult(System.out);
             //通過tranformer修改節(jié)點
             tran.transform(new DOMSource(doc), result);
            } catch (ParserConfigurationException e) {
             e.printStackTrace();
            } catch (SAXException e) {
             e.printStackTrace();
            } catch (IOException e) {
             e.printStackTrace();
            } catch (XPathExpressionException e) {
             e.printStackTrace();
            } catch (TransformerConfigurationException e) {
             e.printStackTrace();
            } catch (TransformerFactoryConfigurationError e) {
             e.printStackTrace();
            } catch (TransformerException e) {
             e.printStackTrace();
            } finally {
             try {
              if(is!=null) is.close();
             } catch (IOException e) {
              e.printStackTrace();
             }
            }
           }

          posted on 2013-08-07 13:54 楊軍威 閱讀(2858) 評論(1)  編輯  收藏

          評論

          # re: XPath解析,寫入,修改xm為l文件 2014-12-22 17:48 sds

          xzxzx  回復(fù)  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 都兰县| 儋州市| 金乡县| 客服| 车险| 巫山县| 安仁县| 涞源县| 乌苏市| 米泉市| 房产| 昌图县| 农安县| 滦平县| 平顺县| 民乐县| 英吉沙县| 吕梁市| 泊头市| 隆昌县| 蓬安县| 北票市| 鄯善县| 济源市| 南通市| 新平| 罗源县| 芒康县| 南陵县| 简阳市| 马公市| 临沭县| 句容市| 内江市| 岚皋县| 嘉义市| 玛沁县| 凤冈县| 蓝田县| 思南县| 营山县|