Hi,
I'm converting an application from using JDOM to SAX because I can no longer afford to have the whole XML in memory.
From what I can understand, the best way of generating XML is to use a JAXP Transformer. However when I do this, namespaces don't seem to be generated properly. Also, for no apparent reason, when reading the resulting XML back into JDOM, I get various errors, including "Content is not allowed in prolog" and "Prefix is not bound".
What I would expect in the output is something like....
<?xml version="1.0" encoding="UTF-8"?>
<package:package xmlns:package="http://preservation.naa.gov.au/package/1.0"></package:package>
But what I get is....
<?xml version="1.0" encoding="UTF-8"?> <package:package></package:package>
However, the resulting output and header looks well-formed, and thus the "Content is not allowed in prolog" seems inexplicable.
My code looks something like this...
SAXTransformerFactory tf = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
TransformerHandler th = tf.newTransformerHandler();
OutputStream out = new FileOutputStream(file);
StreamResult streamResult = new StreamResult(out);
th.setResult(streamResult);
AttributesImpl att = new AttributesImpl();
th.startElement("http://preservation.naa.gov.au/package/1.0", "package", "package:package", att);
th.endElement("http://preservation.naa.gov.au/package/1.0", "package", "package:package");
Is this the correct way to generate XML?
Why aren't namespaces working?
