码迷,mamicode.com
首页 > 其他好文 > 详细

使用jaxp对比xml进行DOM解析

时间:2015-09-07 22:31:32      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

/*DOM解析编程
•遍历所有节点

•查找某一个节点

•删除结点

•更新结点

•添加节点

/*


package
cn.itcast.jaxp; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class Demo1 { /**使用jaxp操作xml文档 * @param args * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { //1.获取工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //2.产生解析器 DocumentBuilder builder = factory.newDocumentBuilder(); //3.解析xml文档,得到代表文档的document Document document = builder.parse(new File("src/book1.xml")); //遍历 list(document); } //得到售价结点的值 @Test public void read() throws Exception{ //1.获取工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book.xml")); NodeList list = document.getElementsByTagName("售价"); Node price = list.item(0); String value = price.getTextContent(); System.out.println(value); } //修改结点的值:<售价>39.00元</售价>改为109 @Test public void update() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book1.xml")); Node price = document.getElementsByTagName("售价").item(0); price.setTextContent("109"); //把内存中的document写到xml文档 TransformerFactory tf = TransformerFactory.newInstance(); //得到转换器 Transformer ts = tf.newTransformer(); ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml"))); } //向指定节点中增加孩子节点(售价节点) @Test public void add() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book1.xml")); //创建需要增加的节点 Node price = document.createElement("售价"); price.setTextContent("59元"); //得到需要增加的节点的父亲 Node parent = document.getElementsByTagName("书").item(0); //把需要增加的节点挂到父结点上 parent.appendChild(price); TransformerFactory tf = TransformerFactory.newInstance(); Transformer ts = tf.newTransformer(); ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml"))); } //向指定位置上插入售价节点 @Test public void add2() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book1.xml")); Node node = document.createElement("售价"); node.setTextContent("39元"); Node parent = document.getElementsByTagName("书").item(0); parent.insertBefore(node, document.getElementsByTagName("书名").item(0)); TransformerFactory tf = TransformerFactory.newInstance(); Transformer ts = tf.newTransformer(); ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml"))); } //删除xml文档的售价结点 @Test public void delete() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book1.xml")); Node node = document.getElementsByTagName("售价").item(2); node.getParentNode().removeChild(node); TransformerFactory tf = TransformerFactory.newInstance(); Transformer ts = tf.newTransformer(); ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml"))); } //操作xml文档属性 @Test public void updateAttribute() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book1.xml")); //操作xml文档的元素时,一般都把元素当作node对象,但是程序员如果发现node不好使时,就应把node强转成相应类型 Node node = document.getElementsByTagName("书").item(0); Element book = null; if(node.getNodeType()==Node.ELEMENT_NODE){ //在作结点转换之前,最好先判断结点类型 book = (Element)node; } book.setAttribute("name", "yyyyyyy"); book.setAttribute("password", "123"); book.removeAttribute("password"); TransformerFactory tf = TransformerFactory.newInstance(); Transformer ts = tf.newTransformer(); ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml"))); } //遍历 public static void list(Node node){ if(node.getNodeType()==Node.ELEMENT_NODE){ System.out.println(node.getNodeName()); } NodeList list = node.getChildNodes(); for(int i=0;i<list.getLength();i++){ Node child = list.item(i); list(child); } } }

 

使用jaxp对比xml进行DOM解析

标签:

原文地址:http://www.cnblogs.com/kydnn/p/4789973.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!