java解析String类型的xml

            <?xml version="1.0" encoding="UTF-8"?>
            <output>
            <message>
            <result>1</result>
            <description>正常</description>
            </message>
            <data>
                <infoList>
                <informationId>11111</informationId><informationTitle></informationTitle>
                <informationKits>10112</informationKits><informationIssuer>111</informationIssuer><informationVersion>1.00</informationVersion>
                <informationIssueTime>2022-02-23 08:22:31.0</informationIssueTime><informationSummary>44444</informationSummary>
                <informationHead>0</informationHead><informationType>1</informationType><informationCommonNum>0</informationCommonNum>
                <channelName>11111111</channelName><channelId>113466</channelId><titleColor>0</titleColor><isConf>0</isConf>
                <documentNo>null</documentNo><transmitToEzsite><![CDATA[0]]></transmitToEzsite><informationModifyTime>null</informationModifyTime>
                <orderCode>1000</orderCode><informationIssueOrg>1111111</informationIssueOrg>
                <informationIsCommend>0</informationIsCommend><channelType>0</channelType>
                </infoList>
            <pager_offset>1</pager_offset>
            <recordCount>1</recordCount>
             </data>
             </output>

 



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FilenameUtils;
import org.codehaus.xfire.client.Client;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.*;


if
(info != "" && info.length() > 0) { long start = System.currentTimeMillis(); String xml = info; SAXReader reader = new SAXReader(); org.dom4j.Document docs; /*开始解析xml格式*/ docs = reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8"))); org.dom4j.Element root = docs.getRootElement(); org.dom4j.Element element; org.dom4j.Element element2; org.dom4j.Element element3; Element element4 = root.element("data"); for (Iterator i = element4.elementIterator("infoList"); i.hasNext(); ) { element = (org.dom4j.Element) i.next(); InfoNew infoNew = new InfoNew(); System.out.println("=====" + element.elementText("informationId")); String elementText = element.elementText("informationId"); infoNew.setInformationId(elementText); infoNew.setInformationTitle(element.elementText("informationTitle")); infoNew.setInformationKits(element.elementText("informationKits")); infoNew.setInformationIssuer(element.elementText("informationIssuer")); infoNew.setInformationIssueTime(element.elementText("informationIssueTime")); infoNew.setInformationSummary(element.elementText("informationSummary")); infoNew.setInformationHead(element.elementText("informationHead")); infoNew.setInformationType(element.elementText("informationType")); infoNew.setInformationCommonNum(element.elementText("informationCommonNum")); infoNew.setChannelName(element.elementText("channelName")); infoNew.setChannelId(element.elementText("channelId")); infoNew.setTitleColor(element.elementText("titleColor")); infoNew.setIsConf(element.elementText("isConf")); infoNew.setDocumentNo(element.elementText("documentNo")); infoNew.setTransmitToEzsite(element.elementText("transmitToEzsite")); infoNew.setInformationModifyTime(element.elementText("informationModifyTime")); infoNew.setOrderCode(element.elementText("orderCode")); infoNew.setInformationIssueOrg(element.elementText("informationIssueOrg")); infoNew.setInformationIsCommend(element.elementText("informationIsCommend")); infoNew.setChannelType(element.elementText("channelType")); Iterator elementIterator = element.elementIterator("picList"); if (elementIterator != null) { for (Iterator j = element.elementIterator("picList"); j.hasNext(); ) { element2 = (org.dom4j.Element) j.next(); System.out.println("\tnum:[" + element2.elementText("picName")); /// // info/downLoad?picName=9EB8F2F682611A0583A8E74263486C27.jpg&fileName=2022031108293589098625558.jpg infoNew.setPicName(element2.elementText("picName")); infoNew.setPicSaveName(element2.elementText("picSaveName")); } } System.out.println(infoNew.toString() + "===="); }

 

posted @ 2022-03-15 14:36  向上丶  阅读(997)  评论(0编辑  收藏  举报