我正在尝试使用JDOM读取以下XML字符串响应,但不知道如何解析?你能帮帮我吗?我正在尝试以下代码来解析:
org.jdom.Element rootNode = document.getRootElement();
List<?> list = rootNode.getChildren("QuotationResponse");
for(int i = 1 ; i <= list.size() ; i++) {
Element node = (Element) list.get(i);
String documentDate = node.getAttribute("documentDate");
String transactionType = node.getAttribute("transactionType");
}XML:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><VtEnvelope
xmlns="un:vtinc:o-series:tps:6:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Login><UserName>user</UserName>
<Password>abcd</Password>
</Login>
<QuotationResponse documentDate="2011-03-24" transactionType="SALE"><Customer><Destination taxAreaId="1230000"><City>Dallas</City>
<MainDivision>TX</MainDivision>
<SubDivision>Chester</SubDivision>
<PostalCode>75038</PostalCode>
<Country>USA</Country>
</Destination>
</Customer>
<SubTotal>1000.0</SubTotal>
<Total>1060.0</Total>
<TotalTax>60.0</TotalTax>
<LineItem lineItemId="1" lineItemNumber="1" taxDate="2013-04-25"><Product productClass="product class attribute value">product code value</Product>
<Quantity>1.0</Quantity>
<FairMarketValue>1000.0</FairMarketValue>
<UnitPrice>1000.0</UnitPrice>
<ExtendedPrice>1000.0</ExtendedPrice>
<Taxes taxResult="TAXABLE" taxType="SALES" situs="DESTINATION" taxCollectedFromParty="BUYER"><Jurisdiction jurisdictionLevel="STATE" jurisdictionId="3051">Texas</Jurisdiction>
<CalculatedTax>60.0</CalculatedTax>
<EffectiveRate>0.06</EffectiveRate>
<Taxable>1000.0</Taxable>
<Imposition impositionType="General Sales and Use Tax">Sales and Use Tax</Imposition>
<TaxRuleId>121</TaxRuleId>
</Taxes>
<TotalTax>60.0</TotalTax>
</LineItem>
</QuotationResponse>
</VtEnvelope></S:Body></S:Envelope>发布于 2013-04-26 01:13:13
您需要使用特定于名称空间的getChildren()方法。您需要的命名空间是"un:vtinc:o-series:tps:6:0“
Namespace ns = Namespace.getNamespace("un:vtinc:o-series:tps:6:0");
List<?> list = rootNode.getChildren("QuotationResponse", ns);如果您使用的是JDOM 2.x,第二行可能是:
Namespace ns = Namespace.getNamespace("un:vtinc:o-series:tps:6:0");
List<Element> list = rootNode.getChildren("QuotationResponse", ns);你做的所有事情都可能是:
Namespace ns = Namespace.getNamespace("un:vtinc:o-series:tps:6:0");
for(Element node : rootNode.getChildren("QuotationResponse", ns)) {
String documentDate = node.getAttribute("documentDate");
String transactionType = node.getAttribute("transactionType");
}编辑:好的,你仍然有问题。我发现现在有很多地方是错的。
您应该使用JDOM 2.0.4。它将有助于类型转换。您以某种方式将属性对象放入到字符串中。这应该是不可能编译的!
String documentDate = node.getAttributeValue("documentđate")最后,QuotationResponse不是根元素的子元素,而是S:Body...然后是VtEncelope。您需要使用正确的名称空间来访问这些名称空间。你需要让你的文档结构正确。
https://stackoverflow.com/questions/16219782
复制相似问题