简体   繁体   中英

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 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM