My current code is printing out the xml like this :
<type xmlns="http://www.example.com">
<OBJECT_TYPE xmlns="">x3000</OBJECT_TYPE>
- <prop xmlns="">
<DESCRIPTION>a very fast train</DESCRIPTION>
<PARENT>NULL</PARENT>
<VIRTUAL>0</VIRTUAL>
<VISIBLE>1</VISIBLE>
<PICTURE>NULL</PICTURE>
<HELP>NULL</HELP>
<MIN_NO>NULL</MIN_NO>
<MAX_NO>NULL</MAX_NO>
<NAME_FORMAT>NULL</NAME_FORMAT>
</prop>
</type>
But I want this output :
<type xmlns="http://www.example.com">
<OBJECT_TYPE>x3000</OBJECT_TYPE>
- <prop>
<DESCRIPTION>a very fast train</DESCRIPTION>
<PARENT>NULL</PARENT>
<VIRTUAL>0</VIRTUAL>
<VISIBLE>1</VISIBLE>
<PICTURE>NULL</PICTURE>
<HELP>NULL</HELP>
<MIN_NO>NULL</MIN_NO>
<MAX_NO>NULL</MAX_NO>
<NAME_FORMAT>NULL</NAME_FORMAT>
</prop>
</type>
How to do that ? This is my current code :
public void saveXmlToFile(Type objType, Properties property)
throws IOException, ParserConfigurationException, SAXException,
JDOMException {
File xmlFile = new File(XMLEditorService.getXMLEditorService()
.getFile());
org.jdom2.Document doc = new SAXBuilder().build(xmlFile);
Element root = doc.getRootElement();
Namespace ns = Namespace.getNamespace("http://www.example.com");
Element type = new Element("type");
Element prop = new Element("prop");
// Add <type> as a child of <root>
root.addContent(type);
// Set namespace on <type>
type.setNamespace(ns);
type.addContent(new Element("OBJECT_TYPE").setText(objType.getObjectType()));
// Turn off namespace on <prop>
prop.setNamespace(Namespace.NO_NAMESPACE);
// Add <prop> as a child of <type>
type.addContent(prop);
prop.addContent(new Element("DESCRIPTION").setText(property.getDescription()));
prop.addContent(new Element("PARENT").setText(property.getParent()));
prop.addContent(new Element("VIRTUAL").setText(property.getVirtual()));
prop.addContent(new Element("VISIBLE").setText(property.getVisible()));
prop.addContent(new Element("PICTURE").setText(property.getPicture()));
prop.addContent(new Element("HELP").setText(property.getHelp()));
prop.addContent(new Element("MIN_NO").setText(property.getMin_no()));
prop.addContent(new Element("MAX_NO").setText(property.getMax_no()));
prop.addContent(new Element("NAME_FORMAT").setText(property.getName_format()));
XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
// Create a new file and write XML to it
xmlOutput.output(doc, new FileOutputStream(new File(XMLEditorService.getXMLEditorService().getFile())));
System.out.println("Wrote to file");
}
, ns
to all theprop.addContent(....)
like I have in the code, did you? – Paryavi