我知道此网站已多次询问此问题,但以前的答案都没有对我有用。 我已经编写了以下代码,其中有两种不同的方法
1.CreateXmlFile -> This method creates an xml file with root tag
2.WriteXmlFile -> This method adds new nodes to the already created xml.
可能这是我没有得到适当缩进的原因。 代码如下:
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();
}
}
}
我收到以下输出:
<?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>
预期输出是:
<?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>
仔细观察ITEM标签的缩进是否存在问题。 请帮助我。
答案 0 :(得分:0)
我无法弄清楚缩进为什么不能正常工作,但我确实有一个解决办法:只有在循环完成后才执行缩进。每次当你再次添加元素时,改变缩进都没有意义。
我添加了一个额外的方法prettyPrint()
,其唯一目的是美化最终的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();
}
}
}