天天看点

org.w3c.dom。 XML解析 练习

HTML文档

  1 import javax.xml.parsers.DocumentBuilder;
  2 import javax.xml.parsers.DocumentBuilderFactory;
  3 import javax.xml.transform.Transformer;
  4 import javax.xml.transform.TransformerFactory;
  5 import javax.xml.transform.dom.DOMSource;
  6 import javax.xml.transform.stream.StreamResult;
  7 import org.w3c.dom.Document;
  8 import org.w3c.dom.Element;
  9 import org.w3c.dom.Node;
 10 import org.w3c.dom.NodeList;
 11 
 12 public class DomDemolianxi {
 13     public static void main(String[] args) {
 14         
 15         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 16         DocumentBuilder builder = null;
 17         Document doc = null;
 18         try {
 19             builder = factory.newDocumentBuilder();
 20             doc = builder.parse("src/book.xml");        
 21         //===============================================
 22 //            test1(doc); 
 23 //            test2(doc); 
 24             test3(doc); 
 25 //            test4(doc); 
 26 //            test5(doc); 
 27 //            test6(doc); 
 28 //            test7(doc); 
 29 //            test8(doc);
 30         //===============================================
 31         } catch (Exception e) {
 32             e.printStackTrace();
 33         }
 34     }
 35     
 36     // 1.获取第二本书的作者
 37     public static void test1(Document doc){
 38         NodeList nl = doc.getElementsByTagName("作者");
 39         System.out.println(nl.item(1).getTextContent());
 40     }
 41     
 42     // 2.获取全部元素的名称
 43     public static void test2(Node d){
 44         if(d.getNodeType()==Node.ELEMENT_NODE){
 45             System.out.println(d.getNodeName());
 46         }
 47         NodeList nl = d.getChildNodes();
 48         for(int i=0;i<nl.getLength();i++){
 49             Node d1 = nl.item(i);
 50             test2(d1);
 51         }
 52     }
 53     
 54     // 3.把zhd的书售价改为10元
 55     public static void test3(Document doc) throws Exception{
 56         NodeList nl = doc.getElementsByTagName("售价");
 57         nl.item(1).setTextContent("10元");
 58         //把内存中dom树写回xml
 59         TransformerFactory factory = TransformerFactory.newInstance();
 60         Transformer    transformer = factory.newTransformer();
 61         transformer.transform(new DOMSource(doc),new StreamResult("src/book.xml"));    
 62     }
 63     
 64     // 4.加一个子节点 第二本书 内部价 5角
 65     public static void test4(Document doc) throws Exception{
 66         NodeList nl = doc.getElementsByTagName("书");
 67         Node secondBook = nl.item(1);
 68         Element e = doc.createElement("内部价");
 69         e.setTextContent("7元");
 70         secondBook.appendChild(e);        
 71         //把内存中dom树写回xml
 72         TransformerFactory factory = TransformerFactory.newInstance();
 73         Transformer    transformer = factory.newTransformer();
 74         transformer.transform(new DOMSource(doc),new StreamResult("src/book.xml"));
 75     }
 76     
 77     // 5.在第二本书 售价的前面增加批发价 5元
 78     public static void test5(Document doc) throws Exception{
 79         Node son = doc.getElementsByTagName("售价").item(1);
 80         Node dad = son.getParentNode();
 81         Element e = doc.createElement("批发价");
 82         e.setTextContent("5元");
 83         dad.insertBefore(e, son);
 84         //把内存中dom树写回xml
 85         TransformerFactory factory = TransformerFactory.newInstance();
 86         Transformer    transformer = factory.newTransformer();
 87         transformer.transform(new DOMSource(doc),new StreamResult("src/book.xml"));        
 88     }
 89     
 90     // 6.删除第二本书的内部价
 91     public static void test6(Document doc) throws Exception{
 92         Node son = doc.getElementsByTagName("内部价").item(0);
 93         Node dad = son.getParentNode();
 94         dad.removeChild(son);
 95         //把内存中dom树写回xml
 96         TransformerFactory factory = TransformerFactory.newInstance();
 97         Transformer    transformer = factory.newTransformer();
 98         transformer.transform(new DOMSource(doc),new StreamResult("src/book.xml"));
 99     }
100     
101     // 7.获取第一本书的出版社
102     public static void test7(Document doc) throws Exception{
103         Element nl =(Element) doc.getElementsByTagName("书").item(0);
104         String n =nl.getAttribute("出版社");
105         System.out.println(n);
106     }
107     
108     // 8.给第二本书加上出版社(属性)
109     public static void test8(Document doc) throws Exception{
110         Element nl = (Element)doc.getElementsByTagName("书").item(1);
111         nl.setAttribute("出版社", "人民出版社");
112         //把内存中dom树写回xml
113         TransformerFactory factory = TransformerFactory.newInstance();
114         Transformer    transformer = factory.newTransformer();
115         transformer.transform(new DOMSource(doc),new StreamResult("src/book.xml"));
116     }
117     
118 
119 }


XML文档:      
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
 2     <书 id="1" 出版社="wwg日报">
 3         <书名>传奇人生</书名>
 4         <作者>wwg</作者>
 5         <售价>999.0元</售价>
 6     </书>
 7     <书 id="2" 出版社="人民出版社">
 8         <书名>论吃</书名>
 9         <作者>zhd</作者>
10         <批发价>5元</批发价>
11         <售价>10元</售价>
12         
13     </书>
14 </书架>