java解析带有可选节点的soap响应

时间:2014-06-26 04:53:32

标签: java parsing soap

我需要解析可以有可选节点的soap响应。它的结构如下:

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

你可以看到它有optinal节点。当我只需要获得一个节点的值,属性:

时,我使用此代码进行解析
 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;

}

如何解析具有可选或不能在soap响应中的可选节点的soap响应?

0 个答案:

没有答案