Hi, there:
When executing the following piece of code, I got this exception:
Caused by: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
Document doc = null;
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder =
builderFactory.newDocumentBuilder();
doc = builder.newDocument();
// add root element
org.w3c.dom.Element root = doc.createElementNS( "http://www.w3.org/2001/XMLSchema",
"xsd:root" );
root.setAttribute( "targetNamespace", "http://www.w3.org/2001/XMLSchema" );
root.setAttribute( "elementFormDefault", "qualified" );
root.setAttribute( "attributeFormDefault", "unqualified" );
root.setAttribute( "version", "1.2" );
doc.appendChild( root );
I guess the reason this happens is because when I create the document instance, root element is already created as well? Since I need to add attributes to root element, I have to create it and attach it to document. Or there is some other reason this happens? How can I avoid this kind of error?
thanks