Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HIERARCHY_REQUEST_ERR

843834Nov 30 2006 — edited Nov 30 2006
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 28 2006
Added on Nov 30 2006
3 comments
224 views