how to create a DTD and integrate it in a JAR file..?
actually i have a java program that reads the data from a specific XML file. now i want to create a jar file for that program and开发者_JAVA百科 also include the corresponding DTD with it, so that any1 who uses my JAR can get its XML checked against that DTD.
Pls help, thanks in advance !
You will need to create a Resolver class which resolves public or system IDs for your DTD(s) to the copy of the DTD you package in your jar.
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 */
}
}
This will allow you to store an arbitrary file type in your .jar and retrieve that later. When you package your .jar, include the DTD file explicitly. For example:
jar cvfm myClass.jar manifest.mf *.class relative\path\to\file.dtd
Watch the direction of (back)slashes for your OS. Then, to retrieve that resource for use in your code, use:
getClass().getResource("relative/path/to/file.dtd")
精彩评论