I have the following codeblock at the beginning of building an XML in Java. ROOT_ELEM is obviously the root element.
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.newDocument();
root = doc.createElementNS(NS,"xsi:" + ROOT_ELEM);
doc.appendChild(root);
} catch (ParserConfigurationException e) {
LOG.error("Unable to create an XML document.", e);
}
The root = doc.createElementNS(NS,"xsi:" + ROOT_ELEM);
part adds the namespace with the xsi prefix for the XML line:
<xsi:rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
My problem is that I need the xsi
on the xmlns:xsi=...
part but not on the xsi:rootElement
part. Is this possible? I know I can convert the whole thing to a String
and do a search and replace but that's hackish and I wanted to know if there is a way to do with methodically with the DOM Parser api. Here is the line I want:
<rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">