I need to parse soap reponse which can have optional nodes. Its structure like this:
queryInvoiceResponse
invoiceList
invoice - optional, unbounded; type Invoice
consignee - optional; type Consignee
address type string
name type string
tin type string
consignor - optional; type Consignor
address type string
name type string
tin type string
customerJoOpParticipants - optional;
joOpParticipant - optional, unbounded; type JoOpParticipants
productShares
share - unbounded; type ProductShare
productNumber - required; type int
quantity type decimal
tin - required; type string
customers
customer - unbounded; type Customer
statuses - optional;
status - optional, unbounded; type CustomerType - type string with restriction - enum { 'COMMITTENT', 'BROKER', 'LESSEE', 'JOINT_ACTIVITY_PARTICIPANT', 'PUBLIC_OFFICE', 'NONRESIDENT', 'INDIVIDUAL' }
address type string
certificateNum type string
certificateSeries type string
name type string
tin type string
trailer type string
deliveryTerm - optional; type DeliveryTerm
contractDate type string
contractNum type string
destination type string
exerciseWay type string
term type string
warrant type string
warrantDate type string
draftID - optional; type long
products type ProductSet
product - unbounded; type Product
additional type string
applicationNumberInCustomsUnion type string
description - required; type string
exciseAmount type decimal
exciseRate type decimal
ndsAmount - required; type decimal
ndsRate type int
priceWithTax - required; type decimal
priceWithoutTax - required; type decimal
quantity type decimal
turnoverSize - required; type decimal
unitCode type string
unitNomenclature type string
unitPrice type decimal
currencyCode - required; type string
currencyRate type decimal
totalExciseAmount - required; type decimal
totalNdsAmount - required; type decimal
totalPriceWithTax - required; type decimal
totalPriceWithoutTax - required; type decimal
totalTurnoverSize - required; type decimal
publicOffice - optional; type PublicOffice
bik type string
iik - required; type string
payPurpose - required; type string
productCode type string
relatedInvoice - optional; type RelatedInvoice
date - required; type string
num - required; type string
sellerJoOpParticipants - optional;
joOpParticipant - optional, unbounded; type JoOpParticipants
productShares
share - unbounded; type ProductShare
productNumber - required; type int
quantity type decimal
tin - required; type string
sellers
seller - unbounded; type Seller
statuses - optional;
status - optional, unbounded; type SellerType - type string with restriction - enum { 'COMMITTENT', 'BROKER', 'FORWARDER', 'LESSOR', 'JOINT_ACTIVITY_PARTICIPANT', 'EXPORTER' }
address type string
bank type string
bik type string
certificateNum type string
certificateSeries type string
deliveryDocDate type string
deliveryDocNum type string
iik type string
kbe type string
name type string
tin - required; type string
trailer type string
signature type string
addInf type string
cancelReason type string
certificate type string
date - required; type string
deliveryDate type dateTime
id type long
inputDate type dateTime
invoiceType - required; type InvoiceType - type string with restriction - enum { 'ORDINARY_INVOICE', 'FIXED_INVOICE', 'ADDITIONAL' }
registrationNumber type string
lastUpdateDate type dateTime
num - required; type string
operatorFullname - required; type string
signatureType - required; type SignatureType - type string with restriction - enum { 'COMPANY', 'OPERATOR' }
signatureValid type boolean
state type InvoiceStateType - type string with restriction - enum { 'ACCEPTED', 'DECLINED' }
status type InvoiceStatusType - type string with restriction - enum { 'CREATED', 'DELIVERED', 'CANCELED', 'REVOKED', 'IMPORTED', 'DRAFT', 'FAILED', 'DELETED' }
turnoverDate - required; type string
currPage - required; type int
lastBlock - required; type boolean
rsCount - required; type int
As u can see it has optinal nodes. I use this code to parse when i need to get only one node's value, attributes:
public static String getSessionId(Document docu){
String SessionId = null;
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = docu;
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
//System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("ns2:createSessionResponse");
// System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
//System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// System.out.println("SessionId is: " + eElement.getElementsByTagName("sessionId").item(0).getTextContent());
SessionId = eElement.getElementsByTagName("sessionId").item(0).getTextContent();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return SessionId;
}
How to parse soap response which has optional nodes that can be or cant be in soap response?