在与其他公司,尤其是国企项目(如公安系统,银保监等系统)对接接口时,对方经常要求入参或者返回参数需要传 XML 格式报文。
请求他们接口需要自己将参数拼接成XML格式,获取他们返回参数时,需要根据接口文档进行解析,然后处理。
本文以一个例子介绍如何解析响应的XML
1.引入依赖
maven项目可以在 pom.xml 里添加
<!-- XML 解析 -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
如果非Maven项目需要引入2个jar: dom4j-1.6.1.jar 和 jaxen-1.1.6.jar
2. 解析XML demo
package com.example.demo.xml;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.util.*;
public class Demo {
public static void main(String[] args) throws DocumentException {
// XML解析demo
String xmlString = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
"<Packet type=\"RESPONSE\" version=\"1.0\">\n" +
" <Head>\n" +
" <errorCode>00000</errorCode>\n" +
" <errorMessage>成功</errorMessage>\n" +
" </Head>\n" +
" <Body>\n" +
" <ResInformation>\n" +
" <User>\n" +
" <id>1</id>\n" +
" <name>张三</name>\n" +
" <age>18</age>\n" +
" <sex>男</sex>\n" +
" </User>\n" +
" <User>\n" +
" <id>2</id>\n" +
" <name>李四</name>\n" +
" <age>20</age>\n" +
" <sex>女</sex>\n" +
" </User>\n" +
" <User>\n" +
" <id>3</id>\n" +
" <name>王五</name>\n" +
" <age>19</age>\n" +
" <sex>女</sex>\n" +
" </User>\n" +
" <School>\n" +
" <id>3</id>\n" +
" <province>广东省</province>\n" +
" <city>深圳市</city>\n" +
" <name>XXXX大学</name>\n" +
" </School>\n" +
" </ResInformation>\n" +
" </Body>\n" +
"</Packet>\n";
Document document = DocumentHelper.parseText(xmlString);
List bodyInfoList = document.selectNodes("//Packet/Body/ResInformation");
Iterator iterator = bodyInfoList.iterator();
List<Map<String, Object>> userList = new ArrayList<>();
Map<String, Object> school = new HashMap<>();
while (iterator.hasNext()) {
Element bodyInfoElement = (Element) iterator.next();
// 解析 user,针对集合
Iterator userIterator = bodyInfoElement.elementIterator("User");
while (userIterator.hasNext()) {
Map<String, Object> userMap = new HashMap<>();
Element element = (Element) userIterator.next();
userMap.put("id", getElementText(element, "id"));
userMap.put("name", getElementText(element, "name"));
userMap.put("age", getElementText(element, "age"));
userMap.put("sex", getElementText(element, "sex"));
userList.add(userMap);
}
// 解析 school,针对单个,对方文档已经说明了就是一个
Iterator schoolIterator = bodyInfoElement.elementIterator("School");
while (schoolIterator.hasNext()) {
Map<String, Object> userMap = new HashMap<>();
Element element = (Element) schoolIterator.next();
school.put("id", getElementText(element, "id"));
school.put("province", getElementText(element, "province"));
school.put("city", getElementText(element, "city"));
school.put("name", getElementText(element, "name"));
}
}
System.out.println(userList);
System.out.println(school);
}
/**
* 功能描述:获取xml元素值
*
* @param element
* @param elementIteratorName
* @return
*/
public static String getElementText(Element element, String elementIteratorName) {
Iterator iterator = element.elementIterator(elementIteratorName);
if (iterator.hasNext()) {
Element subElement = (Element) iterator.next();
return subElement.getText();
}
return null;
}
}
输出结果为:
[{sex=男, name=张三, id=1, age=18}, {sex=女, name=李四, id=2, age=20}, {sex=女, name=王五, id=3, age=19}]
{province=广东省, city=深圳市, name=XXXX大学, id=3}
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏