前端之家收集整理的这篇文章主要介绍了
JAXP对xml文档进行DOM解析,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package cn.itcast.xml; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; 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; //使用dom方式对xml文档进行crud public class Demo3 { @Test public void read1() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book1.xml"); NodeList list = document.getElementsByTagName("书名"); Node node = list.item(0); String content = node.getTextContent(); System.out.println(content); } @Test public void read2() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book1.xml"); //得到根节点 Node root = document.getElementsByTagName("书架").item(0); list(root); } //得到xml文档 的所有
标签 private void list(Node 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); // } if(node instanceof Element){ System.out.println(node.getNodeName()); } NodeList list = node.getChildNodes(); for(int i=0;i<list.getLength();i++){ Node child = list.item(i); list(child); } } //得到xml文档 的
标签属性的值 @Test public void read3() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book1.xml"); Element bookname = (Element) document.getElementsByTagName("书名").item(0); String value = bookname.getAttribute("name"); System.out.println(value); } //向xm文档中
添加节点,<售价>50.00元</售价> public void add() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book1.xml"); //创建节点 Element price = document.createElement("售价"); price.setTextContent("50.00元"); //把创建的节点挂到第一本书上 Element book = (Element) document.getElementsByTagName("书").item(0); book.appendChild(price); //把更新后的内存写到xml文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book1.xml"))); } //有点问题 public void add2() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book1.xml"); //创建节点 Element price = document.createElement("售价"); price.setTextContent("59.00元"); //得到参考节点 Element refNode = (Element) document.getElementsByTagName("售价").item(0); //得到要挂崽的节点 Element book = (Element) document.getElementsByTagName("书").item(0); //往book节点的制定位置插崽 book.insertBefore(book,refNode); //把更新后内存写到xml文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book1.xml"))); } //向xml文档中
添加属性<书名>Java就业培训班教程</书名>上
添加name=“xxx”
属性 public void addAttr() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book1.xml"); Element bookname = (Element) document.getElementsByTagName("书名").item(0); bookname.setAttribute("name","xxxx"); TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book1.xml"))); } public void delete() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book1.xml"); //得到要
删除的节点 Element e = (Element) document.getElementsByTagName("售价").item(0); e.getParentNode().removeChild(e); //得到要
删除的节点的爸爸 // Element book = (Element) document.getElementsByTagName("书").item(0); //爸爸再删崽 // book.removeChild(e); TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book1.xml"))); } @Test public void update() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book1.xml"); Element e = (Element) document.getElementsByTagName("售价").item(0); e.setTextContent("100元"); //把更新的内存协会到xml文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book1.xml"))); } }
原文链接:https://www.f2er.com/xml/298900.html