2016-06-22 69 views
0

我有一個xml需要遍歷與子節點的XML同名

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

我不得不重複這些XML和讀取值,進入一個Selenium Web應用程序,並檢查是否禮品卡應用金額大於零。 If the amount applied is zero then try another card . If the amount applied is greater than zero then break the loop

我的代碼的東西看起來像

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"); 
      } 
     } 

我需要的GetValue implementaion,這樣我可以遍歷。現在我的實現就像

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); 
    } 
    } 
+0

你的問題與Selenium沒有任何關係。你應該刪除該標籤。 – JeffC

回答

0

你在做什麼是可能的,但它不符合XML的精神。你是否控制XML文件?更改格式,以便卡n已接收和數量都有自己的首要標籤將它們綁定在一起:

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

如果不控制的格式,提取標記cardnumber所有節點上,標有number所有節點,然後同時訪問具有相同索引的數組。

+0

是的我可以改變XML格式。是的,我會改變這一點,看起來不錯 – SurajSr