xpath解析含有命名空间别名的xml文件的示例代码

用xpath方式解析含有命名空间别名的xml文件的实例

public SDSPMainOutput parse3_3ReturnStr(String str) {
SDSPMainOutput output = new SDSPMainOutput();


String returns = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\""
+ " xmlns:ns1=\"http://mobilefeecalculation.check.csb.ideal.com\">" + "<soapenv:Body>"
+ "<ns1:SDSPMainInputResponse>"
+ "<ns1:SDSPMainOutput>"
+ "<ns1:SDTransationId>wmh2015062510008012</ns1:SDTransationId>"
+ "<ns1:error_Code>0000</ns1:error_Code>"
+ "<ns1:error_Message>成功</ns1:error_Message>" + "<ns1:SiebelMessage>"
+ "<ns1:ListOfFee ns1:TotalAmount=\"1\">"
+ "<ns1:PropertySet ns1:AdjType=\"2222\" ns1:AdjValue=\"3333\" ns1:ProdName=\"112\" ns1:ProdNeedPayFlg=\"113\" ns1:ProdNum=\"114\" ns1:RePriceFlag=\"115\" ns1:RootProdNum=\"116\" ns1:SN=\"117\" ns1:SPName=\"118\" ns1:SPNeedPayFlg=\"119\" ns1:SPNum=\"120\" ns1:TaxName=\"124\" ns1:TaxRate=\"125\" ns1:TaxSysId=\"126\" ns1:SPSource=\"123\" ns1:SPShowFlg=\"122\" ns1:SPReadOnlyFlg=\"121\" ns1:EXT_ACCT_ITEM_ID=\"7\" ns1:ORDER_ITEM_ID=\"110\" ns1:ACCT_ITEM_TYPE_ID=\"1111\" ns1:JT_TAX=\"18\" ns1:JT_TAX_RATE=\"19\" ns1:PAYMENT_METHOD_CD=\"111\" ns1:CURRENCY=\"1666\" ns1:ATTR_140000020=\"14444\" ns1:ATTR_140000021=\"15555\"/>"
+ "<ns1:PropertySet ns1:AdjType=\"2222\" ns1:AdjValue=\"3333\" ns1:ProdName=\"222\" ns1:ProdNeedPayFlg=\"223\" ns1:ProdNum=\"224\" ns1:RePriceFlag=\"225\" ns1:RootProdNum=\"226\" ns1:SN=\"227\" ns1:SPName=\"228\" ns1:SPNeedPayFlg=\"229\" ns1:SPNum=\"220\" ns1:TaxName=\"224\" ns1:TaxRate=\"225\" ns1:TaxSysId=\"226\" ns1:SPSource=\"223\" ns1:SPShowFlg=\"222\" ns1:SPReadOnlyFlg=\"221\" ns1:EXT_ACCT_ITEM_ID=\"7\" ns1:ORDER_ITEM_ID=\"210\" ns1:ACCT_ITEM_TYPE_ID=\"2222\" ns1:JT_TAX=\"28\" ns1:JT_TAX_RATE=\"29\" ns1:PAYMENT_METHOD_CD=\"211\" ns1:CURRENCY=\"2666\" ns1:ATTR_140000020=\"24444\" ns1:ATTR_140000021=\"25555\"/>"
+ "</ns1:ListOfFee>" + "</ns1:SiebelMessage>"
+ "</ns1:SDSPMainOutput>" + "</ns1:SDSPMainInputResponse>" + "</soapenv:Body>"
+ "</soapenv:Envelope>";

System.out.println("3-3返回值" + returns);

if(str!=null&&!"".equals(str)){
returns = str;
}

Map map = new HashMap();
map.put("soapenv","http://mobilefeecalculation.check.csb.ideal.com");
map.put("ns1","http://mobilefeecalculation.check.csb.ideal.com");


Document document = null;
try {

document = DocumentHelper.parseText(returns);
XPath x = document.createXPath("//ns1:SDSPMainInputResponse");
x.setNamespaceURIs(map);

} catch (DocumentException e1) {
e1.printStackTrace();
}
Iterator<Node> nodeIterator = document.selectNodes("//ns1:SDTransationId").iterator();

while (nodeIterator.hasNext()) {
Node node = nodeIterator.next();
System.out.println("SDTransationId:" + node.selectSingleNode("//ns1:SDTransationId").getStringValue());
}

String tid = document.selectSingleNode("//ns1:SDTransationId").getText();
String errorCode = document.selectSingleNode("//ns1:error_Code").getStringValue();
String errorMessage = document.selectSingleNode("//ns1:error_Message").getStringValue();

SiebelMessage siebelMessage = new SiebelMessage();
Node siebelMessageNode = document.selectSingleNode("//ns1:SiebelMessage");

Node listOfFeeNode = document.selectSingleNode("//ns1:ListOfFee");

String totalAmount = document.selectSingleNode("//attribute::ns1:TotalAmount").getStringValue();

List<Node> propertySetNodes = document.selectNodes("//ns1:PropertySet");

ListOfFee listOfFee = new ListOfFee();

PropertySet[] propertySet = new PropertySet[propertySetNodes.size()];

Node property=null;
for (int i = 0; i < propertySetNodes.size(); i++) {
property = propertySetNodes.get(i);

propertySet[i]=new PropertySet();
propertySet[i].setACCT_ITEM_TYPE_ID(property.selectSingleNode("attribute::ns1:ACCT_ITEM_TYPE_ID").getStringValue());

propertySet[i].setAdjType(property.selectSingleNode("attribute::ns1:AdjType").getStringValue());
propertySet[i].setAdjValue(property.selectSingleNode("attribute::ns1:AdjValue").getStringValue());
propertySet[i].setATTR_140000020(property.selectSingleNode("attribute::ns1:ATTR_140000020").getStringValue());
propertySet[i].setATTR_140000021(property.selectSingleNode("attribute::ns1:ATTR_140000021").getStringValue());

propertySet[i].setCURRENCY(property.selectSingleNode("attribute::ns1:CURRENCY").getStringValue());
propertySet[i].setEXT_ACCT_ITEM_ID(property.selectSingleNode("attribute::ns1:EXT_ACCT_ITEM_ID").getStringValue());
propertySet[i].setJT_TAX(property.selectSingleNode("attribute::ns1:JT_TAX").getStringValue());
propertySet[i].setJT_TAX_RATE(property.selectSingleNode("attribute::ns1:JT_TAX_RATE").getStringValue());
propertySet[i].setORDER_ITEM_ID(property.selectSingleNode("attribute::ns1:ORDER_ITEM_ID").getStringValue());

propertySet[i].setPAYMENT_METHOD_CD(property.selectSingleNode("attribute::ns1:PAYMENT_METHOD_CD").getStringValue());
propertySet[i].setProdName(property.selectSingleNode("attribute::ns1:ProdName").getStringValue());
propertySet[i].setProdNeedPayFlg(property.selectSingleNode("attribute::ns1:ProdNeedPayFlg").getStringValue());
propertySet[i].setProdNum(property.selectSingleNode("attribute::ns1:ProdNum").getStringValue());
propertySet[i].setRePriceFlag(property.selectSingleNode("attribute::ns1:RePriceFlag").getStringValue());

propertySet[i].setRootProdNum(property.selectSingleNode("attribute::ns1:RootProdNum").getStringValue());
propertySet[i].setSN(property.selectSingleNode("attribute::ns1:SN").getStringValue());
propertySet[i].setSPName(property.selectSingleNode("attribute::ns1:SPName").getStringValue());
propertySet[i].setSPNeedPayFlg(property.selectSingleNode("attribute::ns1:SPNeedPayFlg").getStringValue());
propertySet[i].setSPNum(property.selectSingleNode("attribute::ns1:SPNum").getStringValue());

propertySet[i].setSPReadOnlyFlg(property.selectSingleNode("attribute::ns1:SPReadOnlyFlg").getStringValue());
propertySet[i].setSPShowFlg(property.selectSingleNode("attribute::ns1:SPShowFlg").getStringValue());
propertySet[i].setSPSource(property.selectSingleNode("attribute::ns1:SPSource").getStringValue());
propertySet[i].setTaxName(property.selectSingleNode("attribute::ns1:TaxName").getStringValue());
propertySet[i].setTaxRate(property.selectSingleNode("attribute::ns1:TaxRate").getStringValue());

propertySet[i].setTaxSysId(property.selectSingleNode("attribute::ns1:TaxSysId").getStringValue());
//System.out.println(propertySet[i].getACCT_ITEM_TYPE_ID()+","+propertySet[i].getJT_TAX_RATE()+propertySet[i].getProdNum());
}

listOfFee.setPropertySet(propertySet);
listOfFee.setTotalAmount(totalAmount);

siebelMessage.setListOfFee(listOfFee);

String SDTransationId2 = document.selectSingleNode("//ns1:SDTransationId").getStringValue();

output.setError_Code(errorCode);
output.setError_Message(errorMessage);
output.setSiebelMessage(siebelMessage);

output.setSDTransationId(SDTransationId2);

return output; }

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念