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



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to