Serializing an XML Dom

  • Thread starter Tjerk Wolterink
  • Start date
T

Tjerk Wolterink

I have created an W3C XML DOM in java,
it uses namespaces.

Now i want to serialize it to the outputstream:

===
Document document=retrievePageDocument(page, manager.getDataStore());
response.setContentType("text/xml");
PrintWriter out=response.getWriter();

DOMSource domSource=new DOMSource(document);

StreamResult streamResult=new StreamResult(out);
TransformerFactory tf=TransformerFactory.newInstance();
Transformer serializer=tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "xml");
serializer.transform(domSource, streamResult);
===

The serialization works, but it does not include the namespaces!!

I also tried to use the Xerces Serialization API directly:

===
// lets serialize the xml to the output
XMLSerializer serializer=new XMLSerializer(out,
new OutputFormat(document, "UTF-8", true)
);
serializer.serialize(document);
===

But that also does not inlcude the namespaces??@?

The Document object is namespace aware:

===
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document doc;
doc=factory.newDocumentBuilder().newDocument();
Element root=doc.createElementNS(PAGE_NAMESPACE, "page");
doc.appendChild(root);
===


Pleaz help im stuck here!
 
M

Martin Honnen

Tjerk said:
I have created an W3C XML DOM in java,
it uses namespaces.
The serialization works, but it does not include the namespaces!!

The following

DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
Document xmlDocument =
documentBuilder.getDOMImplementation().createDocument(
"http://www.w3.org/1999/xhtml",
"html",
null
);
Element rootElement = xmlDocument.getDocumentElement();

rootElement.setAttributeNS("http://www.w3.org/XML/1998/namespace",
"xml:lang", "en");

System.out.println("xmlDocument class: " +
xmlDocument.getClass().getName());
System.out.println("Serialized document:");
System.out.println(serializeToString(xmlDocument));

run with Java 1.5 gives

xmlDocument class: com.sun.org.apache.xerces.internal.dom.DocumentImpl
Serialized document:
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"></html>

which looks fine, namespace declarations and prefixes are there.

Run with Java 1.4 however I get:

xmlDocument class: org.apache.crimson.tree.XmlDocument
Serialized document:
<?xml version="1.0" encoding="UTF-8"?>
<html xml:lang="en"/>

so you might have hit a bug with the DOM or Transformer implementation
you are using respectively which is on your classpath.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top