当使用Apache xmlbean生成XML时,由于没有添加命名空间定义到元素中,因此我需要将其添加。如何使用xmlbeans API实现这一点?
XmlCursor cursor= targetObject.newCursor();
cursor.toNextToken();
cursor.insertNamespace("A", "namespace1");
//For example
cursor.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
cursor.dispose();
使用:
XmlOptions.setSaveSuggestedPrefixes()
XmlOptions xmlOptions = new XmlOptions();
xmlOptions.setSavePrettyPrint();
xmlOptions.setSavePrettyPrintIndent(4);
xmlOptions.setSaveAggressiveNamespaces();
HashMap<String, String> nsMap = new HashMap<String, String>();
nsMap.put("namespace1","A");
nsMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
xmlOptions.setSaveSuggestedPrefixes(nsMap);
// Create your XmlObject
<Your XmlObject>.save(new File("test.xml"),xmlOptions);