469,603 Members | 2,100 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

XML problem when trying to loop through child nodes

4
Hi,
I have one problem when i'm trying to loop thorugh the child nodes in a XML string in JAVA. The first Loop is working but in the child node there i want to loop and get the values but it doesn't change the value. Always print the first child node value. I got the length of the child nodes but ..... Pls see the xml string.

I want to print the service class and availabilty in the each flightinfo node. I got he lenth of flightinfo also the infoonClasses. But it always print the first Class and Avalibilty in the entire loop.

can any on tell me how can loopthrough the child nodes......

Expand|Select|Wrap|Line Numbers
  1. <MultiAvailabilityReply>
  2.     <flightInfo>
  3.         <basicFlightInfo>
  4.             <flightDetails>
  5.                 <departureDate>250307</departureDate>
  6.                 <departureTime>0030</departureTime>
  7.                 <arrivalDate>250307</arrivalDate>
  8.                 <arrivalTime>0820</arrivalTime>
  9.             </flightDetails>
  10.             <departureLocation>
  11.                 <cityAirport>BOM</cityAirport>
  12.             </departureLocation>
  13.             <arrivalLocation>
  14.                 <cityAirport>SIN</cityAirport>
  15.             </arrivalLocation>
  16.             <marketingCompany>
  17.                 <identifier>SQ</identifier>
  18.             </marketingCompany>
  19.             <flightIdentification>
  20.                 <number>423</number>
  21.             </flightIdentification>
  22.             <productTypeDetail>
  23.                 <productIndicators>D</productIndicators>
  24.                 <productIndicators>EN</productIndicators>
  25.             </productTypeDetail>
  26.             <lineItemNumber>1</lineItemNumber>
  27.         </basicFlightInfo>
  28.         <infoOnClasses>
  29.             <productClassDetail>
  30.                 <serviceClass>F</serviceClass>
  31.                 <availabilityStatus>4</availabilityStatus>
  32.             </productClassDetail>
  33.         </infoOnClasses>
  34.         <infoOnClasses>
  35.             <productClassDetail>
  36.                 <serviceClass>P</serviceClass>
  37.                 <availabilityStatus>4</availabilityStatus>
  38.             </productClassDetail>
  39.         </infoOnClasses>
  40.         <infoOnClasses>
  41.             <productClassDetail>
  42.                 <serviceClass>A</serviceClass>
  43.                 <availabilityStatus>4</availabilityStatus>
  44.             </productClassDetail>
  45.         </infoOnClasses>
  46.         <infoOnClasses>
  47.             <productClassDetail>
  48.                 <serviceClass>Z</serviceClass>
  49.                 <availabilityStatus>4</availabilityStatus>
  50.             </productClassDetail>
  51.         </infoOnClasses>
  52.         <infoOnClasses>
  53.             <productClassDetail>
  54.                 <serviceClass>C</serviceClass>
  55.                 <availabilityStatus>4</availabilityStatus>
  56.             </productClassDetail>
  57.         </infoOnClasses>
  58.         <infoOnClasses>
  59.             <productClassDetail>
  60.                 <serviceClass>J</serviceClass>
  61.                 <availabilityStatus>4</availabilityStatus>
  62.             </productClassDetail>
  63.         </infoOnClasses>
  64.         <infoOnClasses>
  65.             <productClassDetail>
  66.                 <serviceClass>D</serviceClass>
  67.                 <availabilityStatus>4</availabilityStatus>
  68.             </productClassDetail>
  69.         </infoOnClasses>
  70.         <infoOnClasses>
  71.             <productClassDetail>
  72.                 <serviceClass>Y</serviceClass>
  73.                 <availabilityStatus>9</availabilityStatus>
  74.             </productClassDetail>
  75.         </infoOnClasses>
  76.         <infoOnClasses>
  77.             <productClassDetail>
  78.                 <serviceClass>B</serviceClass>
  79.                 <availabilityStatus>9</availabilityStatus>
  80.             </productClassDetail>
  81.         </infoOnClasses>
  82.         <infoOnClasses>
  83.             <productClassDetail>
  84.                 <serviceClass>M</serviceClass>
  85.                 <availabilityStatus>9</availabilityStatus>
  86.             </productClassDetail>
  87.         </infoOnClasses>
  88.         <infoOnClasses>
  89.             <productClassDetail>
  90.                 <serviceClass>W</serviceClass>
  91.                 <availabilityStatus>9</availabilityStatus>
  92.             </productClassDetail>
  93.         </infoOnClasses>
  94.         <infoOnClasses>
  95.             <productClassDetail>
  96.                 <serviceClass>Q</serviceClass>
  97.                 <availabilityStatus>9</availabilityStatus>
  98.             </productClassDetail>
  99.         </infoOnClasses>
  100.         <infoOnClasses>
  101.             <productClassDetail>
  102.                 <serviceClass>N</serviceClass>
  103.                 <availabilityStatus>9</availabilityStatus>
  104.             </productClassDetail>
  105.         </infoOnClasses>
  106.         <infoOnClasses>
  107.             <productClassDetail>
  108.                 <serviceClass>V</serviceClass>
  109.                 <availabilityStatus>1</availabilityStatus>
  110.             </productClassDetail>
  111.         </infoOnClasses>
  112.         <infoOnClasses>
  113.             <productClassDetail>
  114.                 <serviceClass>T</serviceClass>
  115.                 <availabilityStatus>0</availabilityStatus>
  116.             </productClassDetail>
  117.         </infoOnClasses>
  118.         <infoOnClasses>
  119.             <productClassDetail>
  120.                 <serviceClass>L</serviceClass>
  121.                 <availabilityStatus>0</availabilityStatus>
  122.             </productClassDetail>
  123.         </infoOnClasses>
  124.         <infoOnClasses>
  125.             <productClassDetail>
  126.                 <serviceClass>K</serviceClass>
  127.                 <availabilityStatus>0</availabilityStatus>
  128.             </productClassDetail>
  129.         </infoOnClasses>
  130.         <infoOnClasses>
  131.             <productClassDetail>
  132.                 <serviceClass>S</serviceClass>
  133.                 <availabilityStatus>9</availabilityStatus>
  134.             </productClassDetail>
  135.         </infoOnClasses>
  136.         <infoOnClasses>
  137.             <productClassDetail>
  138.                 <serviceClass>E</serviceClass>
  139.                 <availabilityStatus>9</availabilityStatus>
  140.             </productClassDetail>
  141.         </infoOnClasses>
  142.         <infoOnClasses>
  143.             <productClassDetail>
  144.                 <serviceClass>H</serviceClass>
  145.                 <availabilityStatus>9</availabilityStatus>
  146.             </productClassDetail>
  147.         </infoOnClasses>
  148.         <additionalFlightInfo>
  149.             <flightDetails>
  150.                 <typeOfAircraft>773</typeOfAircraft>
  151.                 <numberOfStops>0</numberOfStops>
  152.                 <legDuration>0520</legDuration>
  153.             </flightDetails>
  154.             <departureStation>
  155.                 <terminal>2</terminal>
  156.             </departureStation>
  157.             <arrivalStation>
  158.                 <terminal>2</terminal>
  159.             </arrivalStation>
  160.             <productFacilities>
  161.                 <type>AS</type>
  162.             </productFacilities>
  163.             <productFacilities>
  164.                 <type>DA</type>
  165.             </productFacilities>
  166.             <productFacilities>
  167.                 <type>LSA</type>
  168.             </productFacilities>
  169.             <productFacilities>
  170.                 <type>M</type>
  171.             </productFacilities>
  172.         </additionalFlightInfo>
  173.     </flightInfo>
  174.     <flightInfo>
  175.         <basicFlightInfo>
  176.             <flightDetails>
  177.                 <departureDate>250307</departureDate>
  178.                 <departureTime>0645</departureTime>
  179.                 <arrivalDate>250307</arrivalDate>
  180.                 <arrivalTime>1425</arrivalTime>
  181.             </flightDetails>
  182.             <departureLocation>
  183.                 <cityAirport>BOM</cityAirport>
  184.             </departureLocation>
  185.             <arrivalLocation>
  186.                 <cityAirport>SIN</cityAirport>
  187.             </arrivalLocation>
  188.             <marketingCompany>
  189.                 <identifier>AI</identifier>
  190.             </marketingCompany>
  191.             <flightIdentification>
  192.                 <number>472</number>
  193.             </flightIdentification>
  194.             <productTypeDetail>
  195.                 <productIndicators>D</productIndicators>
  196.                 <productIndicators>ET</productIndicators>
  197.             </productTypeDetail>
  198.             <lineItemNumber>2</lineItemNumber>
  199.         </basicFlightInfo>
  200.         <infoOnClasses>
  201.             <productClassDetail>
  202.                 <serviceClass>J</serviceClass>
  203.                 <availabilityStatus>4</availabilityStatus>
  204.             </productClassDetail>
  205.         </infoOnClasses>
  206.         <infoOnClasses>
  207.             <productClassDetail>
  208.                 <serviceClass>C</serviceClass>
  209.                 <availabilityStatus>4</availabilityStatus>
  210.             </productClassDetail>
  211.         </infoOnClasses>
  212.         <infoOnClasses>
  213.             <productClassDetail>
  214.                 <serviceClass>D</serviceClass>
  215.                 <availabilityStatus>4</availabilityStatus>
  216.             </productClassDetail>
  217.         </infoOnClasses>
  218.         <infoOnClasses>
  219.             <productClassDetail>
  220.                 <serviceClass>W</serviceClass>
  221.                 <availabilityStatus>4</availabilityStatus>
  222.             </productClassDetail>
  223.         </infoOnClasses>
  224.         <infoOnClasses>
  225.             <productClassDetail>
  226.                 <serviceClass>Y</serviceClass>
  227.                 <availabilityStatus>R</availabilityStatus>
  228.             </productClassDetail>
  229.         </infoOnClasses>
  230.         <infoOnClasses>
  231.             <productClassDetail>
  232.                 <serviceClass>B</serviceClass>
  233.                 <availabilityStatus>4</availabilityStatus>
  234.             </productClassDetail>
  235.         </infoOnClasses>
  236.         <infoOnClasses>
  237.             <productClassDetail>
  238.                 <serviceClass>H</serviceClass>
  239.                 <availabilityStatus>4</availabilityStatus>
  240.             </productClassDetail>
  241.         </infoOnClasses>
  242.         <infoOnClasses>
  243.             <productClassDetail>
  244.                 <serviceClass>K</serviceClass>
  245.                 <availabilityStatus>4</availabilityStatus>
  246.             </productClassDetail>
  247.         </infoOnClasses>
  248.         <infoOnClasses>
  249.             <productClassDetail>
  250.                 <serviceClass>L</serviceClass>
  251.                 <availabilityStatus>4</availabilityStatus>
  252.             </productClassDetail>
  253.         </infoOnClasses>
  254.         <infoOnClasses>
  255.             <productClassDetail>
  256.                 <serviceClass>M</serviceClass>
  257.                 <availabilityStatus>4</availabilityStatus>
  258.             </productClassDetail>
  259.         </infoOnClasses>
  260.         <infoOnClasses>
  261.             <productClassDetail>
  262.                 <serviceClass>V</serviceClass>
  263.                 <availabilityStatus>4</availabilityStatus>
  264.             </productClassDetail>
  265.         </infoOnClasses>
  266.         <infoOnClasses>
  267.             <productClassDetail>
  268.                 <serviceClass>G</serviceClass>
  269.                 <availabilityStatus>4</availabilityStatus>
  270.             </productClassDetail>
  271.         </infoOnClasses>
  272.         <additionalFlightInfo>
  273.             <flightDetails>
  274.                 <typeOfAircraft>310</typeOfAircraft>
  275.                 <numberOfStops>0</numberOfStops>
  276.                 <legDuration>0510</legDuration>
  277.             </flightDetails>
  278.             <departureStation>
  279.                 <terminal>2</terminal>
  280.             </departureStation>
  281.             <arrivalStation>
  282.                 <terminal>1</terminal>
  283.             </arrivalStation>
  284.             <productFacilities>
  285.                 <type>AS</type>
  286.             </productFacilities>
  287.             <productFacilities>
  288.                 <type>DA</type>
  289.             </productFacilities>
  290.             <productFacilities>
  291.                 <type>M</type>
  292.             </productFacilities>
  293.         </additionalFlightInfo>
  294.     </flightInfo>
  295.     <MultiAvailabilityReply>
  296.  
Apr 16 '07 #1
3 2905
dorinbogdan
839 Expert 512MB
Welcome to TheScripts TSDN...

Please also post the Java code necessary for a quick test.

Thanks,
Dorin.
Apr 16 '07 #2
jismon
4
Thanks for a quick reply,
pls check the below code,


Expand|Select|Wrap|Line Numbers
  1. import javax.xml.parsers.DocumentBuilder;
  2. import javax.xml.parsers.DocumentBuilderFactory;
  3. import java.io.StringReader;
  4.  
  5.  
  6. import org.w3c.dom.Document;
  7. import org.w3c.dom.Element;
  8. import org.w3c.dom.Node;
  9. import org.w3c.dom.NodeList;
  10. import org.xml.sax.InputSource;
  11. import org.xml.sax.SAXException;
  12. import org.xml.sax.SAXParseException;
  13.  
  14. import com.sun.org.apache.xerces.internal.dom.ChildNode;
  15.  
  16. public class FlightAvailability {
  17.  
  18.     public static void main(String[] args) {
  19.  
  20.         multiAvailResPage();
  21.     }
  22.  
  23.     public static void multiAvailResPage(){
  24.         try {
  25.  
  26.             DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  27.             DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  28.             String strOutPut="";
  29.             String strOutPut1="";
  30.             String source;
  31.             String destination;
  32.             String[] depDate;
  33.             String[] arrDate;
  34.             String[] deptTime;
  35.             String[] arrTime;
  36.             String[] carrierName;
  37.             String[] flightNo;
  38.             String[] serClass;
  39.             String[] vacantSeats;
  40.             depDate    =new String[100];
  41.             arrDate=new String[100];
  42.             deptTime = new String[100];
  43.             arrTime    = new String[100];
  44.             carrierName=new String[100];
  45.             flightNo=new String[100];
  46.             serClass= new String[500];
  47.             vacantSeats=new String[500];
  48.  
  49.             String xmlString = <Pls pate the xml string here>;
  50.             StringReader sr = new StringReader((String) xmlString);
  51.             InputSource iSrc = new InputSource(sr);
  52.             Document doc = docBuilder.parse(iSrc);
  53.             doc.getDocumentElement().normalize();
  54.             //System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
  55.  
  56.  
  57.  
  58.             strOutPut="<table border='0'>";
  59.  
  60.             NodeList departure = doc.getElementsByTagName("origin");
  61.             Element departurePlace = (Element)departure.item(0);
  62.             NodeList originValue = departurePlace.getChildNodes();
  63.             source=((Node)originValue.item(0)).getNodeValue();
  64.  
  65.             NodeList arrival = doc.getElementsByTagName("destination");
  66.             Element arrivalPlace    =(Element)arrival.item(0);
  67.             NodeList arrivalValue    = arrivalPlace.getChildNodes();
  68.             destination= ((Node)arrivalValue.item(0)).getNodeValue();
  69.  
  70.             System.out.println("Source        " + source);
  71.             System.out.println("Destination " + destination);
  72.  
  73.             strOutPut1="<tr><td border=0>From:</td><td border=0><input type='text' readOnly name='source' value=" + source+"></td>";
  74.             strOutPut1=strOutPut1+ "<td border=0>Destination:</td><td border=0><input type='text' readOnly name='destination' value=" + destination+"></td></tr>";
  75.             //ChildNode rootNode = doc.getElementsByTagName("flightInfo");
  76.             NodeList rootNode = doc.getElementsByTagName("flightInfo");        
  77.  
  78.             int nodeLength = rootNode.getLength();
  79.             //System.out.println("Total no of flightInfo : " + nodeLength);
  80.  
  81.             for(int s=0; s<nodeLength; s++){
  82.  
  83.                 Node firstNode = rootNode.item(s);
  84.                 System.out.println("Total no of Innder Child : " + firstNode.getChildNodes().getLength());
  85.                 if(firstNode.getNodeType() == Node.ELEMENT_NODE){
  86.  
  87.  
  88.                     // System.out.println("My Class : " +  getNodeValue.getNodDetails("serviceClass",firstNode));
  89.                     // System.out.println("Available Seat : " +  getNodeValue.getNodDetails("availabilityStatus",firstNode));
  90.  
  91.                     Node innerChildNode=firstNode.getFirstChild();
  92.  
  93. //                    
  94.                     arrDate[s]=getNodeValue.getNodDetails("arrivalDate",firstNode);
  95.                     depDate[s]= getNodeValue.getNodDetails("departureDate",firstNode);
  96.                     deptTime[s]= getNodeValue.getNodDetails("departureTime",firstNode);
  97.                     arrTime[s]=    getNodeValue.getNodDetails("arrivalTime",firstNode);
  98.                     carrierName[s]=getNodeValue.getNodDetails("identifier",firstNode);
  99.                     flightNo[s]    =getNodeValue.getNodDetails("number",firstNode);
  100.  
  101.                     int childLength=firstNode.getChildNodes().getLength();
  102.  
  103.                     for(int childNodeLen=0;childNodeLen < childLength;childNodeLen++)
  104.                     {                    
  105.                         System.out.println("Loop No : " + childNodeLen);
  106.                         Node myInnderNode= innerChildNode.getNextSibling();
  107.                         String nodeName= myInnderNode.getNodeName();
  108.                         System.out.println("nodeName : "+nodeName );
  109.                         if(nodeName == "serviceClass"){
  110.                             System.out.println("Jismon : " );
  111.                         }
  112.                         System.out.println("Class : " +  getNodeValue.getNodDetails("serviceClass",myInnderNode));
  113.                         System.out.println("Available Seat : " +  getNodeValue.getNodDetails("availabilityStatus",myInnderNode));
  114.                         //myInnderNode=firstNode.getChildNodes().item(childNodeLen);
  115.                         myInnderNode=innerChildNode.getNextSibling();
  116.  
  117.  
  118.                     }
  119.                     //Node innerNode = firstNode.getNextSibling();
  120.  
  121.  
  122.                     //for(int myInt=0;myInt<firstNode.getChildNodes().getLength();myInt++)
  123.                     //{
  124.                         /*if( firstNode.getChildNodes().item(s).getNodeName().equalsIgnoreCase("infoOnClasses"))
  125.                         {
  126.  
  127.                              Node InnerNode=firstNode.getChildNodes().item(s).getFirstChild();
  128.                              System.out.println("i st loop : " +  InnerNode.getChildNodes().getLength());
  129.  
  130.                              for(int InnerNodeLength=0;InnerNodeLength < InnerNode.getChildNodes().getLength();InnerNodeLength++)
  131.                              {
  132.                                 // serClass[InnerNodeLength]=getNodeValue.getNodDetails("productClassDetail",InnerNode);
  133.                                  System.out.println("My Class : " +  getNodeValue.getNodDetails("serviceClass",InnerNode));
  134.                                  System.out.println("Available Seat : " +  getNodeValue.getNodDetails("availabilityStatus",InnerNode));
  135.                              }
  136.  
  137.                         }*/
  138.                     //}
  139.  
  140.                     /*if(firstNode.getNodeName().equalsIgnoreCase("infoOnClasses"))
  141.                     {
  142.                         //Node myInnerChild=firstNode.get);
  143.  
  144.                         NodeList myNode = firstNode.getChildNodes();
  145.                         System.out.println("My Print : " + myNode.getLength());
  146.                     }*/
  147.  
  148.                     //int innernodLen = innerNode.getLength();
  149.  
  150.                     /*
  151.  
  152.                     for(int p=0; p<innernodLen; p++){
  153.                         Node innerfirstNode = innerNode.item(p);
  154.                         if(innerfirstNode.getNodeType() == Node.ELEMENT_NODE){
  155.  
  156.                             serClass[p]=getNodeValue.getNodDetails("serviceClass",innerfirstNode);
  157.                             vacantSeats[p]=getNodeValue.getNodDetails("availabilityStatus",innerfirstNode);
  158.                             System.out.println("Service Class " + serClass[p]);
  159.                             System.out.println("Availability  " + vacantSeats[p]);
  160.  
  161.                             //System.out.println("Loop No " + p);
  162.  
  163.                         }
  164.  
  165.                     }*/
  166.                     System.out.println("+++++++++++++++++++++++++++++++");
  167.                     //System.out.println("Inner Node Length  " + innernodLen);
  168.                     System.out.println("Depat date    " + depDate[s]);
  169.                     System.out.println("Depat Time    " + deptTime[s]);
  170.                     System.out.println("Arrival date  " + arrDate[s]);
  171.                     System.out.println("Arrival Time  " + arrTime[s]);
  172.                     System.out.println("Carrier Name  " + carrierName[s]);
  173.                     System.out.println("Flight No     " + flightNo[s]);
  174.  
  175.  
  176.                 }
  177.             }
  178.             strOutPut1=strOutPut1 + "<tr><td>Departure Date:</td><td><input type='text' readOnly name='deptDate' value=" +depDate+"></td></tr>";
  179.             strOutPut=strOutPut+strOutPut1+"</table>";
  180.             System.out.println("HTML STRING : " +strOutPut);
  181.  
  182.         }catch (SAXParseException err) {
  183.             System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
  184.             System.out.println(" " + err.getMessage ());
  185.  
  186.             }catch (SAXException e) {
  187.             Exception x = e.getException ();
  188.             ((x == null) ? e : x).printStackTrace ();
  189.  
  190.             }catch (Throwable t) {
  191.             t.printStackTrace ();
  192.             }
  193.  
  194.  
  195.         }
  196.  
  197. }
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. import org.w3c.dom.Element;
  205. import org.w3c.dom.Node;
  206. import org.w3c.dom.NodeList;
  207.  
  208. /**
  209.  * @Jismon Raju K
  210.  *
  211.  */
  212. public class getNodeValue {
  213.  
  214.     public static String getNodDetails(String tagName,Node myNode){
  215.         String tagValue="";
  216.         Element firstDatasourceElement = (Element)myNode; 
  217.         NodeList departureDate = firstDatasourceElement.getElementsByTagName(tagName);
  218.         Element departureDateValue = (Element)departureDate.item(0);
  219.         NodeList deptValue = departureDateValue.getChildNodes();
  220.         //System.out.println("Departure Date : " + ((Node)childNodes.item(0)).getNodeValue().trim());
  221.         tagValue=((Node)deptValue.item(0)).getNodeValue();
  222.         return tagValue;
  223.     }
  224.  
  225.  
  226. }
Apr 16 '07 #3
dorinbogdan
839 Expert 512MB
Ok,
I will copy this thread to Java forum too, they may answer quicker.
Apr 16 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Dino L. | last post: by
4 posts views Thread by J.B. | last post: by
reply views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.