2011-12-27 2 views
1

변환기 용 config가있는 xml 파일이 있습니다. 아파치 코 몬스 구성으로 설정 읽기

<converter> 
    <replace> 
    <mask from="171, 187, 147, 148" to="34"/> 
    <mask from="150, 151" to="45"/> 
    </replace> 
</converter> 

내가 아파치 코 몬즈 구성을 사용하고이 설정을 읽을 수 있습니다. 태그를 반복하여 코드에서 속성을 처리하려면 "마스크"태그를 어떻게 읽을 수 있습니까?

답변

1

제 해결책은 xpath를 사용하는 것입니다.

  XPath xpath = XPathFactory.newInstance().newXPath(); 
     XPathExpression fromAttribute = xpath.compile("@from"); 
     XPathExpression toAttribute = xpath.compile("@to"); 

     NodeList list = (NodeList) xpath.evaluate("/converter/replace/mask", 
        ((XMLConfiguration) configuration).getDocument(), XPathConstants.NODESET); 

     for (int i = 0; i < list.getLength(); i++) { 
      Node node = list.item(i); 
      String from = fromAttribute.evaluate(node); 
      String to = toAttribute.evaluate(node); 

      //... 
     } 
관련 문제