You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xml.apache.org by Tahura Chaudhry <ta...@hotmail.com> on 2005/05/24 22:21:42 UTC

Crimson Question : missing Namespace declaration

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: general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org