2017-03-23 65 views
0

以下是我的XML。我只是想獲取對於那些誰擁有GLOBAL_EMP_NO值EMPLOYEE_STATUS = 0跳轉多個標記引用XML JAVA中的兄弟標記DOM

XML:

<GLOBAL_EMP_NO>A12345678</GLOBAL_EMP_NO> 
<EMP_NO>12345678</EMP_NO> 
<FIRST_NAME>Tushar Furlenco</FIRST_NAME> 
<PREF_FIRST_NAME/> 
<MIDDLE_NAME/> 
<LAST_NAME>Joseph</LAST_NAME> 
<PREF_LAST_NAME>Tushar</PREF_LAST_NAME> 
<SUFFIX/> 
<SMTP_EMAIL>[email protected]</SMTP_EMAIL> 
<EMPLOYEE_STATUS>0</EMPLOYEE_STATUS> 

我想下面的語法時EMPLOYEE_STATUS = 0。但我這是工作的罰款並獲取GLOBAL_EMP_NO不知道這是否是一種正確的做法。任何人都可以提供這方面的建議嗎?

感謝

Java代碼:

Element element = (Element) node; 
ArrayList emp0 = new ArrayList(); 
String emp_status = element.getElementsByTagName("EMPLOYEE_STATUS").item(0).getTextContent(); 
if (emp_status.equals("0")) { 
emp0.add(element.getElementsByTagName("EMPLOYEE_STATUS").item(0).getPreviousSibling().getPreviousSibling(). 
        getPreviousSibling().getPreviousSibling().getPreviousSibling().getPreviousSibling().getPreviousSibling() 
        .getPreviousSibling().getPreviousSibling().getPreviousSibling().getTextContent()); 
} 
+0

你爲什麼不能得到使用方式EMPLOYEE_STATUS一樣GLOBAL_EMP_NO的價值? – ndlu

回答

-1

您正在尋找這樣的事情MCVE

String xml = "<Root>" + 
       "<Item>" + 
       "<No>A123</No>" + 
       "<Other>Foo</Other>" + 
       "<Status>0</Status>" + 
       "</Item>" + 
       "<Item>" + 
       "<No>B456</No>" + 
       "<Other>Bar</Other>" + 
       "<Status>1</Status>" + 
       "</Item>" + 
       "<Item>" + 
       "<No>C789</No>" + 
       "<Other>Baz</Other>" + 
       "<Status>0</Status>" + 
       "</Item>" + 
      "</Root>"; 
Element root = DocumentBuilderFactory.newInstance().newDocumentBuilder() 
       .parse(new InputSource(new StringReader(xml))).getDocumentElement(); 
NodeList items = root.getElementsByTagName("Item"); 
for (int i = 0; i < items.getLength(); i++) { 
    Element item = (Element) items.item(i); 
    if ("0".equals(item.getElementsByTagName("Status").item(0).getTextContent())) 
     System.out.println(item.getElementsByTagName("No").item(0).getTextContent()); 
} 

輸出

A123 
C789 
+0

Yesssss !!!。非常感謝Andreas。雖然我可以想到它.. –