2014-09-23 20 views
1

我試圖從使用JAVA的XML文件中獲取父元素的子元素的數量。這裏是我正在使用的代碼:從JAVA中的XML文件中打印子元素時的答案無效

File fXmlFile = new File("SearchPromotions.xml"); 
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
Document doc = dBuilder.parse(fXmlFile); 

NodeList l = doc.getElementsByTagName("TestCase"); 
Node parentNode = l.item(0); 
int count = parentNode.getChildNodes().getLength(); 

System.out.println(count); 

,這裏是XML文件:

<TestCase> 
    <SelectedDataTableNames name="SearchData"> </SelectedDataTableNames> 

    <Open page="hsbc" ms="5000" /> 
    <Click object="hsbc.Personal_Link" /> 
    <Click object="hsbc.CreditCard_tab" /> 
    <Call businessComponent="Global.Verify_Search"> 
     <Param name="HotelName_Param" value="@SearchData_link" /> 
    </Call> 
    <CheckElementPresent object="hsbc.Img_Hotel_logo" Identifire="Hotel_Name_PARAM:@SearchData_ResultHotelName" fail="true" customErrorMessage="Searched hotel name is not present in the page." /> 
</TestCase> 

問題IM面是,它是印刷錯誤的值。打印的值是13.但正如你所看到的,父元素「TestCase」只有6個子元素。我哪裏做錯了。請幫助

+1

爲什麼不打印節點以查看它真正得到了什麼? – Dawnkeeper 2014-09-23 09:58:54

回答

0

找到了問題的答案。有用。我遇到的問題是ELEMENT_NODE。我們必須過濾ELEMENT_NODE。這是工作代碼。感謝那些幫助過那裏的人。

File fXmlFile = new File("SearchPromotions.xml"); 
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
Document doc = dBuilder.parse(fXmlFile); 
doc.getDocumentElement().normalize(); 

Element docEle = doc.getDocumentElement(); 
NodeList nl = docEle.getChildNodes(); 
if (nl != null && nl.getLength() > 0) { 
     for (int i = 0; i < nl.getLength(); i++) { 
      if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) { 
       Element el = (Element) nl.item(i); 
       System.out.println(el); 
      } 
     } 
} 
1
public static void main(String[] args) throws Exception { 
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
     String fileContent = readFile("SearchPromotions.xml");// Read trimmed file 
     InputStream in = new ByteArrayInputStream(fileContent.getBytes("UTF-8"));// Create stream to pass it to parser() 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     Document doc = dBuilder.parse(in); 

     NodeList l = doc.getElementsByTagName("TestCase"); 
     Node parentNode = l.item(0); 
     int count = parentNode.getChildNodes().getLength(); 

     System.out.println(count); 
    } 

    private static String readFile(String pathname) throws IOException { 
     File file = new File(pathname); 
     StringBuilder fileContents = new StringBuilder((int) file.length()); 
     Scanner scanner = new Scanner(file); 
     try { 
      while (scanner.hasNextLine()) { 
       fileContents.append(scanner.nextLine().trim()); // Trim the whitespace. This resuls in TEXT_NODE. 
      } 
      return fileContents.toString(); 
     } finally { 
      scanner.close(); 
     } 
    } 

XML中有一些空白字符會導致額外的節點。試試上面的解決方案希望它有幫助。

+0

感謝您的代碼。但是當我嘗試這一個時,它說TestCase屬性必須用>或/>來完成。但是,謝謝你停下來 – Vithushan 2014-09-23 11:11:22

1

節點的子節點包括空白文本節點以及子元素節點。

爲什麼不用XPath做到這一點 - 它不那麼麻煩!

隨着撒克遜和XPath 2.0這將是

Processor p = new Processor(false); 
XdmNode doc = p.newDocumentBuilder().build(
    new StreamSource(new File("searchPromotions.xml"))); 
XdmItem result = p.newXPathCompiler().evaluateSingle("/TestCase/count(*)", doc); 
System.out.println(result.getStringValue());