A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© li745547 初级黑马   /  2018-3-25 14:12  /  1033 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

四、利用Java代码解析XML文档1、解析方式

l  DOM:Document Object Model,文档对象模型。这种方式是W3C推荐的处理XML的一种标准方式。

缺点:必须读取整个XML文档,才能构建DOM模型,如果XML文档过大,造成资源的浪费。

优点:适合对XML中的数据进行操作(CRUD)。

l  SAX:Simple API for XML。这种方式不是官方标准,属于开源社区XML-DEV,几乎所有的XML解析器都支持它。


2、解析工具

JAXP:

DOM或SAX方式进行解析XML。API在JDK之中。

Dom4J:(推荐)

是开源组织推出的解析开发包。(牛,大家都在用,包括SUN公司的一些技术的实现都在用)

五、JAXP进行DOM方式解析XML基本练习1、JAXP简介:

开发包:(JDK中)

DOM:W3C。org.w3c.dom.*   DOM规范。(接口/抽象类)

SAX:开源组织。org.xml.sax.*  SAX规范。(接口/抽象类)

JAXP:javax.xml.*

2、利用JAXP进行DOM方式解析[java] view plain copy


  • //JAXP进行DOM方式解析的基本操作  
  • public class JaxpDemo1 {  
  •   
  •     public static void main(String[] args) throws Exception {  
  •         //得到解析器  
  •         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
  •         DocumentBuilder builder = factory.newDocumentBuilder();  
  •         //通过解析器就可以得到代表整个内存中XML的Document对象  
  •         Document document = builder.parse("src/book.xml");  
  •         test8(document);  
  •     }  
  • //  1、得到某个具体的节点内容:  刘丰  
  •     private static void test1(Document document){  
  •         NodeList nl = document.getElementsByTagName("作者");  
  •         Node authorNode = nl.item(0);  
  •         System.out.println(authorNode.getTextContent());  
  •     }  
  • //  2、遍历所有元素节点:打印元素的名称  
  •     private static void test2(Node node){  
  •         //确定node的类型  
  •         //方式一  
  • //      if(node.getNodeType()==Node.ELEMENT_NODE){  
  • //          //是元素  
  • //      }  
  •         //方式二  
  •         if(node instanceof Element){  
  •             //是元素  
  •             Element e = (Element)node;  
  •             System.out.println(e.getNodeName());//打印元素名称  
  •         }  
  •         //判断有没有子节点  
  •         NodeList nl = node.getChildNodes();  
  •         for(int i=0;i<nl.getLength();i++){  
  •             Node n = nl.item(i);  
  •             test2(n);  
  •         }  
  •     }  
  • //  3、修改某个元素节点的主体内容:<售价>39.00元</售价>--->10元  
  •     private static void test3(Document document) throws Exception{  
  •         //得到售价  
  •         Node priceNode = document.getElementsByTagName("售价").item(0);  
  •         priceNode.setTextContent("10元");  
  •         //更新XML文件  
  •         TransformerFactory tf = TransformerFactory.newInstance();  
  •         Transformer t = tf.newTransformer();  
  •         //构建输入源:  
  •         Source source = new DOMSource(document);  
  •         //构建目标:  
  •         Result result = new StreamResult("src/book.xml");  
  •          
  •         t.transform(source, result);  
  •     }  
  •       
  • //  4、向指定元素节点中增加子元素节点:第一本书添加子元素 <出版社>黑马程序员</出版社>  
  •     private static void test4(Document document) throws Exception{  
  •         //创建:<出版社>黑马程序员</出版社>  
  •         Element e = document.createElement("出版社");  
  •         e.setTextContent("黑马程序员");  
  •         //得到书,把新节点挂上去  
  •         Node bookNode = document.getElementsByTagName("书").item(0);  
  •         bookNode.appendChild(e);  
  •         //更新XML文件  
  •         TransformerFactory tf = TransformerFactory.newInstance();  
  •         Transformer t = tf.newTransformer();  
  •         //构建输入源:  
  •         Source source = new DOMSource(document);  
  •         //构建目标:  
  •         Result result = new StreamResult("src/book.xml");  
  •          
  •         t.transform(source, result);  
  •     }  
  • //  5、向指定元素节点上增加同级元素节点:第一本书<售价>前面添加<批发价>30</批发价>  
  •     private static void test5(Document document) throws Exception{  
  •         //创建新节点  
  •         Element e = document.createElement("批发价");  
  •         e.setTextContent("30元");  
  •         //找到<售价>  
  •         Node priceNode = document.getElementsByTagName("售价").item(0);  
  •         //父标签:调用insertBefore(新节点,参考节点);  
  •          
  •         Node bookNode = priceNode.getParentNode();  
  •         bookNode.insertBefore(e, priceNode);  
  •         //更新XML文件  
  •         TransformerFactory tf = TransformerFactory.newInstance();  
  •         Transformer t = tf.newTransformer();  
  •         //构建输入源:  
  •         Source source = new DOMSource(document);  
  •         //构建目标:  
  •         Result result = new StreamResult("src/book.xml");  
  •          
  •         t.transform(source, result);  
  •     }  
  • //  6、删除指定元素节点:删除批发价  
  •     private static void test6(Document document) throws Exception{  
  •         Node priceNode = document.getElementsByTagName("批发价").item(0);  
  •         priceNode.getParentNode().removeChild(priceNode);  
  •         //更新XML文件  
  •         TransformerFactory tf = TransformerFactory.newInstance();  
  •         Transformer t = tf.newTransformer();  
  •         //构建输入源:  
  •         Source source = new DOMSource(document);  
  •         //构建目标:  
  •         Result result = new StreamResult("src/book.xml");  
  •          
  •         t.transform(source, result);  
  •     }  
  • //  7、操作XML文件属性:书籍添加一个属性:ISBN=“ABC”  
  •     private static void test7(Document document) throws Exception{  
  •         Node bookNode = document.getElementsByTagName("书").item(0);  
  •         if(bookNode instanceof Element){  
  •             Element e = (Element)bookNode;  
  •             e.setAttribute("ISBN", "ABC");  
  •         }  
  •         //更新XML文件  
  •         TransformerFactory tf = TransformerFactory.newInstance();  
  •         Transformer t = tf.newTransformer();  
  •         //构建输入源:  
  •         Source source = new DOMSource(document);  
  •         //构建目标:  
  •         Result result = new StreamResult("src/book.xml");  
  •          
  •         t.transform(source, result);  
  •     }  
  • //  8、操作XML文件属性:获取ISBN=“ABC”  
  •     private static void test8(Document document) throws Exception{  
  •         Node bookNode = document.getElementsByTagName("书").item(0);  
  •         if(bookNode instanceof Element){  
  •             Element e = (Element)bookNode;  
  •             System.out.println(e.getAttribute("ISBN"));  
  •         }  
  •     }  
  • }  




3、DOM小案例

a、建立xml文件


[java] view plain copy


  • <?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>  
  •     <student examid="222" idcard="111">  
  •         <name>刘丰</name>  
  •         <location>湖北</location>  
  •         <grade>100</grade>  
  •     </student>  
  • <student examid="dsf" idcard="2342"><name>dsf</name><location>435</location><grade>654.0</grade></student></exam>  




b、代码要精细。要分层。


DAO:com.zhilinghui.dao

VIEW:com.zhilinghui.view

JavaBean:com.zhilinghui.domain(领域)






1 个回复

正序浏览
我来占层楼啊  
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马