[英]Java - How does an xml document loads a DTD using XML Catalogs?
我想知道這一點,以便我可以將 xsl 轉換應用於 xml 文檔而不會丟失一些實體,例如–
如何告訴解析器(我不關心的任何解析器)使用哪個目錄,然后執行 xsl 轉換?,如何將新配置的解析器連接到轉換工廠。
下面的代碼表示我想在 xml 文件上執行的轉換(它工作正常)。 我只想知道如何添加 XML 目錄方法,以便 xml 文檔正確加載其 DTD 並繼續執行 xsl 轉換步驟。
try {
SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
Templates step1Template = stf.newTemplates(new StreamSource(
this.getClass().getResourceAsStream("xsltransformation_step1.xsl")
));
Templates step2Template = stf.newTemplates(new StreamSource(
this.getClass().getResourceAsStream("xsltransformation_step2.xsl")
));
Templates step3Template = stf.newTemplates(new StreamSource(
this.getClass().getResourceAsStream("xsltransformation_step3.xsl")
));
TransformerHandler th1 = stf.newTransformerHandler(step1Template);
TransformerHandler th2 = stf.newTransformerHandler(step2Template);
TransformerHandler th3 = stf.newTransformerHandler(step3Template);
StreamSource xmlStreamSource = new StreamSource(new File(xmlInputFile));
StreamResult outputStreamSource1 = new StreamResult(new File (outputNewFile1));
StreamResult outputStreamSource2 = new StreamResult(new File (outputNewFile2));
th1.setResult(new SAXResult(th2));
th2.setResult(new SAXResult(th3));
th3.setResult(outputStreamSource1);
Transformer t = stf.newTransformer();
t.transform(xmlStreamSource, new SAXResult(th1));
}catch (TransformerException e){
e.printStackTrace();
return false;
}
這是包含實體的 xmlInputFile 的示例
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE manual PUBLIC '-//docufy//Docufy Standard DTD 20080125//EN' '/system/cosimago/dtd/manual.dtd'>
<chapter>
<title>LEDs „5 – 8“ am CPU-Board prüfen</title>
<body>
<!-- just content -->
</body>
</chapter>
如果有好心人幫我解決這個問題,我會非常感激。
先感謝您。
安德烈斯
使用SAXTransformerFactory.newInstance()
創建您自己的 XML 解析器 ( XMLReader
) 是最簡單的。 然后使用XMLReader.setEntityResolver()
在解析器上設置CatalogResolver
。 然后將XMLReader
包裝在SAXSource
,並將其作為Source
對象提供給Transformer.transform()
。
使用 Saxon 還可以通過配置屬性間接提供實體解析器,但這要復雜得多,並且僅當您無法自己控制XMLReader
的創建和配置時才需要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.