标签:ext etl unit object log sys lin remove int
(查看
API文档)
首先引出要解析的xml文件:
<?xml version="1.0" encoding="UTF-8"?
><书架> <书> <书名>javaweb开发</书名> <作者 id="12">张孝祥</作者> <售价>59元</售价> </书> <书> <书名>JavaScript网页开发</书名> <作者>张孝祥</作者> <售价>28.00元</售价> </书> </书架>
下面即是对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.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.junit.Before; 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 Demo3 { /** * 利用dom完毕文档的crud * @param args * @throws Exception * @throws ParserConfigurationException * @throws Exception * @throws SAXException * @throws Exception */ private Document document; @Before public void before() throws Exception{ //获得dom解析工厂类 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //得到dom解析器 DocumentBuilder builder = factory.newDocumentBuilder(); //解析xml文件 document = builder.parse("src/book.xml"); } //遍历 @Test public void listXml() throws ParserConfigurationException, SAXException, Exception{ list(document); } public 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); //递归整个dom树 list(child); } } //读取书名节点的值:<书名>javaweb开发</书名> @Test public void test1(){ Document document = this.document; Node node = document.getElementsByTagName("书名").item(0); String value = node.getTextContent(); System.out.println(value); } //读取书名属性的值:<售价 type="rmb">39.00元</售价> @Test public void test2(){ Document document = this.document; Node node = document.getElementsByTagName("售价").item(0); Element price = (Element) node; //发现node满足不了。把node强转成对应类型 String attValue = price.getAttribute("type"); System.out.println(attValue); } //向xml文档中加入售价节点 @Test public void test3() throws Exception{ Document document = this.document; //创建要挂的节点 Element price = document.createElement("售价"); price.setTextContent("59元"); //把创建的结点挂到书节点下 Node book = document.getElementsByTagName("书").item(0); book.appendChild(price); //把内存中更新后对象树,又一次定回到xml文档中 TransformerFactory factory = TransformerFactory.newInstance(); Transformer tf = factory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //向xml文档中指定位置上加入售价节点 @Test public void test4() throws Exception{ Document document = this.document; //创建要加入的节点 Element price = document.createElement("售价"); price.setTextContent("59元"); //得到要向哪个节点上挂子节点 Node book = document.getElementsByTagName("书").item(0); //向參考节点前,挂新节点 book.insertBefore(price, document.getElementsByTagName("售价").item(0)); //把内存中更新后对象树,又一次定回到xml文档中 TransformerFactory factory = TransformerFactory.newInstance(); Transformer tf = factory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //向xml文档加入节点属性 @Test public void test5() throws Exception{ Document document = this.document; //得到要加入属性的节点 Element author = (Element) document.getElementsByTagName("作者").item(0); author.setAttribute("id", "12"); //向节点挂属性 //把内存中更新后对象树,又一次定回到xml文档中 TransformerFactory factory = TransformerFactory.newInstance(); Transformer tf = factory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //删除xml文档中的售价节点 @Test public void test6() throws Exception{ Document document = this.document; //得到要删除的节点 Node price = document.getElementsByTagName("售价").item(0); //得到要删除的节点的父亲 Node parent = document.getElementsByTagName("书").item(0); parent.removeChild(price); //把内存中更新后对象树,又一次定回到xml文档中 TransformerFactory factory = TransformerFactory.newInstance(); Transformer tf = factory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //删除2 : 删除售价节点所在的书结点 @Test public void test7() throws Exception{ Document document = this.document; //得到要删除的节点 Node price = document.getElementsByTagName("售价").item(0); price.getParentNode().getParentNode().removeChild(price.getParentNode()); //把内存中更新后对象树。又一次定回到xml文档中 TransformerFactory factory = TransformerFactory.newInstance(); Transformer tf = factory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } @Test public void test8() throws Exception{ Document document = this.document; document.getElementsByTagName("售价").item(1).setTextContent("19元"); //把内存中更新后对象树。又一次定回到xml文档中 TransformerFactory factory = TransformerFactory.newInstance(); Transformer tf = factory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } }
标签:ext etl unit object log sys lin remove int
原文地址:http://www.cnblogs.com/gccbuaa/p/7091772.html