2017-09-18 5 views
1

고유 한 내부 태그가 하나있는 경우 XML 객체를 구문 분석 할 수 있습니다. 하지만 문제는 부모 태그에 두 개의 중복 태그가있을 때입니다. 두 태그 값을 모두 얻으려면 어떻게해야합니까? XML 문자열로 응답을 받고 있어요. 여기 Java에서 DocumentBuilder를 사용하여 중복 xml 태그 값을 구문 분석 할 수 없습니다.

내 코드입니다

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
InputSource is = new InputSource(); 
is.setCharacterStream(new StringReader(responseXML)); 
if (is != null) { 
    Document doc = db.parse(is); 
    String errorCode = ""; 
    NodeList errorDetails = doc.getElementsByTagName("ERROR-LIST"); 
    if (errorDetails != null) { 
     int length = errorDetails.getLength(); 
     if (length > 0) { 
      for (int i = 0; i < length; i++) { 
       if (errorDetails.item(i).getNodeType() == Node.ELEMENT_NODE) { 
        Element el = (Element) errorDetails.item(i); 
        if (el.getNodeName().contains("ERROR-LIST")) { 
         NodeList errorCodes = el.getElementsByTagName("ERROR-CODE"); 
         for (int j = 0; j < errorCodes.getLength(); j++) { 
          Node errorCode1 = errorCodes.item(j); 
          logger.info(errorCode1.getNodeValue()); 
         } 

        } 
       } 
      } 
     } else { 
      isValidResponse = true; 
     } 
    } 
} 

내가 서버에서 받고 있어요 응답은

<DATA><HEADER><RESPONSE-TYPE CODE = "0" DESCRIPTION = "Response Error" /> 
</HEADER><BODY><ERROR-LIST> 
<ERROR-CODE>9000</ERROR-CODE> 
<ERROR-CODE>1076</ERROR-CODE> 
</ERROR-LIST></BODY></DATA> 

임 내가 모든 오류 코드를 잡을 수있는 방법 만 9000 오류 코드를 얻을 수 오류 목록 아래에?

모든 아이디어는 크게 감사하겠습니다. 모든 노드 getElementsByTagName 반환을 통해

el.getElementsByTagName("ERROR-CODE").item(0).getTextContent(); 

루프 :

답변

0

당신은 명시 적으로 오류 목록의 첫 번째 요소를 요청하고 있습니다.

NodeList errorCodes = el.getElementsByTagName("ERROR-CODE"); 
for (int j = 0; j < errorCodes.getLength(); j++) { 
    String errorCode = errorCodes.item(j).getTextContent(); 
} 
+0

항목 (i)을 넣어 보았지만 여전히 동일하게 표시됩니다. 그것의 길이 때문에 ofNodeList errorDetails = doc.getElementsByTagName ("ERROR-LIST"); 1로 지정하십시오. – Aliy

+0

@Aliy'i'는 사용할 변수입니다. 그 중 하나는 ERROR-LIST 어커런스의 양을 나타내며 1입니다. 업데이트 된 답변을 참조하십시오. –

+0

나는 당신의 대답을 시도했다. errorCode를 로그 할 때, [ERROR-CODE : null]로 부여하면 – Aliy