js解析XML

昨天写一个接口的部分XML解析功能,用JS实现,直接在页面显示。

xmlDoc = $.parseXML(selected.FReqInfo),
$xml = $(xmlDoc),
$Order = $xml.find("Order");
//console.log($Order);
//console.log($Order.find("OrderId").text());
var strHTML = "";
var orderInfo = 'OrderId:' + $Order.find("OrderId").text() + "<br>";
var VendorProductCode = 'VendorProductCode:' + $Order.find("VendorProductCode").text() + "<br>";
var DepartureDate = 'DepartureDate:' + $Order.find("DepartureDate").text() + "<br>";
var BookingTime = 'BookingTime:' + $Order.find("OrderInfo").find("BookingTime").text() + "<br>";

strHTML += orderInfo;
strHTML += VendorProductCode;
strHTML += DepartureDate;
strHTML += BookingTime;

var PackageInfo = $Order.find("OrderInfo").find("PackageInfo");

PackageInfo.each(function () {
var $strId = $(this).children("Currency").text();

strHTML += "AdultQuantity:" + $(this).children("AdultQuantity").text() + "<br>";
strHTML += "InfantQuantity:" + $(this).children("InfantQuantity").text() + "<hr>";
strHTML += "VendorPackageCode:" + $(this).children("VendorPackageCode").text() + "<br>";
strHTML += "Currency:" + $(this).children("Currency").text() + "<br>";
strHTML += "AdultCostPrice:" + $(this).children("AdultCostPrice").text() + "<hr>";
strHTML += "ChildQuantity:" + $(this).children("ChildQuantity").text() + "<br>";
strHTML += "ChildCostPrice:" + $(this).children("ChildCostPrice").text() + "<br>";
strHTML += "ChildQuantity:" + $(this).children("ChildQuantity").text() + "<hr>";
strHTML += "SinglePersonQuantity:" + $(this).children("SinglePersonQuantity").text() + "<br>";
strHTML += "SinglePersonCostPrice:" + $(this).children("SinglePersonCostPrice").text() + "<br>";
strHTML += "SinglePersonQuantity:" + $(this).children("SinglePersonQuantity").text() + "<br>";

});

var OptionInfoList = $Order.find("OrderInfo").find("OptionInfoList");//没有节点信息
OptionInfoList.each(function () {
//var $strId = $(this).children("Currency").text();

//strHTML += "AdultQuantity:" + $(this).children("AdultQuantity").text() + "<br>";
//strHTML += "SinglePersonQuantity:" + $(this).children("SinglePersonQuantity").text() + "<br>";
//strHTML += "ChildQuantity:" + $(this).children("ChildQuantity").text() + "<hr>";
});
var TravelerList = $Order.find("OrderInfo").find("TravelerList").find("Traveler");
TravelerList.each(function () {
console.log($(this));
strHTML += "TravelerId:" + $(this).children("TravelerId").text() + "<br>";
strHTML += "ChineseName:" + $(this).children("ChineseName").text() + "<br>";
strHTML += "EnglishName:" + $(this).children("EnglishName").text() + "<hr>";
strHTML += "Gender:" + $(this).children("Gender").text() + "<hr>";
strHTML += "Nationality:" + $(this).children("Nationality").text() + "<hr>";
strHTML += "BirthDate:" + $(this).children("BirthDate").text() + "<hr>";
strHTML += "BirthCity:" + $(this).children("BirthCity").text() + "<hr>";
strHTML += "Mobile:" + $(this).children("Mobile").text() + "<hr>";
strHTML += "AgeCategory:" + $(this).children("AgeCategory").text() + "<hr>";
strHTML += "IDType:" + $(this).children("IDType").text() + "<hr>";
strHTML += "IDNumber:" + $(this).children("IDNumber").text() + "<hr>";
strHTML += "IssuedDate:" + $(this).children("IssuedDate").text() + "<hr>";
strHTML += "ExpiredDate:" + $(this).children("ExpiredDate").text() + "<hr>";
strHTML += "IsRoomShare:" + $(this).children("IsRoomShare").text() + "<hr>";
});

 

这样使用的场景是将网络传输的XML文件进行降维度显示,方便业务人员理解。

 

//Xml path
function xmlPath(inputXmlPath,xmlDoc,root){
var pathArr = (inputXmlPath,xmlDoc,root).split("/");
var xmlDoc = $.parseXML(xmlDoc);
// 在每个逗号(/)处进行分解。

 

for(item in pathArr )
{

var currentPath = pathArr [item ];
if (currentPath .length>0)
{

xmlDoc = xmlDoc.find (currentPath) ;

}

}
return xmlDoc ;
}

这里抽象出来一个比较通用的方法,很多要检查的地方没写,先留个底吧

posted @ 2017-11-03 09:17  小毛驴  阅读(730)  评论(0编辑  收藏  举报