(1)XPath的使用
前面讲了Stax处理XML的查找,有基于光标的查找,和基于迭代模型的查找。Stax也支持XPath查找。
/** * XPath的使用 * 查找category为WEB的bookList */ @Test public 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,第二个参数是文档 //查找category为WEB的bookList NodeList list = (NodeList)xpath.evaluate("//book[@category='WEB']",doc,XPathConstants.NODESET); for (int i = 0; i < list.getLength(); i++) { //遍历输出相应的结果 Element el = (Element)list.item(i); System.out.println(el.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(); } } }
输出结果:
XQuery Kick Start
Learning XML
(2)写文档:使用XMLStreamWriter创建xml
/** * 使用XMLStreamWriter创建xml */ @Test public void test07() { try { XMLStreamWriter xsw = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out); xsw.writeStartDocument("UTF-8","1.0"); xsw.writeEndDocument(); String ns = "http://11:dd"; //三个参数分别是前缀、标签名、命名空间URI xsw.writeStartElement("ns","person",ns); xsw.writeStartElement(ns,"id"); //xsw.writeStartElement("person"); //xsw.writeStartElement("id"); xsw.writeCharacters("1"); xsw.writeEndElement(); xsw.writeEndElement(); xsw.flush(); xsw.close(); } catch (XMLStreamException e) { e.printStackTrace(); } catch (FactoryConfigurationError e) { e.printStackTrace(); } }
输出结果:
<?xml version="1.0" encoding="UTF-8"?><ns:person><ns:id>1</ns:id></ns:person>
去掉注释部分后的输出结果:
<?xml version="1.0" encoding="UTF-8"?><ns:person><ns:id><person><id>1</id></person>
(3)修改文档:使用Transformer更新节点信息
读取文档,修改部分,并打印出文档
/** * 使用Transformer更新节点信息 * 修改标题为Learning XML的书的价格为333.9 */ @Test public 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']",XPathConstants.NODESET); //获取上面所查找书的price节点,并set Element be = (Element)list.item(0); Element el = (Element)(be.getElementsByTagName("price").item(0)); el.setTextContent("333.9"); //通过transformer修改节点 Result result = new StreamResult(System.out); 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(); } } }
输出结果:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>333.9</price>
</book>
</bookstore>
原文链接:https://www.f2er.com/xml/299336.html