the code below produces a simple XML version of a DTD using the DeclHandler
interface
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.ext.DeclHandler;
import org.xml.sax.helpers.DefaultHandler;
public class DTD2XML {
public static Node extractDTD(String dtdFilePath) throws IOException {
final String testXML = "<?xml version='1.0'?>"
+ "<!DOCTYPE root SYSTEM '" + dtdFilePath + "'>"
+ "<root/>";
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
DtdHandler dtdHandler = new DtdHandler();
// link the DeclHandler into the parser
parser.setProperty("http://xml.org/sax/properties/declaration-handler",
dtdHandler);
parser.parse(new InputSource(new StringReader(testXML)), new
DefaultHandler());
return dtdHandler.getDoc();
} catch (SAXException sax) {
sax.printStackTrace();
Exception root = sax.getException();
if (root != null) {
System.out.println("\nroot-cause =");
root.printStackTrace();
}
} catch (ParserConfigurationException pcx) {
pcx.printStackTrace();
}
return null;
}
/**
* this class builds a simple DOM document from the DTD info
*/
private static class DtdHandler implements DeclHandler {
private Document _doc;
private Element _root;
public DtdHandler() throws ParserConfigurationException {
DocumentBuilderFactory docFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docFactory.newDocumentBuilder();
_doc = builder.newDocument();
_root = _doc.createElement("DTD");
_doc.appendChild(_root);
}
public Document getDoc() {
return _doc;
}
public void elementDecl (String name, String model) throws SAXException
{
Element elem = _doc.createElement("elementDecl");
elem.setAttribute("name", name);
elem.setAttribute("model", model);
_root.appendChild(elem);
}
public void attributeDecl (String eName, String aName, String type,
String valueDefault, String value) throws SAXException {
Element elem = _doc.createElement("attributeDecl");
elem.setAttribute("eName", eName);
elem.setAttribute("aName", aName);
elem.setAttribute("type", type);
elem.setAttribute("valueDefault", valueDefault);
elem.setAttribute("value", value);
_root.appendChild(elem);
}
public void internalEntityDecl (String name, String value) throws
SAXException {
Element elem = _doc.createElement("internalEntityDecl");
elem.setAttribute("name", name);
elem.setAttribute("value", value);
_root.appendChild(elem);
}
public void externalEntityDecl (String name, String publicId, String
systemId)
throws SAXException {
Element elem = _doc.createElement("externalEntityDecl");
elem.setAttribute("name", name);
elem.setAttribute("publicId", publicId);
elem.setAttribute("systemId", systemId);
_root.appendChild(elem);
}
}
}
once you have the DTD's DOM you can use the information however you need it.
HTH.
...................ron.
-----Original Message-----
From: Igor PARTL [mailto:[EMAIL PROTECTED]
Sent: Friday, November 09, 2001 7:01 AM
To: Xerces-J-User
Subject: Working with DTD
Hi!
I've no suggestion, where I could begin. Perhaps someone can give me a
point to start (better: parts of code):
I want to read an dtd.
Then I want to build a xml-file.
Now I've been searching for a method, that give me the possible
elements at one point in the xml-file.
Example:
<!ELEMENT a (b|c)*>
...
<xml>
<a>
<b/>
POINT ...-> The answer is: Possible elements are 'b' or 'c'.
</a>
</xml>
Igor PARTL
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]