待解析的xml文件:
<?xml version="1.0" encoding="utf-8"?> <ResInfo> <barcode/> <asset> <assetcardno>B</assetcardno> <comments/> <buydate/> <category/> </asset> <entity> <entityid>901061314</entityid> <entitycode>LTJHXYYCX01/XA-HWMACBTS529</entitycode> <entityname>蓝田局华胥电信营业厅CDMA基站/BTS529</entityname> <entityspec>BTS</entityspec> <vendorname>HuaWei</vendorname> <model>HUAWEI BTS3900</model> <installaddress/> </entity> <version/> <sectornum>3</sectornum> <rackname>HW401C</rackname> <containers> <container> <shelfhight>0.086</shelfhight> <cardinfos> <cardinfo> <cardname>290.蓝田局华胥电信营业厅CDMA基站/BTS529/架1列1/框3槽(0)HECM</cardname> </cardinfo> <cardinfo> <cardname>290.蓝田局华胥电信营业厅CDMA基站/BTS529/架1列1/框3槽(10)FAN</cardname> </cardinfo> <cardinfo> <cardname>290.蓝田局华胥电信营业厅CDMA基站/BTS529/架1列1/框3槽(9)UPEU</cardname> </cardinfo> <cardinfo> <cardname>290.蓝田局华胥电信营业厅CDMA基站/BTS529/架1列1/框3槽(7)CMPT</cardname> </cardinfo> <cardinfo> <cardname>290.蓝田局华胥电信营业厅CDMA基站/BTS529/架1列1/框3槽(2)HCPM</cardname> </cardinfo> </cardinfos> </container> <container> <shelfhight>0.308</shelfhight> <cardinfos> <cardinfo> <cardname>290.蓝田局华胥电信营业厅CDMA基站/BTS529/架1列1/框1槽(2)CRFU</cardname> </cardinfo> <cardinfo> <cardname>290.蓝田局华胥电信营业厅CDMA基站/BTS529/架1列1/框1槽(4)CRFU</cardname> </cardinfo> <cardinfo> <cardname>290.蓝田局华胥电信营业厅CDMA基站/BTS529/架1列1/框1槽(0)CRFU</cardname> </cardinfo> </cardinfos> </container> <container> <shelfhight>0.086</shelfhight> </container> <container> <shelfhight>0.044</shelfhight> </container> </containers> <ReturnResult>0</ReturnResult> <ReturnInfo>成功</ReturnInfo> </ResInfo>
j用dom4j解析该xml文件
需要导入的jar包: import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; //dom4j解析xml //创建SAXReader对象 reader SAXReader reader = new SAXReader(); //因为我项目中的xml文件 是查出来的,类型是字符串类型,所以我们需要把字符串转换成xml文件 String resultxml = broadBandResult.getReviceXML();//我拿到的字符串类型的返回报文 Document document = DocumentHelper.parseText(resultxml); //将字符串转换成xml,具体转换 可以百度搜索 dom4j字符串转xml //获取文件的根节点 Element root = document.getRootElement(); String node1Name = "需展示的节点名称"; //得到节点的值 /ResInfo/asset/assetcardno为需要得到哪个节点的路径 //这里我要得到assetcardno节点的值
String node1text = root.selectSingleNode("/ResInfo/asset/assetcardno").getText();String node2Name = "描述";
String node2text = root.selectSingleNode("/ResInfo/asset/comments").getText();
//因为项目需要拼接html ,这一部分大家可以略过不看
desc.append("<div class="mui-input-row" style="-webkit-user-select:text;height:30%"><label>"+node1Name+"</label><label name='"+node1text+"' id='"+node1text+"1' style=" 60%;">"+node1text+"</label></div>")
.append("<div class="mui-input-row" style="-webkit-user-select:text;height:30%"><label>"+node2Name+"</label><label name='"+node2text+"' id='"+node2text+"1' style=" 60%;">"+node2text+"</label></div>")
//得到containers节点
Node node1 = root.selectSingleNode("/ResInfo/containers");
//得到containers节点下边的子节点container,因为有多个container,所以用list集合接收
List<Element> list = ((Element) node1).elements("container");
//循环这个集合
for (Element element : list) {
String node13Name = "机框安装高度";
//得到container节点里面的shelfhight节点
Element node13 =element.element("shelfhight");
//得到shelfhight节点的值
String node13Text = node13.getText();
//拼接html
desc.append("<li class="mui-table-view-cell" ><div style=" 80%;float: left;"></div><br/>")
.append("<div><label>"+node13Name+"</label><label name='"+node13Text+"' id='"+node13Text+"1' style=" 60%;">"+node13Text+"</label></div><br/>");
//得到container节点里面的cardinfos
Element node14 =(Element) element.selectSingleNode("cardinfos");
if(node14 != null){
logger.error("===node14==="+node14);
//得到cardinfos节点里面的cardinfo节点,因为cardinfos节点里面有多个cardinfo节点,所以用List集合接收
List<Element> childList2 =node14.elements("cardinfo");
logger.error("===childList2集合长度==="+childList2.size());
//循环childList2 集合
for (Element element2 : childList2) {
String node15Name = "机框下的板卡拼装名称列表";
//得到cardinfo节点里面的cardname节点
Element node15 =element2.element("cardname");
//得到cardname节点的值
String node15Text = node15.getText();
// 拼接html
desc.append("<div><label name='"+node15Text+"' id='"+node15Text+"1' style="60%;">"+node15Text+"</label></div><br/>");
}
}
desc.append("</li>");
}
这样就解析完了一个xml文件。。。。这只是我记录工作中碰到的问题,欢迎各位大神予以纠正指错