Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding namespace definition to xml using apache xmlbeans

Tags:

java

xml

xmlbeans

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?

like image 535
nobody Avatar asked Dec 14 '25 09:12

nobody


2 Answers

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();
like image 150
nobody Avatar answered Dec 16 '25 23:12

nobody


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);
like image 32
pranav Avatar answered Dec 16 '25 23:12

pranav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!