首页 > 代码库 > 对xml进行增,删,改

对xml进行增,删,改

 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?><书架> 2     <书> 3         <书名 name="xxxx">何茂赟自传</书名> 4         <作者>何茂赟</作者> 5          6    <售价>500.00</售价></书> 7     <书> 8         <书名>何茂赟自传2</书名> 9         <作者>何茂赟</作者>10         <售价>100.00元</售价>11     </书>12 </书架>
package xml.demo;import java.io.FileOutputStream;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.Source;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;public class demo {        //向xml文档中添加节点:<售价>59.00</售价>        @Test    public void add() throws Exception      {        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//创建工厂        DocumentBuilder builder=factory.newDocumentBuilder();//得到dom解析器        Document document=builder.parse("src/xml/demo/shu.xml");//解析xml文档,得到代表文档的document                Element price=document.createElement("售价");        price.setTextContent("59.00");        //把创建的节点挂到第一本书书上        Element book=(Element) document.getElementsByTagName("书").item(0);        book.appendChild(price);        TransformerFactory tffactory=TransformerFactory.newInstance();        Transformer tf=tffactory.newTransformer();        tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/xml/demo/shu.xml")));      }        //向xml文档中指定节点位置添加节点:<售价>59.00</售价>                @Test            public void add1() throws Exception              {                DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//创建工厂                DocumentBuilder builder=factory.newDocumentBuilder();//得到dom解析器                Document document=builder.parse("src/xml/demo/shu.xml");//解析xml文档,得到代表文档的document                                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(price, refNode);                //把更改的内容更新到xml文档中去                TransformerFactory tffactory=TransformerFactory.newInstance();                Transformer tf=tffactory.newTransformer();                tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/xml/demo/shu.xml")));              }                //向xml文档中插入属性: <书名>何茂赟自传</书名>上添加name="xxxx"                @Test            public void addarr() throws Exception              {                DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//创建工厂                DocumentBuilder builder=factory.newDocumentBuilder();//得到dom解析器                Document document=builder.parse("src/xml/demo/shu.xml");//解析xml文档,得到代表文档的document                                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/xml/demo/shu.xml")));              }                //向xml文档中删除节点:<售价>59.00</售价>                @Test            public void delet() throws Exception              {                DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//创建工厂                DocumentBuilder builder=factory.newDocumentBuilder();//得到dom解析器                Document document=builder.parse("src/xml/demo/shu.xml");//解析xml文档,得到代表文档的document                                //得到要删除的节点                Element e=(Element) document.getElementsByTagName("售价").item(0);                //得到要删除节点的爸爸                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/xml/demo/shu.xml")));              }                //向xml文档中删除节点:<售价>59.00</售价>                @Test            public void delet1() throws Exception              {                DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//创建工厂                DocumentBuilder builder=factory.newDocumentBuilder();//得到dom解析器                Document document=builder.parse("src/xml/demo/shu.xml");//解析xml文档,得到代表文档的document                                //得到要删除的节点                Element e=(Element) document.getElementsByTagName("售价").item(0);                e.getParentNode().removeChild(e);                //e.getParentNode()..getParentNode().removeChild(e);//这句程序可以把要删除节点的父节点给删除                                TransformerFactory tffactory=TransformerFactory.newInstance();                Transformer tf=tffactory.newTransformer();                tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/xml/demo/shu.xml")));              }                //向xml文档中更改节点:<售价>59.00</售价>,把59改成100块                @Test            public void update() throws Exception              {                DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//创建工厂                DocumentBuilder builder=factory.newDocumentBuilder();//得到dom解析器                Document document=builder.parse("src/xml/demo/shu.xml");//解析xml文档,得到代表文档的document                                //得到要删除的节点                Element e=(Element) document.getElementsByTagName("售价").item(0);                e.setTextContent("500.00");                                TransformerFactory tffactory=TransformerFactory.newInstance();                Transformer tf=tffactory.newTransformer();                tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/xml/demo/shu.xml")));              }    }

 

对xml进行增,删,改