I have created a xml with Xml bean and now it's time to write the created xml in a file. But when I am writing my file in the xml file it's unformatted. So, I need to help to write the formatted xml into the file. My Code is given below:

document contains the xml and it's a object of org.apache.xmlbeans.XmlObject

File file = new File("test.xml");
document.save(file);
有帮助吗?

解决方案

XmlBeans.save() uses toString() to get the XML string, which is not formatted. Due to this the string in your file is unformatted as well. For formatted output do the following:

XmlOptions xmlOptions = new XmlOptions();
xmlOptions.setSavePrettyPrint();
document.save(file, xmlOptions);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top