3 Replies Latest reply: Oct 10, 2007 1:38 PM by 807592 RSS

    XML parsing

    807587
      I am trying to parse an xml String to grab the transaction ID from there. Now if I have an xml as follows:-
      <?xml version=\"1.0\" encoding=\"UTF-8\"?><DST><jobName>AWDLookupLOB</jobName><trace>0</trace><readable>Y</readable><AWD><userID>CDPDHR</userID><lookupType>W</lookupType><lookupCriteria><lookupField><name>POLN</name><operand>=</operand><value>75404330</value></lookupField></lookupCriteria><lookupResults QWE=\"Y\"><complete>Y</complete><continueKey>                          2005-09-14-14.21.29.417376T01POLN000877133910                                                                   2005-09-14-14.21.42.849344001                                 </continueKey><transaction QWE=\"Y\" id=\"2005-09-14-14.21.29.417376T01\"><commentFlag>N</commentFlag><relation>1</relation><selected>N</selected><relationRecordType>000</relationRecordType><relationCreation>1900-01-01-00.00.00.000000</relationCreation><createTime>2005-09-14-14.21.29.417376</createTime><recordType>T</recordType><createNode>01</createNode><ownerNode>01</ownerNode><exists>Y</exists><viewFlag>Y</viewFlag><updateFlag>Y</updateFlag><workSelectFlag>Y</workSelectFlag><lockedBy></lockedBy><csd>DEFAULT.CSD</csd><icon></icon><autoFlags>NF  NN NN YYY               N</autoFlags><LOBCALL1>                                                         </LOBCALL1><LOBCALL2>                                                         </LOBCALL2><LOBCALL3>                                                         </LOBCALL3><LOBCALL4>                                                         </LOBCALL4><key>2005-09-14-14.21.29.417376T01</key><businessArea>CICHAS4</businessArea><workType>LOAN</workType><status>QPASS</status><queue>END</queue><priority>999</priority><priorityIncrease>000</priorityIncrease><vip>N</vip><suspended>N</suspended><amountType></amountType><amount>0.0000</amount><fieldGroup QWE=\"Y\"/></transaction></lookupResults></AWD><jobReturn><process>com.dstsystems.awd.ns2.jobManager.ke.tasks.AWDLookupLOB</process><code>0</code><description></description></jobReturn></DST>
      It works fine since there is only one transaction element. If however there are more than one transaction elements I get only the first transactio ID and not the second one. e.g of I have the following it wouldnot work
      <?xml version=\"1.0\" encoding=\"UTF-8\"?><DST><jobName>AWDLookupLOB</jobName><trace>0</trace><readable>Y</readable><AWD><userID>CDPDHR</userID><lookupType>W</lookupType><lookupCriteria><lookupField><name>POLN</name><operand>=</operand><value>75404330</value></lookupField></lookupCriteria><lookupResults QWE=\"Y\"><complete>Y</complete><continueKey>                          2005-09-14-14.21.29.417376T01POLN000877133910                                                                   2005-09-14-14.21.42.849344001                                 </continueKey><transaction QWE=\"Y\" id=\"2005-09-14-14.21.29.417376T01\"><commentFlag>N</commentFlag><relation>1</relation><selected>N</selected><relationRecordType>000</relationRecordType><relationCreation>1900-01-01-00.00.00.000000</relationCreation><createTime>2005-09-14-14.21.29.417376</createTime><recordType>T</recordType><createNode>01</createNode><ownerNode>01</ownerNode><exists>Y</exists><viewFlag>Y</viewFlag><updateFlag>Y</updateFlag><workSelectFlag>Y</workSelectFlag><lockedBy></lockedBy><csd>DEFAULT.CSD</csd><icon></icon><autoFlags>NF  NN NN YYY               N</autoFlags><LOBCALL1>                                                         </LOBCALL1><LOBCALL2>                                                         </LOBCALL2><LOBCALL3>                                                         </LOBCALL3><LOBCALL4>                                                         </LOBCALL4><key>2005-09-14-14.21.29.417376T01</key><businessArea>CICHAS4</businessArea><workType>LOAN</workType><status>QPASS</status><queue>END</queue><priority>999</priority><priorityIncrease>000</priorityIncrease><vip>N</vip><suspended>N</suspended><amountType></amountType><amount>0.0000</amount><fieldGroup QWE=\"Y\"/></transaction><transaction QWE=\"Y\" id=\"2006-11-11-14.21.29.1111111T01\"><commentFlag>N</commentFlag><relation>1</relation><selected>N</selected><relationRecordType>000</relationRecordType><relationCreation>1900-01-01-00.00.00.000000</relationCreation><createTime>2005-09-14-14.21.29.417376</createTime><recordType>T</recordType><createNode>01</createNode><ownerNode>01</ownerNode><exists>Y</exists><viewFlag>Y</viewFlag><updateFlag>Y</updateFlag><workSelectFlag>Y</workSelectFlag><lockedBy></lockedBy><csd>DEFAULT.CSD</csd><icon></icon><autoFlags>NF  NN NN YYY               N</autoFlags><LOBCALL1>                                                         </LOBCALL1><LOBCALL2>                                                         </LOBCALL2><LOBCALL3>                                                         </LOBCALL3><LOBCALL4>                                                         </LOBCALL4><key>2005-09-14-14.21.29.417376T01</key><businessArea>CICHAS4</businessArea><workType>LOAN</workType><status>QPASS</status><queue>END</queue><priority>999</priority><priorityIncrease>000</priorityIncrease><vip>N</vip><suspended>N</suspended><amountType></amountType><amount>0.0000</amount><fieldGroup QWE=\"Y\"/></transaction></lookupResults></AWD><jobReturn><process>com.dstsystems.awd.ns2.jobManager.ke.tasks.AWDLookupLOB</process><code>0</code><description></description></jobReturn></DST>
      The code is as follows
      private String parseDOMGetTransID(String respString) throws JDOMException, IOException
           {
                String transID;
      
                // build a JDom tree
                SAXBuilder builder = new SAXBuilder();
      
                Document document = builder.build(new StringReader(respString));
                Element root = document.getRootElement();
      
                Element awdNode = root.getChild(AWD);
                Element lookupResults = awdNode.getChild(LOOKUPRESULTS);
                Element transactionNode = lookupResults.getChild(TRANSACTION);
      
                transID = transactionNode.getAttributeValue(ID);
                /*Logger.finer*/
                System.out.println("AWDLookupLOB::parseFile() transId from the parser " + transID);
      
                return transID;
           }
      could somebody help me out here as to what should I do??????
        • 1. Re: XML parsing
          796447
          Element transactionNode = lookupResults.getChild(TRANSACTION);
          You only told it to get a single child Element, so what did you expect?
          Look at the API and see if there isn't a getChildren() or something to get you the collection of child nodes instead of just the first one.
          • 2. Re: XML parsing
            807587
            try this
            SAXBuilder builder = new SAXBuilder();
            
                      Document document = builder.build(new StringReader(respString));
                      Element root = document.getRootElement();
            
                      Element awdNode = root.getChild(AWD);
                      Element lookupResults = awdNode.getChild(LOOKUPRESULTS);
                      java.util.List list = lookupResults.getChildren(TRANSACTION);
                      System.out.println("No of chikdren is " + list.size());          
                      String [] transID = new String[list.size()];
                      for (int i = 0; i < list.size(); i++)
                      {               
                           transID[i] = (String)((Element)list.get(i)).getAttributeValue(ID);
                           System.out.println("AWDLookupLOB::parseFile() transId from the parser " + transID);
                      }          
                                

                      return transID;

            • 3. Re: XML parsing
              807592
              what imports do i need to uses this.

              i have xerces, how can i use that instead of sax?