public static void test06() {
InputStream is = null;
try {
is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
//創(chuàng)建文檔處理對(duì)象
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//通過DocumentBuilder創(chuàng)建doc的文檔對(duì)象
Document doc = db.parse(is);
//創(chuàng)建XPath
XPath xpath = XPathFactory.newInstance().newXPath();
//第一個(gè)參數(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)建文檔處理對(duì)象
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//通過DocumentBuilder創(chuàng)建doc的文檔對(duì)象
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");
//第一個(gè)參數(shù)就是xpath,第二參數(shù)就是文檔
NodeList list = (NodeList)xpath.evaluate("http://book[title='Learning XML']", doc,XPathConstants.NODESET);
//獲取price節(jié)點(diǎn)
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é)點(diǎn)
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();
}
}
}