一、嵌套多层<?xml version="1.0" encoding="UTF-8"?> <jstmbsinterface> <result code="0" description="" serverdatetime="2016-07-12 09:51:26"></result> <data encryptflag="0" encryptkey=""> <Ships count="3" batchnum="1" totalbatch="1"> <Ship> <IsCJStandard>0</IsCJStandard> <ShipTypeName>24234</ShipTypeName> <InspectDate>2014-3-20 0:00:00</InspectDate> <Comments><![CDATA[]]> </Comments> <ShipTeuOne>0.00</ShipTeuOne> <LoadTonOne>43.00</LoadTonOne> <HostNumOne>1</HostNumOne> <SafeManageNum></SafeManageNum> <InitialRegNo>4243</InitialRegNo> <ShipIdNo></ShipIdNo> <ShipIdNoDate></ShipIdNoDate> <StockProportion>54.00</StockProportion> <EntLicenseNo></EntLicenseNo> <ShipId>301003221008213841</ShipId> <CancelTime></CancelTime> <ValidDate>2015-3-20 0:00:00</ValidDate> <RegCertNum>2342342</RegCertNum> <TotalLength></TotalLength> <ShipName>966</ShipName> <ArchiveNum>01319</ArchiveNum> <ShipEnName></ShipEnName> <ShipSurveyNum>2003Q2106441</ShipSurveyNum> <ShipRegNum>270510000022</ShipRegNum> <ShipType>0203</ShipType> <TransType>2</TransType> <Runner>运输有限公司</Runner> <BeforeName></BeforeName> <NationCertNum>270510000022</NationCertNum> <LeaseCretNum></LeaseCretNum> <TempNationCertNum>41234</TempNationCertNum> <ChargeShipType>0</ChargeShipType> <Manager></Manager> <ShipOwner>缺1</ShipOwner> <Material>01</Material> <BuildDate>2003-12-10 0:00:00</BuildDate> <ManagerLicenseNum>交长苏XK1726</ManagerLicenseNum> <ShipValue>0.00</ShipValue> <RebuildDate></RebuildDate> <ShipPort>无锡</ShipPort> <ShipPortCode>3225</ShipPortCode> <ShipNationality></ShipNationality> <BuildCompany>造船厂</BuildCompany> <GrossTon>97.00</GrossTon> <NetTon>54.00</NetTon> <BuildSite>000</BuildSite> <Draft>0.00</Draft> <LoadTon>150.00</LoadTon> <ShipTeu></ShipTeu> <GoodsArea></GoodsArea> <Vehicles>0</Vehicles> <Passengers>0</Passengers> <RatingSpeed>0.00</RatingSpeed> <HostNum>1</HostNum> <MainFrame>134.00</MainFrame> <IsStandard>1</IsStandard> <ShipLength>28.60</ShipLength> <ShipTypeWidth>6.18</ShipTypeWidth> <ShipTypeDepth>2.00</ShipTypeDepth> <IsSailingJh>1</IsSailingJh> <TranLicenseNum>0159</TranLicenseNum> <VerifyCode>53822</VerifyCode> <LicenseFirstDate>2012-04-16 00:00:00</LicenseFirstDate> <LicenseStartDate>2015-08-28 00:00:00</LicenseStartDate> <LicenseEndDate>2018-08-27 00:00:00</LicenseEndDate> <LicenseIssueDate>2015-8-28 16:47:52</LicenseIssueDate> <IssueOrgName>运输管理局</IssueOrgName> <OrgId>6872b6ce14b042da8127ba6fde4ddb97</OrgId> <OwnerId>300704281011306626</OwnerId> <BusiScopeNames>普通货船运输</BusiScopeNames> <BusiScopeName>普通货船运输</BusiScopeName> <BusiScopeCodes>0102010201</BusiScopeCodes> <AreaCode>3265100</AreaCode> <CertState>1</CertState> <OperState>10</OperState> <CaptainName></CaptainName> <ShipWidth>6.18</ShipWidth> <ShipHeight>2.00</ShipHeight> <TotalLoadTon></TotalLoadTon> <IsInlRiverStandard>0</IsInlRiverStandard> </Ship> <Ship> <IsCJStandard>0</IsCJStandard> <ShipTypeName>341</ShipTypeName> <InspectDate></InspectDate> <Comments><![CDATA[]]> </Comments> <ShipTeuOne>0.00</ShipTeuOne> <LoadTonOne>122.00</LoadTonOne> <HostNumOne>2</HostNumOne> <SafeManageNum></SafeManageNum> <InitialRegNo>1111</InitialRegNo> <ShipIdNo>231312</ShipIdNo> <ShipIdNoDate>2015-8-28 0:00:00</ShipIdNoDate> <StockProportion>54.00</StockProportion> <EntLicenseNo></EntLicenseNo> <ShipId>300302191055102943</ShipId> <CancelTime></CancelTime> <ValidDate></ValidDate> <RegCertNum>21111</RegCertNum> <TotalLength></TotalLength> <ShipName>太湖</ShipName> <ArchiveNum>08028</ArchiveNum> <ShipEnName></ShipEnName> <ShipSurveyNum>211155364273</ShipSurveyNum> <ShipRegNum>2123</ShipRegNum> <ShipType>0101</ShipType> <TransType>1</TransType> <Runner>游游船公司</Runner> <BeforeName></BeforeName> <NationCertNum>2123</NationCertNum> <LeaseCretNum>1111</LeaseCretNum> <TempNationCertNum>22222</TempNationCertNum> <ChargeShipType>0</ChargeShipType> <Manager></Manager> <ShipOwner>缺</ShipOwner> <Material>01</Material> <BuildDate>1980-4-1 0:00:00</BuildDate> <ManagerLicenseNum>K0745号</ManagerLicenseNum> <ShipValue>0.00</ShipValue> <RebuildDate>2015-8-28 0:00:00</RebuildDate> <ShipPort>无锡</ShipPort> <ShipPortCode>3225</ShipPortCode> <ShipNationality></ShipNationality> <BuildCompany>船厂</BuildCompany> <GrossTon>153.00</GrossTon> <NetTon>31.00</NetTon> <BuildSite>FJKAK</BuildSite> <Draft>3.00</Draft> <LoadTon>0.00</LoadTon> <ShipTeu></ShipTeu> <GoodsArea>0.00</GoodsArea> <Vehicles>0</Vehicles> <Passengers>130</Passengers> <RatingSpeed>0.00</RatingSpeed> <HostNum>2</HostNum> <MainFrame>182.20</MainFrame> <IsStandard>0</IsStandard> <ShipLength>0.00</ShipLength> <ShipTypeWidth>0.00</ShipTypeWidth> <ShipTypeDepth>0.00</ShipTypeDepth> <IsSailingJh>0</IsSailingJh> <TranLicenseNum>0208003</TranLicenseNum> <VerifyCode>43407</VerifyCode> <LicenseFirstDate>2011-03-17 00:00:00</LicenseFirstDate> <LicenseStartDate>2015-08-28 00:00:00</LicenseStartDate> <LicenseEndDate>2018-08-27 00:00:00</LicenseEndDate> <LicenseIssueDate>2015-8-28 0:00:00</LicenseIssueDate> <IssueOrgName>运输管理局</IssueOrgName> <OrgId>6872b6ce14b042da8127ba6fde4ddb97</OrgId> <OwnerId>300209280855043295</OwnerId> <BusiScopeNames>旅游运输</BusiScopeNames> <BusiScopeName>通客船运输</BusiScopeName> <BusiScopeCodes>0302030101</BusiScopeCodes> <AreaCode>320100</AreaCode> <CertState>1</CertState> <OperState>10</OperState> <CaptainName></CaptainName> <ShipWidth>0.00</ShipWidth> <ShipHeight>0.00</ShipHeight> <TotalLoadTon></TotalLoadTon> <IsInlRiverStandard>0</IsInlRiverStandard> </Ship> <Ship> <IsCJStandard>0</IsCJStandard> <ShipTypeName>集装箱船</ShipTypeName> <InspectDate>2013-6-27 8:55:59</InspectDate> <Comments><![CDATA[]]> </Comments> <ShipTeuOne>50.00</ShipTeuOne> <LoadTonOne>1078.00</LoadTonOne> <HostNumOne>2</HostNumOne> <SafeManageNum></SafeManageNum> <InitialRegNo>12</InitialRegNo> <ShipIdNo></ShipIdNo> <ShipIdNoDate></ShipIdNoDate> <StockProportion>100.00</StockProportion> <EntLicenseNo></EntLicenseNo> <ShipId>301211050949527795</ShipId> <CancelTime></CancelTime> <ValidDate>2012-6-27 0:00:00</ValidDate> <RegCertNum>123</RegCertNum> <TotalLength></TotalLength> <ShipName>6号</ShipName> <ArchiveNum>01413</ArchiveNum> <ShipEnName></ShipEnName> <ShipSurveyNum>2012J2102836</ShipSurveyNum> <ShipRegNum>270512000146</ShipRegNum> <ShipType>0205</ShipType> <TransType>2</TransType> <Runner>限公司</Runner> <BeforeName></BeforeName> <NationCertNum>270512000146</NationCertNum> <LeaseCretNum></LeaseCretNum> <TempNationCertNum>123</TempNationCertNum> <ChargeShipType>0</ChargeShipType> <Manager></Manager> <ShipOwner>缺</ShipOwner> <Material>01</Material> <BuildDate>2012-8-18 0:00:00</BuildDate> <ManagerLicenseNum>2182</ManagerLicenseNum> <ShipValue>0.00</ShipValue> <RebuildDate></RebuildDate> <ShipPort>jj锡</ShipPort> <ShipPortCode>3225</ShipPortCode> <ShipNationality></ShipNationality> <BuildCompany>2312修造厂</BuildCompany> <GrossTon>734.00</GrossTon> <NetTon>477.00</NetTon> <BuildSite>江1561</BuildSite> <Draft>0.00</Draft> <LoadTon>1078.00</LoadTon> <ShipTeu>50.00</ShipTeu> <GoodsArea>1078.00</GoodsArea> <Vehicles>50</Vehicles> <Passengers>0</Passengers> <RatingSpeed>0.00</RatingSpeed> <HostNum>2</HostNum> <MainFrame>280.00</MainFrame> <IsStandard>0</IsStandard> <ShipLength>48.14</ShipLength> <ShipTypeWidth>12.50</ShipTypeWidth> <ShipTypeDepth>3.15</ShipTypeDepth> <IsSailingJh>0</IsSailingJh> <TranLicenseNum>12)0200166</TranLicenseNum> <VerifyCode>63933</VerifyCode> <LicenseFirstDate>2012-11-05 00:00:00</LicenseFirstDate> <LicenseStartDate>2014-10-14 00:00:00</LicenseStartDate> <LicenseEndDate>2014-10-31 00:00:00</LicenseEndDate> <LicenseIssueDate>2014-10-14 0:00:00</LicenseIssueDate> <IssueOrgName>033运输管理处</IssueOrgName> <OrgId>6872b6ce14b042da8127ba6fde4ddb97</OrgId> <OwnerId>301006171506256528</OwnerId> <BusiScopeNames>156146货船运输</BusiScopeNames> <BusiScopeName>0213023通货船运输</BusiScopeName> <BusiScopeCodes>0102010201</BusiScopeCodes> <AreaCode>320200</AreaCode> <CertState>1</CertState> <OperState>10</OperState> <CaptainName></CaptainName> <ShipWidth>12.50</ShipWidth> <ShipHeight>3.15</ShipHeight> <TotalLoadTon></TotalLoadTon> <IsInlRiverStandard>0</IsInlRiverStandard> </Ship> </Ships> </data> </jstmbsinterface>
解析方式:
import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.*; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class AllpagesParse extends Model<AllpagesParse> { static String ShipName; static String ShipEnName; static String ShipIdNo; static String Runner; static String EntLicenseNo; static String ShipOwner; static String BuildDate; static String ShipPort; static String GrossTon; static String NetTon; static String Draft; static String LoadTonOne; static String ShipTeuOne; static String Passengers; static String RatingSpeed; static String HostNumOne; static String MainFrame; static String TotalLength; static String ShipLength; static String ShipWidth; static String ShipTypeDepth; static String TransType; static String TranLicenseNum; static String LicenseStartDate; static String LicenseEndDate; static String IssueOrgName; static String BusiScopeName; static String CertState; static String ShipType; public static String updatetime; public static String batchnum ; public static String totalbatch; public static String fStr; public static void parseXML(String respXML) throws DocumentException, MalformedURLException{ SAXReader reader = new SAXReader(); fStr = respXML.replaceAll(" ", ""); Document doc = DocumentHelper.parseText(fStr); Element root=doc.getRootElement();//获取根节点 @SuppressWarnings("rawtypes") List list = root.elements("data"); List listresult = root.elements("result"); for(Object objresult:listresult){ Element result = (Element) objresult; updatetime = result.attributeValue("serverdatetime"); } for (Object obj: list) { Element shipElement = (Element) obj; parseShipElement(shipElement); } } private static void parseShipElement(Element shipElement) { @SuppressWarnings("rawtypes") List list = shipElement.elements(); for (Object obj: list) { Element Ships = (Element) obj; String key = Ships.getName(); batchnum = Ships.attributeValue("batchnum"); totalbatch = Ships.attributeValue("totalbatch"); System.out.println("\n"+"Ships_batchnum:"+"\t"+batchnum+"\n"); //当前页数 System.out.println("\n"+"Ships_totalbatch:"+totalbatch+"\n"); //总页数 String value = Ships.getTextTrim(); @SuppressWarnings("rawtypes") java.util.Iterator Ship = Ships.elementIterator("Ship"); while(Ship.hasNext()){ Element tableItem = (Element) Ship.next(); ShipName = tableItem.elementTextTrim("ShipName"); BuildDate = tableItem.elementTextTrim("BuildDate"); CertState = tableItem.elementTextTrim("CertState"); //1:有效 2:注销 ShipPort = tableItem.elementTextTrim("ShipPort"); Draft = tableItem.elementTextTrim("Draft"); RatingSpeed = tableItem.elementTextTrim("RatingSpeed"); ShipLength = tableItem.elementTextTrim("ShipLength"); ShipWidth = tableItem.elementTextTrim("ShipWidth"); TransType = tableItem.elementTextTrim("TransType"); TranLicenseNum = tableItem.elementTextTrim("TranLicenseNum"); LicenseStartDate = tableItem.elementTextTrim("LicenseStartDate"); LicenseEndDate = tableItem.elementTextTrim("LicenseEndDate"); IssueOrgName = tableItem.elementTextTrim("IssueOrgName"); ShipEnName = tableItem.elementTextTrim("ShipEnName"); ShipIdNo = tableItem.elementTextTrim("ShipIdNo"); Runner = tableItem.elementTextTrim("Runner"); EntLicenseNo = tableItem.elementTextTrim("EntLicenseNo"); ShipOwner = tableItem.elementTextTrim("ShipOwner"); GrossTon = tableItem.elementTextTrim("GrossTon"); NetTon = tableItem.elementTextTrim("NetTon"); LoadTonOne = tableItem.elementTextTrim("LoadTonOne"); ShipTeuOne = tableItem.elementTextTrim("ShipTeuOne"); Passengers = tableItem.elementTextTrim("Passengers"); HostNumOne = tableItem.elementTextTrim("HostNumOne"); MainFrame = tableItem.elementTextTrim("MainFrame"); TotalLength = tableItem.elementTextTrim("TotalLength"); ShipTypeDepth = tableItem.elementTextTrim("ShipTypeDepth"); BusiScopeName = tableItem.elementTextTrim("BusiScopeName"); ShipType= tableItem.elementTextTrim("ShipType"); } } }
二、嵌套一层
<?xml version="1.0" encoding="UTF-8"?> <response id=”1” cmd=”clone”> <src_host_name>192.168.4.205</src_host_name> <src_physics_host_name>192.168.4.206</src_physics_host_name> <result>1</result> <error_code>100</error_code> </response>
解析方式:
String dataGot = "xml"; Document doc = DocumentHelper.parseText(dataGot); Element root=doc.getRootElement();//获取根节点 String host_name = root.elementTextTrim("src_host_name"); String result = root.elementTextTrim("result"); String error_code = root.elementTextTrim("error_code");
三、嵌套两层
<?xml version="1.0" encoding="UTF-8" ?> <Response> <MO> <Mobile>16357164589</Mobile> <Message>哈哈哈哈哈哈哈哈哈哈哈</Message> <ReceiveTime>2012-01-12 15:23:12</ReceiveTime> <ExtendCode>扩展号</ExtendCode> <MsgID>1100</MsgID> </MO> <MO> <Mobile>15957166591</Mobile> <Message>我了</Message> <ReceiveTime>2012-01-12 15:23:12</ReceiveTime> <ExtendCode>扩展号</ExtendCode> <MsgID>1101</MsgID> </MO> </Response>
解析方式:
Document doc = DocumentHelper.parseText(xml); Element Response = doc.getRootElement(); Iterator<?> Report = Response.elementIterator("MO"); while (Report.hasNext()) { Element tableItem = (Element) Report.next(); // String msgID = tableItem.elementTextTrim("MsgID"); String mobile = tableItem.elementTextTrim("Mobile"); String message = tableItem.elementTextTrim("Message"); String receiveTime = tableItem.elementTextTrim("ReceiveTime"); log.info("SmsReply>>>>>>>>>>mobile:" + mobile + ",message:" + message+",receiveTime:"+receiveTime); }
来源:https://www.cnblogs.com/thiaoqueen/p/7046017.html