Hi I've wrote the following helper method to create or replace an XML element into an XML document (this may help others).
I notice that I must specify the default namespace into my XML element (elt parameter below), otherwise when the element is inserted xmlbeans add a default one with no value ?! If I add an XML element <rule> into a <ruleset> document, I get: <ruleset xmlns="urn:ietf:params:xml:ns:common-policy"> <rule id="rule_1253695448_0" *xmlns=""*> [...] </rule> </ruleset> ... and of course it does not validate against the schema. What should I do in order to remove this value ? Or what should I do to automatically set a default namespace to elt if it has none ? Here's the code of my method, making use of XmlCursor.copyXml(): public static void createReplaceElement(XmlObject doc, String namespace, XmlObject elt, String xpathQuery) { int index; // Create a cursor and move it to the position defined by XPath query XmlCursor docCursor = doc.newCursor(); String query = "declare default element namespace '" + namespace + "';" + xpathQuery; docCursor.selectPath(query); // If element already exists, delete it first (Replace case) if (docCursor.toNextSelection()) { docCursor.removeXml(); // keep the cursor to insert the new element in the same place. } // Create case (element does not already exist) else { System.out.println("XmlBeansHelper: Element doesn't exist yet"); index = xpathQuery.lastIndexOf("/"); if (index != -1) { String q = xpathQuery.substring(0, index); query ="declare default element namespace '" + namespace + "';" + q; docCursor.selectPath(query); if (docCursor.toNextSelection()) { if (!docCursor.toFirstChild()) docCursor.toEndToken(); } else return; } } // Element cursor XmlCursor eltCursor = elt.newCursor(); if (eltCursor.currentTokenType().isStartdoc()) eltCursor.toNextToken(); eltCursor.copyXml(docCursor); docCursor.dispose(); eltCursor.dispose(); } Best Regards, Pascal