简体   繁体   中英

Need to iterate an xml with child nodes with same name

I have an xml

<CommonTestData>
<GiftCards> 
  <GiftCard>
      <cardnumber>7777016774730834</cardnumber>
      <number>1815</number>
  </GiftCard>
  <GiftCard>
      <cardnumber>7777016774687937</cardnumber>
      <number>6256</number>
  </GiftCard>
</GiftCards>

I have to iterate these xml and read values and enter into an Selenium web application and check whether gift card applied amount is greater than zero . If the amount applied is zero then try another card . If the amount applied is greater than zero then break the loop

My code something look like

for (int i=0;i<xmlvalue.getNodeCount("GiftCard", "CommonTestData.xml");i++){
            //giftcardaccordian.click();
            giftcardnumber.sendKeys(xmlvalue.getValue("cardnumber"+i, "GiftCard", "CommonTestData.xml")); // I need code for getvalue function so that i can iterate through 
            giftcardpin.sendKeys(xmlvalue.getValue("cardnumber"+i, "GiftCard", "CommonTestData.xml"));

            giftcardapplybutton.click();
            try{
                if(appliedgiftcardamount.getText()!="$0"){
                    break;
                }
            }catch (Exception e ){
                Assert.fail("Cannot apply reward certicate");
            }
        }

I need implementaion for Getvalue so that i can iterate through. Right now my implementation is something like

public String getValue(String csstagname, String Elementname, String xmlfilename) {
    String eElement1;

    try {
      String path = "config/XML/" + xmlfilename;
      File fXmlFile = new File(path);
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(fXmlFile);
      doc.getDocumentElement().normalize();
      NodeList nList = doc.getElementsByTagName(Elementname);

      for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
          eElement1 = csstagname;
          Element eElement2 = (Element) nNode;

          value = (getTagValue(eElement1, eElement2));

        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {

      return (value);
    }
  }

What you are doing is possible, but it's not in the spirit of XML. Do you control the XML file? Change the format so that card nnumber and number have their own overarching tag to bind them together:

<CommonTestData>
    <GiftCards> 
      <GiftCard>
          <cardnumber>7777016774730834</cardnumber>
          <number>1815</number>
      </GiftCard>
      <GiftCard>
          <cardnumber>7777016774687937</cardnumber>
          <number>6256</number>
      </GiftCard>
    </GiftCards>
</CommonTestData>

If you do not control the format, extract all nodes tagged with cardnumber , all nodes tagged with number , then access both arrays with same index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM