java saxreader 字符串_DocumentHelper 和SAXReader 解析xml 字符串

DocumentHelper 解析xml字符串

String xml=com.jetsen.platform.util.FileUtil.getFileContent(request.getSession().getServletContext().getRealPath("/WEB-INF/platform-app-interface.xml"));

org.dom4j.Document doc = org.dom4j.DocumentHelper.parseText(xml);

org.dom4j.Element stu = doc.getRootElement();

java.util.List> l=new java.util.ArrayList>();

java.util.List elist=stu.elements();

for(int i = 0 ; i < elist.size() ; i++)

{

org.dom4j.Element e=elist.get(i);

java.util.HashMap map=new java.util.HashMap();

map.put("id",e.attributeValue("id"));

map.put("beanId",e.attributeValue("beanId"));

map.put("methodName",e.attributeValue("methodName"));

map.put("paramClass",e.attributeValue("paramClass"));

map.put("example",e.getText());

l.add(map);

}

request.setAttribute("l",l);

SAXReader用法示例

/要上传的配置文件的路径

static String uploadCfgFile = "WEB-INF/conf/uploadfile.xml";

Document uploadDocument;//文档对象实体

Map uploadMap = new HashMap();

/** * 获取配置文件的绝对路径 *@param request *@return */

private String getUploadCfgFilePath(ServletRequest request){

return ((HttpServletRequest) request).getSession().getServletContext().getRealPath("uploadCfgFile");

}

** * 初始化配置文件 *@param request *@return */

private Document initUploadCfgDocument(ServletRequest request){

SAXReader saxReader = new SAXReader();

try {

//得到指定文件,以流的形式对文件进行序列化

FileInputStream fis = new FileInputStream(getUploadCfgFilePath(request));

uploadDocument = (Document) saxReader.read(fis);

} catch (FileNotFoundException e) {

logger.debug(e);

} catch (DocumentException e) {

logger.debug(e);

}

return uploadDocument;

/** * 获取配置文件 *@param request *@return */

private Document getUploadCfgDocument(ServletRequest request){

if (this.uploadDocument == null) {

this.uploadDocument = initUploadCfgDocument(request);

}

return this.uploadDocument;

}

/** * 获取某个指定ticket节点的配置属性 *@param request *@return */

private Map getUploadMap(ServletRequest request){

Document uploadDocument = getUploadCfgDocument(request); //得到XML的根节点(message)

Element root = uploadDocument.getRootElement();

Iterator tickets = root.element("body").element("ticketNotify").elementIterator(); //获取ticketNotify节点下所有的ticket节点的配置属性,并将其放到Map中

while (tickets.hasNext()) { Element ticket = (Element) tickets.next(); uploadMap.put("id", ticket.attributeValue("id"));

uploadMap.put("dealTime", ticket.attributeValue("dealTime"));

uploadMap.put("status", ticket.attributeValue("status")); } //Element.asXML方法,获得包括该标签的所有XML数据

// System.out.println(root.element("body").asXML());

return uploadMap;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解析XML文件有多种方式,其中两种常用的方法是使用SAXReader和DocumentHelper。下面分别介绍一下这两种方法的使用。 使用SAXReader解析XML字符串: 1.首先导入相关的包和类: ```java import java.io.StringReader; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; ``` 2.将XML字符串转换为输入流,并将输入流传给SAXReader对象: ```java String xmlString = "<root><person><name>John</name></person></root>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(xmlString)); ``` 3.获取根节点并遍历子节点: ```java Element root = document.getRootElement(); List<Element> personList = root.elements("person"); for (Element person : personList) { String name = person.elementText("name"); System.out.println(name); } ``` 使用DocumentHelper解析XML字符串: 1.首先导入相关的包和类: ```java import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.DocumentHelper; ``` 2.使用DocumentHelper将XML字符串转换为Document对象: ```java String xmlString = "<root><person><name>John</name></person></root>"; Document document = DocumentHelper.parseText(xmlString); ``` 3.获取根节点并遍历子节点: ```java Element root = document.getRootElement(); List<Node> personList = root.selectNodes("person"); for (Node person : personList) { String name = ((Element) person).elementText("name"); System.out.println(name); } ``` 以上就是使用SAXReader和DocumentHelper解析XML字符串的方法。需要注意的是,这里使用了dom4j库进行XML解析,因此需要先导入dom4j的相关包。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值