2013-08-30 100 views
0

我有一個Java類,它返回特定系統的狀態,然後返回一個新的ResponseEntity並從中生成一個XML文件。我想從文件中剝離XML標籤並僅顯示內容。從生成的XML文件去除條帶標籤

的Java:

@RequestMapping(method = RequestMethod.GET) 
    public ResponseEntity<StatusData> getStatus() throws IOException { 
     StatusData status = new StatusData(); 
     status.setIsDbUp(statusService.isDbUp()); 
     status.setIsAppUp(statusService.isAppUp()); 
     return new ResponseEntity<StatusData>(status, HttpStatus.OK); 
    } 

生成的XML:

<com.ck.app.StatusData> 
     <isDbUp>DB: UP</isDbUp> 
     <isAppUp>APP: UP</isAppUp> 
    </com.ck.app.StatusData> 

我寫了一個腳本,XSL,但我不能確定如何使用它。

回答

0

您可以通過DOM樹節點重複並打印文本節點。

public static void main(String[] args) throws IOException, ParseException, JAXBException, URISyntaxException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, NoSuchMethodException, SAXException, ParserConfigurationException { 
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
    Document doc = builder.parse(Main.class.getResourceAsStream("/file.xml")); 

    visit(doc); 
} 

public static void visit(Node node) { 
    NodeList nl = node.getChildNodes(); 

    for (int i = 0; i < nl.getLength(); i++) { 

     Node child = nl.item(i); 
     if (child.getNodeType() == Node.TEXT_NODE) 
      System.out.println(child.getTextContent()); 

     visit(child); 
    } 
}