Crimson Question : missing Namespace declaration

"Tahura Chaudhry" <[email protected]>
Newsgroups gmane.text.xml.general
Message-ID <[email protected]>
Hi,

I am attempting to creating an XML document and then serialize it into a 
Java string.  My code looks like:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();

Element root = maindoc.createElementNS("http://www.foo.com", "ns1:data");
doc.appendChild(root);

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(root);
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
transformer.transform(source, result);
String xmlString = sw.toString();

When I print out the XML string, I see that the namespace declaration is 
missing. The prefix "ns1" is used but not declared. The XML looks like:
<?xml version="1.0" encoding="UTF-8"?>
<ns1:data/>

What do I need to do to get the namespace declaration to appear in the XML 
string.

thanks,
Tahura Chaudhry
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.