如何使用Java创建如下所示的XML文件?我试过但我无法获得内部标签
<FlyBoy>
<learJet>CL-215</learJet>
<rank>2</rank>
<FlyBoy>
<viper>Mark II</viper>
<rank>1</rank>
<FlyBoy>
<viper>Mark II4455</viper>
<rank>2</rank>
<FlyBoy>
<viper>Mark II56666</viper>
<rank>3</rank>
</FlyBoy>
</FlyBoy>
</FlyBoy>
</FlyBoy>
答案 0 :(得分:2)
结构不是通常的结构。但是如果你想要它仍然可以使用下面的代码生成
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class XmlGenerator {
Document document = null;
Element root = null;
private void generateXml() {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
document = documentBuilderFactory.newDocumentBuilder().newDocument();
root = document.createElement("FlyBoy");
document.appendChild(root);
Element learJet = document.createElement("learJet");
learJet.setTextContent("CL-215");
root.appendChild(learJet);
Element rank = document.createElement("rank");
rank.setTextContent("2");
root.appendChild(rank);
Element flyBoy1 = renderFlyBoy(root, "Mark II", "1");
Element flyBoy2 = renderFlyBoy(flyBoy1, "Mark II4455", "2");
Element flyBoy3 = renderFlyBoy(flyBoy2, "Mark II56666", "3");
DOMSource domSource = new DOMSource(document);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
StreamResult result = new StreamResult(new File("my.xml"));
transformer.transform(domSource, result);
} catch (ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException e) {
e.printStackTrace();
}
}
/**
* Render flyboy
*
*/
private Element renderFlyBoy(Element parent, String viper, String rank) {
Element flyBoyEl = document.createElement("FlyBoy");
parent.appendChild(flyBoyEl);
Element viperEl = document.createElement("viper");
viperEl.setTextContent(viper);
flyBoyEl.appendChild(viperEl);
Element rankEl = document.createElement("rank");
rankEl.setTextContent(rank);
flyBoyEl.appendChild(rankEl);
return flyBoyEl;
}
// Test
public static void main(String[] args) {
XmlGenerator ob = new XmlGenerator();
try {
ob.generateXml();
System.out.println("done...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用此代码生成的xml如下,
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<FlyBoy>
<learJet>CL-215</learJet>
<rank>2</rank>
<FlyBoy>
<viper>Mark II</viper>
<rank>1</rank>
<FlyBoy>
<viper>Mark II4455</viper>
<rank>2</rank>
<FlyBoy>
<viper>Mark II56666</viper>
<rank>3</rank>
</FlyBoy>
</FlyBoy>
</FlyBoy>
</FlyBoy>