用SAXReader解析xml文档

来源:互联网 发布:淘宝女装店铺人气排名 编辑:程序博客网 时间:2024/05/29 08:10

x5config.xml里面的xml内容

<?xml version="1.0" encoding="utf-8"?>

<x5-config> 
  <business-server>http://127.0.0.1:8080/BusinessServer</business-server>  
  <login-name>system</login-name>
  <password>123456</password>

</x5-config>


java 代码

public String login() throws DocumentException, UnknownHostException {
// 从配置文件读取服务器地址和分配给第三方接口的用户
SAXReader reader = new SAXReader();
Document dom = reader.read(getClass().getResource("/").getPath() + "/../x5config.xml");

String businessServer = dom.selectSingleNode("/x5-config/business-server").getText();
String loginName = dom.selectSingleNode("/x5-config/login-name").getText();
String password = dom.selectSingleNode("/x5-config/password").getText();

// 获得本地IP地址
String localIP = java.net.InetAddress.getLocalHost().getHostAddress();
// 初始化动作引擎
ActionEngine.init(businessServer);
// 登录
String bSessionID = ActionEngine.login(loginName, ActionUtils.md5(password), localIP, null);
// 返回bSessionID
return bSessionID;
}

=======================案例2===================================

使用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. }  

 


0 0
原创粉丝点击