I am aware that this question has been asked multiple times on this site, however none of the previous answers have worked for me. I have written the following code where in i have 2 different methods
1.CreateXmlFile -> This method creates an xml file with root tag
2.WriteXmlFile -> This method adds new nodes to the already created xml.
May be this is the reason am not getting proper indentation. The code is as follows:
public class CreateXmlDemo {
public static void main(String[] args) {
CreateXmlDemo c1 = new CreateXmlDemo();
c1.createXmlFile();
CreateXmlDemo c2 = new CreateXmlDemo();
CreateXmlDemo c3 = new CreateXmlDemo();
CreateXmlDemo c4 = new CreateXmlDemo();
CreateXmlDemo[] items= {c2,c3,c4};
for(CreateXmlDemo item : items){
item.writeXml();
}
}
public void createXmlFile(){
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("ITEMS");
doc.appendChild(rootElement);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("D:\\Faiz\\output.xml"));
transformer.transform(source, result);
}catch(Exception e){
e.printStackTrace();
}
}
public void writeXml(){
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse("D:\\Faiz\\output.xml");
doc.normalize();
Element rootElement = doc.getDocumentElement();
Element item = doc.createElement("ITEM");
Element id = doc.createElement("ID");
id.appendChild(doc.createTextNode("1"));
item.appendChild(id);
Element name = doc.createElement("Name");
name.appendChild(doc.createTextNode("James"));
item.appendChild(name);
rootElement.appendChild(item);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("D:\\Faiz\\output.xml"));
transformer.transform(source, result);
}catch(Exception e){
e.printStackTrace();
}
}
}
I am getting the Following output:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ITEMS>
<ITEM>
<ID>1</ID>
<Name>James</Name>
</ITEM>
<ITEM>
<ID>1</ID>
<Name>James</Name>
</ITEM>
<ITEM>
<ID>1</ID>
<Name>James</Name>
</ITEM>
</ITEMS>
The Expected Output is:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ITEMS>
<ITEM>
<ID>1</ID>
<Name>James</Name>
</ITEM>
<ITEM>
<ID>1</ID>
<Name>James</Name>
</ITEM>
<ITEM>
<ID>1</ID>
<Name>James</Name>
</ITEM>
</ITEMS>
observecarefully there is a problem with the indentation of ITEM tag. please help me on this.
I can't figure out why the indentation isn't working properly, but I do have a work-around: perform the indentation only after the loop completes. It doesn't make sense anyway to change the indentation each time when you're just going to add elements again.
I've added an extra method prettyPrint()
whose only purpose is to beautify the final XML.
public class CreateXmlDemo {
private static String fileLocation = null;
public static void main(String[] args) {
fileLocation = "D:\\Faiz\\output.xml";
CreateXmlDemo c1 = new CreateXmlDemo();
c1.createXmlFile();
CreateXmlDemo c2 = new CreateXmlDemo();
CreateXmlDemo c3 = new CreateXmlDemo();
CreateXmlDemo c4 = new CreateXmlDemo();
CreateXmlDemo[] items= {c2,c3,c4};
for(CreateXmlDemo item : items){
item.writeXml();
}
c1.prettyPrint();
}
public void createXmlFile(){
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("ITEMS");
doc.appendChild(rootElement);
writeWithoutIndentation(doc);
}catch(Exception e){
e.printStackTrace();
}
}
public void writeXml(){
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(fileLocation);
doc.normalize();
Element rootElement = doc.getDocumentElement();
Element item = doc.createElement("ITEM");
Element id = doc.createElement("ID");
id.appendChild(doc.createTextNode("1"));
item.appendChild(id);
Element name = doc.createElement("Name");
name.appendChild(doc.createTextNode("James"));
item.appendChild(name);
rootElement.appendChild(item);
rootElement.normalize();
writeWithoutIndentation(doc);
}catch(Exception e){
e.printStackTrace();
}
}
public void writeWithoutIndentation(Document doc){
try{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(fileLocation));
transformer.transform(source, result);
}catch(Exception e){
e.printStackTrace();
}
}
public void prettyPrint(){
try{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(fileLocation);
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(fileLocation));
transformer.transform(source, result);
}
catch(Exception e){
e.printStackTrace();
}
}
}