Hi,

I have created two utility methods for setting and inserting namespace into
XmlObject:

   public static void setBeanElementNSPrefix(XmlObject xmlBean, String
prefix) {
       XmlCursor xmlCursor = xmlBean.newCursor();
       QName qn = xmlCursor.getName();
       qn = new QName(qn.getNamespaceURI(), qn.getLocalPart(), prefix);
       xmlCursor.setName(qn);
       xmlCursor.toNextToken();
       xmlCursor.insertNamespace(prefix, qn.getNamespaceURI());
       xmlCursor.dispose();
   }
   public static void addNStoBean(XmlObject xmlBean, String prefix, String
ns) {
       xmlCursor = xmlBean.newCursor();
       xmlCursor.toNextToken();
       xmlCursor.insertNamespace(prefix, ns);
       xmlCursor.dispose();
   }

If I call them in this order:
  addNStoBean(myObj, ...);
  setBeanElementNSPrefix(myObj, ...);
it works fine, but if I call in the reversed order:
  setBeanElementNSPrefix(myObj, ...);
  addNStoBean(myObj, ...);

I got:

Exception in thread "main"
org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
   at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(
XmlObjectBase.java:1213)
   at org.apache.xmlbeans.impl.values.XmlObjectBase.newCursor(
XmlObjectBase.java:243)
   at test.atypon.xmlbeans.BeanSigningTest.setBeanElementNSPrefix(
BeanSigningTest.java:121)

Further investigation found that the problem is with
        xmlCursor.setName(qn);

From the API point of view, it would be bad to require the caller to follow
the order. How can I prevent this error?

Thanks

Reply via email to