java SAXReader[通俗易懂]

java SAXReader[通俗易懂]使用SAXReader需要导入dom4j-full.jar包。    dom4j是一个Java的XMLAPI,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XMLAPI,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。    使用举例:1.   s.xml内容 [xhtml] …

大家好,欢迎来到IT知识分享网。

使用SAXReader需要导入dom4j-full.jar包。

     dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。

     使用举例:

1.    s.xml内容

 

[xhtml] 
view plain  
copy

 

  1. <?xml version=“1.0” encoding=“GB2312”?>  
  2. <data>  
  3.     <row queryDTO.enterpriseId=“gfd” queryDTO.loginName=“gdfg” queryDTO.state=“0”/>  
  4. </data>  

 

 

2.解析

 

[c-sharp] 
view plain  
copy

 

  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6. import org.dom4j.Document;  
  7. import org.dom4j.DocumentException;  
  8. import org.dom4j.Element;  
  9. import org.dom4j.io.SAXReader;  
  10. import org.dom4j.tree.AbstractAttribute;  
  11.   
  12. public class ReadXMLTest {  
  13.       
  14.     public static void main(String[] args){  
  15.         File xmlFile = new File(“C:/s.xml”);  
  16.         FileInputStream fis = null;  
  17.         try {  
  18.             fis = new FileInputStream(xmlFile);  
  19.         } catch (FileNotFoundException e) {  
  20.             e.printStackTrace();  
  21.             System.err.println(“File is not exsit!”);  
  22.         }  
  23.           
  24.         SAXReader saxReader = new SAXReader();  
  25.         List rowList = null;  
  26.         try {  
  27.             //生成文档对应实体  
  28.             Document doc = saxReader.read(fis);  
  29.             //获取指定路径下的元素列表,这里指获取所有的data下的row元素  
  30.             rowList = doc.selectNodes(“//data/row”);  
  31.         } catch (DocumentException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.           
  35.           
  36.         for(Iterator iter = rowList.iterator();iter.hasNext();){  
  37.             //获得具体的row元素   
  38.             Element element = (Element)iter.next();  
  39.             //获得row元素的所有属性列表  
  40.             List elementList = element.attributes();  
  41.             for(Iterator iter1 = elementList.iterator();iter1.hasNext();){  
  42.                 //将每个属性转化为一个抽象属性,然后获取其名字和值  
  43.                 AbstractAttribute aa = (AbstractAttribute)iter1.next();  
  44.                 System.out.println(“Name:”+aa.getName()+“;Value:”+aa.getValue());  
  45.             }  
  46.                             //输出:  
  47.                             //Name:queryDTO.enterpriseId;Value:gfd  
  48.                             //Name:queryDTO.loginName;Value:gdfg  
  49.                             //Name:queryDTO.state;Value:0  
  50.             System.out.println(element.getName());  
  51.                             //输出:  
  52.                             //row  
  53.             // 取得row元素的queryDTO.enterpriseId属性的值  
  54.             System.out.println(element.attributeValue(“queryDTO.enterpriseId”));  
  55.                             //输出:  
  56.                             //gfd  
  57.             //如果element下有子元素,(类似width=”**”),要想获得该子元素的值,可以用如下方法  
  58.             System.out.println(element.elementText(“width”));//因为没有,所以输出为null。  
  59.         }  
  60.           
  61.     }  
  62. }  

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/13598.html

(0)
上一篇 2023-03-24 18:00
下一篇 2023-03-25 20:00

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信