开发者

Adding namespace definition to xml using apache xmlbeans

开发者 https://www.devze.com 2023-01-02 11:00 出处:网络
I need to add namespace defintion to an element since its not getting added when when xml is generated using apache xmlbean.开发者_高级运维 How do I acheieve this using xmlbeans API?I have found answe

I need to add namespace defintion to an element since its not getting added when when xml is generated using apache xmlbean.开发者_高级运维 How do I acheieve this using xmlbeans API?


I have found answer to the problem. Here's how it is.

XmlCursor cursor= targetObject.newCursor();
cursor.toNextToken();
cursor.insertNamespace("A", "namespace1");
//For example
cursor.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
cursor.dispose();


Use:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号