• 使用jaxp对比xml进行DOM解析


    /*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); } } }
  • 相关阅读:
    [2019.2.24]BZOJ4591 [Shoi2015]超能粒子炮·改
    [2019.2.13]BZOJ4318 OSU!
    [2019.1.17]BZOJ1066 [SCOI2007]蜥蜴
    [2019.1.15]BZOJ2152 聪聪可可
    [2019.1.17]BZOJ3993 [SDOI2015]星际战争
    [2019.1.14]BZOJ2005 [Noi2010]能量采集
    [2019.1.10]BZOJ1853 [Scoi2010]幸运数字
    [2019.1.9]BZOJ2299 [HAOI2011]向量
    yyb博客的几道神仙题
    CQOI2018异或序列 [莫队]
  • 原文地址:https://www.cnblogs.com/kydnn/p/4789973.html
Copyright © 2020-2023  润新知