문제

I am trying to read following XML String response using JDOM but have no idea how to parse? can you please help me? I am trying following codes to parse:

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>
도움이 되었습니까?

해결책

You need to the use Namespace-specific getChildren() method. The Namespace you want is "un:vtinc:o-series:tps:6:0"

Namespace ns = Namespace.getNamespace("un:vtinc:o-series:tps:6:0");
List<?> list = rootNode.getChildren("QuotationResponse", ns);

If you were using JDOM 2.x, the second line could be:

Namespace ns = Namespace.getNamespace("un:vtinc:o-series:tps:6:0");
List<Element> list = rootNode.getChildren("QuotationResponse", ns);

and your whole thing could be:

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");
}

Edit: OK, you are still having problems. I see a number of things that are wrong now.

You should be using JDOM 2.0.4. It will help with the type-casting. You somehow are putting an Attribute object in to a String. That should not be possible to compile!

String documentDate = node.getAttributeValue("documentđate")

Finally, the QuotationResponse is not a child of the root element, but of the S:Body.... and then VtEncelope. You will need to access these with the right namespaces. You need to get your document structure right.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top