You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by "Chuck H. Zhao" <ch...@usa.net> on 2001/03/09 20:32:37 UTC

serialize xml with internal dtd

When I serialize a DOM object with internal DTD, the output is no longer a 
well-formed xml document.

For example, I simply construct a DOM ojbect from dummy.xml and then 
serialize it:

dummy.xml:
<?xml version="1.0"?>
<!DOCTYPE foo  [
	<!ELEMENT foo (bar*)>
	<!ELEMENT bar (#PCDATA)>
]>
<foo>
	<bar> blar... </bar>
</foo>

TestSerialize.java:
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.apache.xml.serialize.*;

public class TestSerialize {

	public static void main(String[] argv) {
		try {
			System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
				 "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
			DocumentBuilderFactory dbf =  DocumentBuilderFactory.newInstance();
			dbf.setValidating(true);
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse("file:///d:/dummy.xml");
			java.io.StringWriter sw = new java.io.StringWriter();
			XMLSerializer xmlSer = new XMLSerializer(sw, null);
			xmlSer.serialize(doc);
			System.out.println(sw.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output:

	<!ELEMENT foo (bar*)>
	<!ELEMENT bar (#PCDATA)>
<?xml version="1.0"?>
<foo>  <bar> blar... </bar> </foo>

This is not well-formed.  I tried to use OutputFormat class and still got 
the same result.  The only thing I can do is to call
			xmlSer.serialize(doc.getDocumentElement());
In which case I lose the DTD altogether.

Is the output format by design or is it a bug?  Is there anyway to generate 
a well-formed xml with internal DTD?

--Chuck Zhao



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org