2014-02-26 6 views
0

내 웹 앱 국제화를하고있어 내가 다음과 같이 XML 파일에 오류 메시지 목록이 있습니다자바 구문 분석 XML의 국제화

<ErrorList> 
    <Errors culture="en"> 

    <Error id="InvalidBooking"> 
     <Heading1>We are unable to find your booking.</Heading1> 
     <Heading2></Heading2> 
     <Description1>Please try again.</Description1> 
     <Description2>OR</Description2> 
     <Description3>Seek assistance at the Check-In counter.</Description3> 
     <Description4></Description4> 
    </Error> 
</Errors> 

    <Errors culture="zh"> 

     <Error id="InvalidBooking"> 
     <Heading1>我们无法找到您的预订。</Heading1> 
     <Heading2></Heading2> 
     <Description1>请再试一次。.</Description1> 
     <Description2>或</Description2> 
     <Description3>请联系登机柜台寻求帮助。</Description3> 
     <Description4></Description4> 
     </Error> 

    </Errors> 

</ErrorList> 

나는 오류 메시지를로드 할 DOM XML 파서를 사용 . 그 때 나는 오류 ID를 일치시켜 특정 오류가 발생합니다 :

DocumentBuilder builder = DocumentBuilderFactory.newInstance(). 
       newDocumentBuilder(); 
     Document doc = builder.parse(xml); 

     doc.getDocumentElement().normalize(); 

     NodeList nList = doc.getElementsByTagName("Error"); 

     for (int temp = 0; temp < nList.getLength(); temp++) { 

      Node nNode = nList.item(temp); 

      if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

       Element eElement = (Element) nNode; 

       Error _error = new Error(); 

       _error.setKey(eElement.getAttribute("id")); 
       _error.setHeading1(eElement.getElementsByTagName("Heading1").item(0).getTextContent()); 
       _error.setHeading2(eElement.getElementsByTagName("Heading2").item(0).getTextContent()); 
       _error.setDescription1(eElement.getElementsByTagName("Description1").item(0).getTextContent()); 
       _error.setDescription2(eElement.getElementsByTagName("Description2").item(0).getTextContent()); 
       _error.setDescription3(eElement.getElementsByTagName("Description3").item(0).getTextContent()); 
     _error.setDescription5(eElement.getElementsByTagName("Description4").item(0).getTextContent()); 

       getErrors().add(_error); 

어떻게 해당 섹션에서 오류 메시지를로드하는 문화 ID를 지정 가야합니까? 예를 들어 중국어로 전환하면 오류 culture = "zh"를 찾고 해당 섹션 만 구문 분석합니까? 감사합니다. .

+0

여기에서 중요한 질문은 왜 그런 식으로 구현 했는가입니다. 평범하고 오래된 자바 속성 파일에 어떤 문제가 있습니까? 번역은 어때? 어떻게 그들이 이것을 번역한다고 생각하니? 그것은 유지 보수가 불가능한 것 같습니다 ... –

답변

0

for 루프에서 nNode의 속성을 가져 와서 확인할 수 있어야합니다.

if ("zh".equals(nNode.getAttribute("culture"))) { 
      // do it here... 
} 
+0

고맙습니다. 나는 일하도록했다. 로케일이 'culture'와 일치하면 오류를 구문 분석하기 위해 for 루프를 사용합니다. – user3354252