java学习

java学习

 

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

public static void test06() {
  InputStream is = null;
  try {
   is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
   //创建文档处理对象
   DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
   //通过DocumentBuilder创建doc的文档对象
   Document doc = db.parse(is);
   //创建XPath
   XPath xpath = XPathFactory.newInstance().newXPath();
   //第一个参数就是xpath,第二参数就是文档
   NodeList list = (NodeList)xpath.evaluate("//book[@category='WEB']", doc,XPathConstants.NODESET);
   for(int i=0;i<list.getLength();i++) {
    //遍历输出相应的结果
    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");
   //创建文档处理对象
   DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
   //通过DocumentBuilder创建doc的文档对象
   Document doc = db.parse(is);
   //创建XPath
   XPath xpath = XPathFactory.newInstance().newXPath();
   Transformer tran = TransformerFactory.newInstance().newTransformer();
   tran.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
   tran.setOutputProperty(OutputKeys.INDENT, "yes");
   //第一个参数就是xpath,第二参数就是文档
   NodeList list = (NodeList)xpath.evaluate("//book[title='Learning XML']", doc,XPathConstants.NODESET);
   //获取price节点
   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修改节点
   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 杨军威 阅读(2333) 评论(1)  编辑  收藏

评论

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

xzxzx  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜