[英]Need to iterate an xml with child nodes with same name
我有一個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實現,以便我可以迭代。 現在我的實現是這樣的
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);
}
}
你正在做的事情是可能的,但它不符合XML的精神。 你控制XML文件嗎? 更改格式,以便卡號和號碼有自己的總體標記將它們綁定在一起:
<CommonTestData>
<GiftCards>
<GiftCard>
<cardnumber>7777016774730834</cardnumber>
<number>1815</number>
</GiftCard>
<GiftCard>
<cardnumber>7777016774687937</cardnumber>
<number>6256</number>
</GiftCard>
</GiftCards>
</CommonTestData>
如果您不控制格式,請提取所有標記為cardnumber
節點,所有節點都標記為number
,然后訪問具有相同索引的兩個陣列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.