首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

运用XMLInputFactory,XMLStreamReader,XMLEventReader解析XML文件

2013-08-04 
使用XMLInputFactory,XMLStreamReader,XMLEventReader解析XML文件package com.org.domainimport java.io.

使用XMLInputFactory,XMLStreamReader,XMLEventReader解析XML文件
package com.org.domain;import java.io.IOException;import java.io.InputStream;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.stream.EventFilter;import javax.xml.stream.XMLEventReader;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.events.XMLEvent;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;/** * 使用XMLInputFactory,XMLStreamReader,XMLEventReader解析XML文件 * */public class DoXML {private String url="com/org/config/MyXml.xml";/** * 文件读取流获取XML节点 * @throws IOException */@Testpublic void testXMLStreamReader() throws IOException {try {XMLInputFactory factory = XMLInputFactory.newInstance();InputStream in = DoXML.class.getClassLoader().getResourceAsStream(url);XMLStreamReader reader = factory.createXMLStreamReader(in);while (reader.hasNext()) {int type = reader.next();if(type==XMLStreamConstants.START_ELEMENT){//开始节点System.out.print(reader.getName());}else if(type==XMLStreamConstants.CHARACTERS){//表示事件字符System.out.println("type"+type);}else if(type==XMLStreamConstants.END_ELEMENT){//结束节点System.out.println(reader.getName());}}reader.close();} catch (Exception e) {e.printStackTrace();}}/** * XML事件读取 */@Testpublic void testXMLEventReader(){try {XMLInputFactory factory = XMLInputFactory.newInstance();InputStream in = DoXML.class.getClassLoader().getResourceAsStream(url);XMLEventReader reader = factory.createXMLEventReader(in);while(reader.hasNext()){XMLEvent event = reader.nextEvent();if(event.isStartElement()){String name = event.asStartElement().getName().toString();if("Title".equals(name)){System.out.println(reader.getElementText());}}}}catch (Exception e) {e.printStackTrace();}}/** * 通过XML事件Filter过滤节点的值 */@Testpublic void testXMLFilteredReader(){try {XMLInputFactory factory = XMLInputFactory.newInstance();InputStream in = DoXML.class.getClassLoader().getResourceAsStream(url);XMLEventReader eventReader = factory.createXMLEventReader(in);XMLEventReader reader = factory.createFilteredReader(eventReader, new EventFilter() {@Overridepublic boolean accept(XMLEvent event) {//有Titl节点返回true,否则返回falseif(event.isStartElement()) {String name = event.asStartElement().getName().toString();if(name.equals("Title")){return true;}}return false;}});while(reader.hasNext()){XMLEvent event = reader.nextEvent();if(event.isStartElement()){String name = event.asStartElement().getName().toString();if("Title".equals(name)){System.out.println(reader.getElementText());} }}} catch (Exception e) {e.printStackTrace();}}/** * 通过XPath拿节点的值 */@Testpublic void testXPathXMLNodeText(){try {InputStream in = DoXML.class.getClassLoader().getResourceAsStream(url);DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder db = factory.newDocumentBuilder();Document doc = db.parse(in);XPathFactory xPathFactory = XPathFactory.newInstance();XPath xpath = xPathFactory.newXPath();String expression = "Articles//item[@category='language']";NodeList nodeList = (NodeList)xpath.evaluate(expression,doc,XPathConstants.NODESET);for(int i = 0 ; i< nodeList.getLength();i++){Element ele = (Element)(nodeList.item(i));NodeList tiel = ele.getElementsByTagName("Title");Element e = (Element) tiel.item(0);System.out.println(e.getTextContent());}} catch (Exception e) {e.printStackTrace();}}/** * 修改XML节点的值 */@Testpublic void testUpdateXMLNodeText(){try {InputStream in = DoXML.class.getClassLoader().getResourceAsStream(url);Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);XPath xPath = XPathFactory.newInstance().newXPath();Transformer transformer = TransformerFactory.newInstance().newTransformer();transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");String expression = "Articles//item[@category='language']/Title";//修改Title的值NodeList nodelist = (NodeList)xPath.evaluate(expression, doc,XPathConstants.NODESET);Element price = (Element)(nodelist.item(0));price.setTextContent("PHP");transformer.transform(new DOMSource(doc), new StreamResult(System.out));} catch (Exception e) {e.printStackTrace();}}/** * 组装XML *///@Testpublic void testXMLStreamWriter(){/*try {XMLOutputFactory factory = XMLOutputFactory.newInstance();XMLStreamWriter writer = factory.createXMLStreamWriter(System.out);String ns= "http://www.google.com";writer.writeStartDocument("UTF-8","1.0");writer.setDefaultNamespace("http://www.google.com");writer.writeDefaultNamespace("http://www.google.com");writer.writeNamespace("ns", ns);writer.setPrefix("ns", ns);writer.writeStartElement("ns","this",ns);writer.writeCharacters("test");writer.writeStartElement("ns","test",ns);writer.writeCharacters("content");writer.writeEndElement();writer.writeEndElement();writer.writeEndDocument();writer.flush();writer.close();} catch (Exception e) {e.printStackTrace();}*/}}

?

热点排行