首先要解析string类型的xml必须先要将其转化成dom格式,然后再进行解析取值,如下是解析代码示例:
- package com.company;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- /**
- * 解析由String类型拼接的XML格式
- * @author Administrator
- */
- public class Test6 {
- public static void main(String[] args) throws DocumentException {
- String xml="<?xml version= '1.0' encoding='gb2312'?><weighData><weighTime>2012-12-2 12:23:12</weighTime><cardNum>2</cardNum><cfid>123</cfid></weighData>";
- Document dom=DocumentHelper.parseText(xml);
- Element root=dom.getRootElement();
- String weighTime=root.element("weighTime").getText();
- String cardNum=root.element("cardNum").getText();
- String cfid=root.element("cfid").getText();
- System.out.println(weighTime);
- System.out.println(cardNum);
- System.out.println(cfid);
- }
- }