javadom4jcdata_java⽤dom4j解析带有cdata的xml报⽂
java⽤dom4j解析带有cdata的xml报⽂
背景
解决⽅法
按照节点格式⽣成对应的实体类
解析类
注意
使⽤xstream需要引⼊3个jar包
总结
背景
前两天在⼯作中,调⽤外部的webservice接⼝,发现对⽅的返回报⽂格式与常见的不同,在解析中也⼀直有问题,遂记录下来。
报⽂格式
1
success
张三
男
18
80
20
]]>
思路
可以发现在整个xmj报⽂的中间,有⼀部分数据⽤包裹起来,导致我们不能⽤以前的⽅法来解析,需要先去掉xml报⽂头,定位到return节点,再处理cdata中的数据。
解决⽅法
按照节点格式⽣成对应的实体类
先⽣成实体类,需要lombok
Response.java
import lombok.*;
/**
* @Author: xs
* @Description:java⽤dom4j解析带有cdata的xml报⽂
* @Date:Create:in 2020/6/28 15:48
* @Modified By:
@Builder
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor
public class Response {
private ResponseBody responsebody;
}
ResponseBody.java
import lombok.*;
/**
* @Author: xs
* @Description:
* @Date:Create:in 2020/6/28 15:49
* @Modified By:
*/
@Builder
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor
public class ResponseBody {
private RespInfo respInfo;
}
RespInfo.java
import lombok.*;
import java.util.List;
/
**
* @Author: xs
* @Description:
* @Date:Create:in 2020/6/28 15:52
* @Modified By:
*/
@Builder
@Data
@NoArgsConstructor
public class RespInfo {
private String resultCode;
private String resultMsg;
private List users;
}
Users.java
import lombok.*;
import java.util.List;
/**
* @Author: xs
* @Description:
* @Date:Create:in 2020/6/28 15:53
* @Modified By:
*/
@Builder
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor
public class Users {
private String name;
private String sex;
private String age;
private List stuends;
}
Stuends.java
import lombok.*;
/**
* @Author: xs
* @Description:
* @Date:Create:in 2020/6/28 15:55
* @Modified By:
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor
public class Stuends {
private String score;
private String height;
}
解析类
testXml.java
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.l.DomDriver; import org.dom4j.Document;
import org.dom4j.DocumentHelper;
/**
* @Author: xs
* @Description:
* @Date:Create:in 2020/6/28 15:56
* @Modified By:
*/
public class testXml {
public static void main(String[] args) {
String str = "\n" +
"\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 1\n" +
" success\n" +
" \n" +
" \n" +
" 张三\n" +
" \n" +
" \n" +
" 80\n" +
" 20\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"]]>\n" +
" \n" +
" \n" +
"";
Response response = test(str);
String name = Responsebody().getRespInfo().getUsers().get(0).getName();
System.out.println("name: "+name);
}
public static Response test(String xml){
Response response = new Response();
try{
Document document = DocumentHelper.parseText(xml);
String returnStr = RootElement().element("Body").element("result").element("return").getText();使用dom4j解析xml文件
// 此处初始化XStream加了new DomDriver(),因为缺少xpp3_min的jar包,如果有这个jar不加new DomDriver()也可以XStream xStream = new XStream(new DomDriver());
xStream.alias("Response",Response.class);
xStream.alias("responsebody",ResponseBody.class);
xStream.alias("respInfo",RespInfo.class);
xStream.alias("user",Users.class);
xStream.alias("stuend",Stuends.class);
Document document1 = DocumentHelper.parseText(returnStr);
String returnXml = RootElement().asXML();
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论