Forum Discussion

anatar02's avatar
anatar02
Contributor
12 years ago

How to read the child node with different parent node

Hi team, 



I have a xml with contains of  two diffrent parent nodes.. with same child node name....here i wanted to retrivew the 2nd child node from 2nd parent nide? here my xml and code 




       <SaleItem>




       <Workstationid>34</Workstationid>

</SaleItem>









      <Sale>




      <Workstationid>38</Workstationid>

</Sale>




now here i wanted to retrivew the valies of Workstationid node, i am able to read the first child node value of first parent child....? but my doubt is how can retrivew the 2nd child node of 2nd parent node which is 38 is Workstationid....Can some one help me on this?







function test()


{


  var xmlDoc, s;


  


  // Specify your file name


  var FileName = "C:\\StorePos\\FILES\\HISTORY\\00000003\\20130305\\034\\00000003_034_0000001.kvx";


  


  // Load xml file


  xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");


  xmlDoc.load(FileName);


 


  // Check errors


  if (xmlDoc.parseError.errorCode != 0)


  {


    s = "Reason: " + xmlDoc.parseError.reason + "\n" +


        "Line: " + xmlDoc.parseError.line + "\n" +


        "Text: " + xmlDoc.parseError.srcText;


    Log.Error("Failed to open file " + FileName, s);


    return;


  }


 


  // Specify your XPath expression 


  var xmlNode = xmlDoc.selectNodes("//Workstationid");


    


  if (xmlNode != null)


  {


  var i =5;


  do


    {


     i++; 


     Log["Message"](xmlNode.item(0).text);


     


     if (i==5) break;


    }


  while (i < 10) 


  }


  


 }







  • It seems that the following line is incorrect: Log["Message"](xmlNode.item(0).text);

    You are using 0 as item index. This code should work: Log["Message"](xmlNode.item(i).text);



    BTW, the cycle code is too complicated. If you have only 2 nodes then the cycle is not necessary:

    Log["Message"](xmlNode.item(0).text);

    Log["Message"](xmlNode.item(1).text);



    If there are more nodes then I suggest this:

    if (xmlNode != null)

    {

      for (var i = 0; i < xmlNode["length"]; i++)

        Log["Message"](xmlNode.item(i).text);

    }

2 Replies

  • It seems that the following line is incorrect: Log["Message"](xmlNode.item(0).text);

    You are using 0 as item index. This code should work: Log["Message"](xmlNode.item(i).text);



    BTW, the cycle code is too complicated. If you have only 2 nodes then the cycle is not necessary:

    Log["Message"](xmlNode.item(0).text);

    Log["Message"](xmlNode.item(1).text);



    If there are more nodes then I suggest this:

    if (xmlNode != null)

    {

      for (var i = 0; i < xmlNode["length"]; i++)

        Log["Message"](xmlNode.item(i).text);

    }
  • Thanks hanya!!!



    bellow line solved my issues!!!




    {


      for (var i = 0; i < xmlNode["length"]; i++)


        //Log["Message"](xmlNode.item(i).text);


        Log["Message"](xmlNode.item(nodeNumber).text);


       


    }