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

使用JAXP对XML文档进行DOM解析

时间:2014-06-28 16:07:53      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   

bubuko.com,布布扣
  1 import java.io.FileOutputStream;
  2 
  3 import javax.xml.parsers.DocumentBuilder;
  4 import javax.xml.parsers.DocumentBuilderFactory;
  5 import javax.xml.parsers.ParserConfigurationException;
  6 import javax.xml.transform.Transformer;
  7 import javax.xml.transform.TransformerFactory;
  8 import javax.xml.transform.dom.DOMSource;
  9 import javax.xml.transform.stream.StreamResult;
 10 
 11 import org.junit.Test;
 12 import org.w3c.dom.Document;
 13 import org.w3c.dom.Element;
 14 import org.w3c.dom.Node;
 15 import org.w3c.dom.NodeList;
 16 
 17 
 18 //使用dom方式对xml文档进行crud
 19 public class Demo2 {
 20     
 21     @Test
 22     public void read() throws Exception{
 23         
 24         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 25         DocumentBuilder builder = factory.newDocumentBuilder();
 26         Document document = builder.parse("src/book.xml");
 27     
 28         NodeList list = document.getElementsByTagName("书名");
 29         Node node = list.item(1);
 30         String content = node.getTextContent();
 31         System.out.println(content);
 32         
 33     }
 34     
 35     @Test
 36     public void read2() throws Exception{
 37         
 38         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 39         DocumentBuilder builder = factory.newDocumentBuilder();
 40         Document document = builder.parse("src/book.xml");
 41     
 42         //得到根节点
 43         Node root = document.getElementsByTagName("书架").item(0);
 44         list(root);
 45     }
 46 
 47     private void list(Node node) {
 48         if(node instanceof Element){
 49         System.out.println(node.getNodeName());
 50         }
 51         NodeList list = node.getChildNodes();
 52         
 53         for(int x=0;x<list.getLength()-1;x++){
 54             Node child = list.item(x);
 55             list(child);
 56             //System.out.println(child.getTextContent());
 57         }
 58         
 59     }
 60     
 61     //得到xml文档中标签属性值:<书名 name="xxxx">Java就业培训教程</书名>
 62     @Test
 63     public void read3() throws Exception{
 64         
 65         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 66         DocumentBuilder builder = factory.newDocumentBuilder();
 67         Document document = builder.parse("src/book.xml");
 68     
 69         //得到根节点
 70         Element bookname = (Element) document.getElementsByTagName("书名").item(0);
 71         String value = bookname.getAttribute("name");
 72         System.out.println(value);
 73     }
 74     
 75     //向xml文档中添加节点:<售价>59.00元</售价>
 76     @Test
 77     public void add() throws Exception{
 78         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 79         DocumentBuilder builder = factory.newDocumentBuilder();
 80         Document document = builder.parse("src/book.xml");
 81     
 82         //创建节点
 83         Element price =document.createElement("售价");
 84         price.setTextContent("59.00元");
 85         
 86         //把创建的节点挂到第一本书上
 87         Element book = (Element) document.getElementsByTagName("书").item(0);
 88         book.appendChild(price);
 89         
 90         //把更新后内存写回xml中
 91         TransformerFactory tffactory = TransformerFactory.newInstance();
 92         Transformer tf = tffactory.newTransformer();
 93         tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
 94     
 95     }
 96     
 97     //向xml文档中指定位置上添加节点:<售价>59.00元</售价>
 98     @Test
 99     public void add2() throws Exception{
100         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
101         DocumentBuilder builder = factory.newDocumentBuilder();
102         Document document = builder.parse("src/book.xml");
103     
104         //创建节点
105         Element price =document.createElement("售价");
106         price.setTextContent("59.00元");
107         
108         //得到参考节点
109         Element refNode = (Element) document.getElementsByTagName("作者").item(1);
110         
111         //得到要挂载的节点
112         Element book = (Element) document.getElementsByTagName("书").item(1);
113         
114         //往book节点的指定位置插崽
115         book.insertBefore(price, refNode);
116         
117         //把更新后内存写回xml中
118         TransformerFactory tffactory = TransformerFactory.newInstance();
119         Transformer tf = tffactory.newTransformer();
120         tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
121     
122     }
123     
124     
125     //向xml文档节点上添加属性:<书名>java就业培训教程</书名>上添加name="zero"属性
126     @Test
127     public void addAttr() throws Exception{
128         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
129         DocumentBuilder builder = factory.newDocumentBuilder();
130         Document document = builder.parse("src/book.xml");
131     
132         Element bookname = (Element) document.getElementsByTagName("书名").item(0);
133         bookname.setAttribute("name", "zero");
134         
135         //把更新后内存写回xml中
136         TransformerFactory tffactory = TransformerFactory.newInstance();
137         Transformer tf = tffactory.newTransformer();
138         tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
139     
140     }
141     
142     @Test
143     public void delete() throws Exception{
144         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
145         DocumentBuilder builder = factory.newDocumentBuilder();
146         Document document = builder.parse("src/book.xml");
147         
148         //得到要删除节点
149         Element e = (Element) document.getElementsByTagName("售价").item(1);
150         System.out.println(e);
151         //得到要删除的节点
152         Element book = (Element) document.getElementsByTagName("书").item(1);
153         System.out.println(book);
154         //父节点删子节点
155         book.removeChild(e);
156         
157         
158         //把更新后内存写回xml中
159         TransformerFactory tffactory = TransformerFactory.newInstance();
160         Transformer tf = tffactory.newTransformer();
161         tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
162     
163     
164     }
165     
166     //delete
167     @Test
168     public void delete2() throws Exception{
169         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
170         DocumentBuilder builder = factory.newDocumentBuilder();
171         Document document = builder.parse("src/book.xml");
172         
173         //得到要删除节点
174         Element e = (Element) document.getElementsByTagName("售价").item(1);
175         e.getParentNode().getParentNode().removeChild(e.getParentNode());
176         
177         
178         //把更新后内存写回xml中
179         TransformerFactory tffactory = TransformerFactory.newInstance();
180         Transformer tf = tffactory.newTransformer();
181         tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
182     
183     
184     }
185     
186     //update
187     @Test
188     public void update() throws Exception{
189         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
190         DocumentBuilder builder = factory.newDocumentBuilder();
191         Document document = builder.parse("src/book.xml");
192         
193         Element e = (Element) document.getElementsByTagName("售价").item(1);
194         e.setTextContent("168元");
195         
196         //把更新后内存写回xml中
197         TransformerFactory tffactory = TransformerFactory.newInstance();
198         Transformer tf = tffactory.newTransformer();
199         tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
200     
201     }
202 }
View Code
bubuko.com,布布扣
 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
 2     <>
 3         <书名 name="zero">Java就业培训教程</书名>
 4         <售价>59.00元</售价>
 5         <作者>张孝祥</作者>
 6     </>
 7     <>
 8         <书名 name="zero">Java就业培训教程</书名>
 9         <售价>168元</售价>
10         <作者>张孝祥</作者>
11     </>
12     
13 </书架>
View Code

 

使用JAXP对XML文档进行DOM解析,布布扣,bubuko.com

使用JAXP对XML文档进行DOM解析

标签:des   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/aineko/p/3798179.html

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