Skip to main content

Not getting internal DTD in written xml.

1 reply [Last post]
Joined: 2008-03-27

I have an xml file...
<?xml version="1.0" encoding="utf-8"?>


Oberon's Legacy
Corets, Eva
5.95 you see, with an internal dtd.

I am reading it with an XMLStreamWriter, allocating events for all XMLStreamConstants types,
and writing them to the XmlEventWriter with add(). Later on, I parse and load as a dom,
which I do as follows:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
builder.setErrorHandler(myHandler); // if null, uses default handler
try {
XMLEvent ev;
// Create an Event from the current event of the Reader.
// Then write it to a pipe, and parse (load) the piped InputStream as a Node.
PipedOutputStream ps = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(ps);
try {
XMLEventWriter writer = XMLOutputFactory.
while (reader.hasNext()) {
ev = p0.getAllocator().allocate(;
catch (XMLStreamException e){
throw new XmlException(e.getMessage());

Node node = builder.parse(is); // problem starts HERE

This 'node' above is later output as an xml file, and the dtd is missing.
When I do a 'toString()' on the events added to the writer, I see the whole DTD.
But when I look at the Node, and when I traverse the DOM, and when I output the
DOM, no DTD.

Did I do something wrong?
Everything else works fine. There is an error above in the xml, and that is handled
just fine. It's all working great, but the DTD disappears.

Suggestions? Corrections?


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-12-15

Hi Minka99,

For the purpose of creating a Document from a file, DOM can be used directly to parse the file and return a Document, in which case there's no need to go through a StAX read-and-write. If you do need the StAX read/write part in your application for other reasons, you could pair the Event Writer with an Event Reader or use the Event Factory to avoid reference to implementation class.