selenium java 讀取xml (資料驅動)

來源:互聯網
上載者:User

標籤:檔案中   tno   資料驅動   factor   簽名   pack   void   happy   ack   

selenium 資料驅動 (xml解析)

getElementByTagName()可以通過標籤名擷取某個標籤。它所擷取的對象是以數組形式存放。如“caption”和“item”標籤在info.xml 檔案中有多個,那麼可以指定數組的下標在擷取某個標籤。
root.getElementsByTagName(‘caption‘) 獲得的是標籤為caption 一組標籤;getElementsByTagName("p")[3]
item(0).getNodeName() 表示一組標籤中的第一個。
item(2).getNodeName() 表示一組標籤中的第三個。

getAttributes()方法可以獲得元素的屬性,getNamedItem()指定屬性的名字。

getTextContent()方法用於擷取標籤對之前的資料。

 HTML代碼
<?xml version="1.0" encoding="UTF-8"?><catalog><maxid>4</maxid><login username="pytest" passwd="123456"><caption>Java</caption><item id="4"><caption>test</caption></item></login><item id="2"><caption>Zope</caption></item></catalog>
 java代碼一
 1 package selenium.test; 2 import java.io.File; 3 import javax.xml.parsers.DocumentBuilder; 4 import javax.xml.parsers.DocumentBuilderFactory; 5 import org.w3c.dom.Document; 6 import org.w3c.dom.NodeList; 7  8 public class readXML { 9     public static void main(String[] args) {10         try {11             File xmlFile = new File("C:\\Users\\happy\\Desktop\\Domo.xml");12             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();13             DocumentBuilder builder = factory.newDocumentBuilder();14             Document doc = builder.parse(xmlFile);15             NodeList ma = doc.getElementsByTagName("maxid");16             NodeList ca = doc.getElementsByTagName("caption");17             NodeList it = doc.getElementsByTagName("item");18             System.out.println(ma.item(0).getNodeName());19             System.out.println(ca.item(0).getNodeName());20             System.out.println(it.item(0).getNodeName());21         } catch (Exception e) {22             e.printStackTrace();23         }24     }25 }
java代碼二
 1  1 package selenium.test; 2  2  3  3 import java.io.File; 4  4 import javax.xml.parsers.DocumentBuilder; 5  5 import javax.xml.parsers.DocumentBuilderFactory; 6  6  7  7 import org.python.constantine.platform.darwin.Sysconf; 8  8 import org.w3c.dom.Document; 9  9 import org.w3c.dom.NodeList;10 10 11 11 public class readXML {12 12     public static void main(String[] args) {13 13         try {14 14             File xmlFile = new File("C:\\Users\\happy\\Desktop\\Domo.xml");15 15             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();16 16             DocumentBuilder builder = factory.newDocumentBuilder();17 17             Document doc = builder.parse(xmlFile);18 18             // 擷取指定標籤中元素/屬性的值19 19             NodeList login = doc.getElementsByTagName("login");20 20             System.out.println(login.item(0).getAttributes().getNamedItem("username"));21 21             System.out.println(login.item(0).getAttributes().getNamedItem("passwd"));22 22             NodeList ca = doc.getElementsByTagName("caption");23 23             // 擷取標籤中的常值內容24 24             for (int i = 0; i < ca.getLength(); i++) {25 25                 System.out.println(ca.item(i).getTextContent());26 26                 //迴圈擷取caption標籤中的常值內容27 27             }28 28 29 29         } catch (Exception e) {30 30             e.printStackTrace();31 31         }32 32     }33 33 34 34 }

 

selenium java 讀取xml (資料驅動)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.