首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JDOM进行Xml命名空间解析

使用JDOM进行Xml命名空间解析
EN

Stack Overflow用户
提问于 2013-04-26 00:13:13
回答 1查看 177关注 0票数 0

我正在尝试使用JDOM读取以下XML字符串响应,但不知道如何解析?你能帮帮我吗?我正在尝试以下代码来解析:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-26 01:13:13

您需要使用特定于名称空间的getChildren()方法。您需要的命名空间是"un:vtinc:o-series:tps:6:0“

代码语言:javascript
复制
Namespace ns = Namespace.getNamespace("un:vtinc:o-series:tps:6:0");
List<?> list = rootNode.getChildren("QuotationResponse", ns);

如果您使用的是JDOM 2.x,第二行可能是:

代码语言:javascript
复制
Namespace ns = Namespace.getNamespace("un:vtinc:o-series:tps:6:0");
List<Element> list = rootNode.getChildren("QuotationResponse", ns);

你做的所有事情都可能是:

代码语言:javascript
复制
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。它将有助于类型转换。您以某种方式将属性对象放入到字符串中。这应该是不可能编译的!

代码语言:javascript
复制
String documentDate = node.getAttributeValue("documentđate")

最后,QuotationResponse不是根元素的子元素,而是S:Body...然后是VtEncelope。您需要使用正确的名称空间来访问这些名称空间。你需要让你的文档结构正确。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16219782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档