3

我需要将命名空间定义添加到元素,因为当使用 apache xmlbean 生成 xml 时它没有被添加。如何使用 xmlbeans API 实现这一目标?

4

2 回答 2

3

我找到了问题的答案。事情是这样的。

XmlCursor cursor= targetObject.newCursor();
cursor.toNextToken();
cursor.insertNamespace("A", "namespace1");
//For example
cursor.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
cursor.dispose();
于 2010-06-16T08:54:43.713 回答
0

采用:

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);
于 2013-03-29T09:01:47.233 回答