實際上,我有一個Java程序,它從特定的XML文件中讀取數據。 現在我想爲該程序創建一個jar文件,並且還包含相應的DTD,這樣任何使用我的JAR的人都可以針對該DTD檢查其XML。如何創建DTD並將其集成到JAR文件中?
請幫助,提前致謝!
實際上,我有一個Java程序,它從特定的XML文件中讀取數據。 現在我想爲該程序創建一個jar文件,並且還包含相應的DTD,這樣任何使用我的JAR的人都可以針對該DTD檢查其XML。如何創建DTD並將其集成到JAR文件中?
請幫助,提前致謝!
您需要創建一種解決公共或系統ID爲您的DTD(縣)打包在DTD的副本解析器類你的罐子。
DocumentBuilderFactory factory = xmlFactories.newDocumentBuilderFactory();
factory.setNamespaceAware(true);
factory.setValidating(false);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
documentBuilder.setEntityResolver(new EntityManager());
......
public class EntityManager implements EntityResolver {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
/* code goes here to return contents of DTD */
}
}
這將允許您在.jar中存儲任意文件類型並在以後檢索。打包.jar時,請明確包含DTD文件。例如:
jar cvfm myClass.jar manifest.mf *.class relative\path\to\file.dtd
觀察操作系統的(後退)斜線的方向。然後,檢索利用該資源在你的代碼,使用:
getClass().getResource("relative/path/to/file.dtd")
哎thnks您的回覆.. 也許我並不清楚笏我想.. 我將重新幀我的問題。 – 2011-03-26 16:18:55
我在使用JAXB解組器時如何處理這個問題? – 2014-08-25 08:39:09
jaxb有一個API。也許寫一個新的問題? – bmargulies 2014-08-25 11:48:56