2017-01-10 8 views
1

폴더에 저장된 XSS 및 XSL을 사용하여 올바른 형식으로 XML을 표시하는 XML 파일이 있습니다. 내가 다음 코드를JEditorPane에 스타일 시트가있는 XML 표시

JEditorPane editor = new JEditorPane(); 
editor.setBounds(114, 65, 262, 186); 
frame.getContentPane().add(editor); 
editor.setContentType("html"); 
File file=new File("c:/r/testResult.xml"); 
editor.setPage(file.toURI().toURL()); 

모두 사용할 때 내가 볼 수는 스타일링없이 XML의 텍스트 부분입니다. 스타일 시트로이 디스플레이를 만들려면 어떻게해야합니까?

답변

1

JEditorPane은 XSLT 스타일 시트를 자동으로 처리하지 않습니다. 당신은 변화를 직접 수행해야합니다

try (InputStream xslt = getClass().getResourceAsStream("StyleSheet.xslt"); 
      InputStream xml = getClass().getResourceAsStream("Document.xml")) { 
     DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     Document doc = db.parse(xml); 

     StringWriter output = new StringWriter(); 
     TransformerFactory tf = TransformerFactory.newInstance(); 
     Transformer transformer = tf.newTransformer(new StreamSource(xslt)); 
     transformer.transform(new DOMSource(doc), new StreamResult(output)); 

     String html = output.toString(); 

     // JEditorPane doesn't like the META tag... 
     html = html.replace("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">", ""); 
     editor.setContentType("text/html; charset=UTF-8"); 

     editor.setText(html); 
    } catch (IOException | ParserConfigurationException | SAXException | TransformerException e) { 
     editor.setText("Unable to format document due to:\n\t" + e); 
    } 
    editor.setCaretPosition(0); 

가 특정 xsltxml 문서에 대한 적절한 InputStream 또는 StreamSource를 사용합니다.

+0

감사합니다. 많이 도움이되었습니다. – sam