创建的 XML 文件中的缩进不正确
Indentation Incorrect in XML file created
我知道这个问题已在此站点上被问过多次,但是 none 之前的答案对我有用。
我写了下面的代码,其中有两种不同的方法
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 标签的缩进有问题。
请帮助我。
我不明白为什么缩进不能正常工作,但我有一个解决方法:仅在循环完成后执行缩进。无论如何,每次要再次添加元素时都更改缩进是没有意义的。
我添加了一个额外的方法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();
}
}
}
我知道这个问题已在此站点上被问过多次,但是 none 之前的答案对我有用。 我写了下面的代码,其中有两种不同的方法
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 标签的缩进有问题。 请帮助我。
我不明白为什么缩进不能正常工作,但我有一个解决方法:仅在循环完成后执行缩进。无论如何,每次要再次添加元素时都更改缩进是没有意义的。
我添加了一个额外的方法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();
}
}
}