练习四
1、了解xml 格式以及解析方法,掌握xml与map互转的方法
2、了解json格式以及解析方法,掌握json与map互转的方法
3、新建测试方法,将以下xml格式报文转换成json格式报文
以下一个练习将会包含到xml2map、map2json,所以间接形成了xml2json。
反向转换也可以参考转换时用到的工具类和方法
interBoss.xml文件
点击查看代码
<?xml version="1.0" encoding="UTF-8" ?>
<InterBOSS>
<SvcCont><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<SvcRoot>
<Item>
<Name>IndictSeq</Name>
<Value>20210726CSVC2700000003</Value>
</Item>
<Item>
<Name>CallerNo</Name>
<Value>18271403993</Value>
</Item>
<Item>
<Name>CalledNo</Name>
<Value>10086</Value>
</Item>
<Item>
<Name>MSISDN</Name>
<Value>18271403993</Value>
</Item>
<Item>
<Name>SubsName</Name>
<Value>*树宏</Value>
</Item>
<Item>
<Name>SubsLevel</Name>
<Value>04</Value>
</Item>
<Item>
<Name>SubsBrand</Name>
<Value>01</Value>
</Item>
<Item>
<Name>SvcTypeId</Name>
<Value>10010105020101</Value>
</Item>
<Item>
<Name>UrgentId</Name>
<Value>03</Value>
</Item>
<Item>
<Name>HomeProv</Name>
<Value>270</Value>
</Item>
</SvcRoot>]]>
</SvcCont>
</InterBOSS>
实现过程Xml2Json.java
点击查看代码
public class Xml2Json {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
try {
InputStream is = new FileInputStream(new File("src/main/resources/test/interBOSS.xml"));
SAXReader sax = new SAXReader(); //创建解析器
Document doc = sax.read(is); //read方法将配置读到内存中,创建对应的Document[org.dom4j]对象树
Element root = doc.getRootElement(); //获取XML文档的根节点对象
//遍历根节点
HashMap<String, Object> rootMap = new HashMap<>();
HashMap<String, Object> SvcContMap = new HashMap<>();
HashMap<String, Object> CDATAMap = new HashMap<>();
HashMap<String, Object> SvcRootMap = new HashMap<>();
List<Map<String, Object>> itemMapList = new ArrayList<>();
Map<String, Object> interBOSSMap = new HashMap<>();
List<Map<String, Object>> svcContList = new ArrayList<>();
for (Iterator<Element> rootIter = root.elementIterator(); rootIter.hasNext(); ) {
Map<String, Object> svcContMap = new HashMap<>();
Element svcContElt = rootIter.next();
Document doc2 = DocumentHelper.parseText(svcContElt.getText());
Element root2 = doc2.getRootElement();
List<Map<String, Object>> svcRootList = new ArrayList<>();
for (Iterator<Element> svcContIter = root2.elementIterator(); svcContIter.hasNext(); ) {
Map<String, Object> svcRootMap = new HashMap<>();
Element itemEle = svcContIter.next();
Map<String, Object> itemMap = new HashMap<>();
Map<String, Object> itemSubMap = new HashMap<>();
for (Iterator<Element> itemIter = itemEle.elementIterator(); itemIter.hasNext(); ) {
Element svcRootElt = itemIter.next();
itemSubMap.put(svcRootElt.getName(), svcRootElt.getText());
}
itemMap.put("Item", itemSubMap);
svcRootList.add(itemMap);
}
if(svcRootList.size() == 1){
svcContMap.put("SvcRoot", svcRootList.get(0));
}else{
svcContMap.put("SvcRoot", svcRootList);
}
svcContList.add(svcContMap);
}
if(svcContList.size() == 1){
interBOSSMap.put("SvcCont", svcContList.get(0));
}else {
interBOSSMap.put("SvcCont", svcContList);
}
rootMap.put("InterBOSS", interBOSSMap);
System.out.println("map对象:" + rootMap.toString());
//map转换为json。从而实现xml到json的转换
String strMap2Json = JsonUtils.javaMap2Json(rootMap);
System.out.println("Json字符串:" + strMap2Json);
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行实现结果:
"InterBOSS": {
"SvcCont": {
"SvcRoot": [
{
"Item": {
"Name": "IndictSeq",
"Value": "20210726CSVC2700000003"
}
},
{
"Item": {
"Name": "CallerNo",
"Value": "*************"
}
},
{
"Item": {
"Name": "CalledNo",
"Value": "10086"
}
},
{
"Item": {
"Name": "MSISDN",
"Value": "*************"
}
},
{
"Item": {
"Name": "SubsName",
"Value": "**********************"
}
},
{
"Item": {
"Name": "SubsLevel",
"Value": "04"
}
},
{
"Item": {
"Name": "SubsBrand",
"Value": "01"
}
},
{
"Item": {
"Name": "SvcTypeId",
"Value": "10010105020101"
}
},
{
"Item": {
"Name": "UrgentId",
"Value": "03"
}
},
{
"Item": {
"Name": "HomeProv",
"Value": "270"
}
}
]
}
}
},