dom4j解析xml字符串实例-01
来源:互联网 发布:国际刑警 知乎 编辑:程序博客网 时间:2024/05/22 01:36
dom4j解析xml例子:
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
public class test {
/**
* 取单个节点文本
* @param doc
* @param tag
* @return
*/
public static String getText(Document doc, String tag) {
Node node = doc.selectSingleNode("//" + tag);
if (node == null) {
return null;
}
if (node.getText() == null) {
return null;
}
String res = node.getText().trim();
return res;
}
/**
* 解析通用响应头
*
* @param StrResponse
* @return
* @throws Exception
*/
public static void parseCommonMsgResponseHeader(Document doc) throws Exception {
System.out.println("name:"+getText(doc, "name"));
System.out.println("version:"+getText(doc, "version"));
System.out.println("times:"+getText(doc, "times"));
}
/**
* 解析方法
* @param strReponse
* @throws Exception
*/
public static void parse(String strReponse) throws Exception {
Document doc = DocumentHelper.parseText(strReponse);
System.out.println("doc:"+doc.asXML());//查询转换后的xml
test.parseCommonMsgResponseHeader(doc);
}
/**
* 测试类
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String responseXml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<test>" +
"<name>userloginresp</name>" +
"<version>1.0.0</version>" +
"<times>20120728234226</times>" +
"</test>";
test.parse(responseXml);
}
}
- dom4j解析xml字符串实例-01
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析XML字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- dom4j解析xml字符串实例
- 关于toad中文识别的问题
- 对象序列化
- StarUML使用说明-指导手册
- (3)查 询 转 换
- BuildSystem(gyp&scons)
- dom4j解析xml字符串实例-01
- 休闲驿站
- android 控件的隐藏和显示
- 项目实践之Singleton模式
- Ajax学习笔记(二)——jQuery(1)
- 【DB】设计原则
- 用Android访问本地站点---(localhost,10.0.2.2)要区别
- [12月21日的脚本] 在会话中找出最近的邮件项并移动(VBA)
- C语言中的头文件