gpt4 book ai didi

java - 创建具有多个子元素的 XML 文件

转载 作者:行者123 更新时间:2023-12-02 13:21:54 25 4
gpt4 key购买 nike

我有 ArrayList 对象中的 XML 元素的值。我必须创建具有以下结构的 XML 文件,其中元素带有子节点。

编号、名称和级别分别添加到数组列表对象alNumber、alName 和alLevel 中。值按以下顺序添加到 arraylist 对象中。

alNumber.get(1)=0000000042
alName.get(1)=Test Part2
alLevel.get(1)=1
alNumber.get(2)=0000000022
alName.get(2)=Test3243
alLevel.get(2)=1
alNumber.get(3)=0000000061
alName.get(3)=testpartsub
alLevel.get(3)=1
alNumber.get(4)=0000000083
alName.get(4)=testpartsub4
alLevel.get(4)=2

......

以下应该是 XML 文件的输出:

<?xml version="1.0" encoding="UTF-8"?>
<node name="Test Par1" number="0000000041">
<node name="Test Part2" number="0000000042"/> //Level - 1
<node name="Test3243" number="0000000022"/>
<node name="testpartsub" number="0000000061">
<node name="testpartsub4" number="0000000083"/> //<Level -2
<node name="testpartsub1" number="0000000081">
<node name="Test1" number="0000000001"/> //Level -3
<node name="Testpartsub3" number="0000000082">
<node name="Test2" number="0000000021"/> //Level - 4
</node>
</node>
</node>
<node name="testpartsub2" number="0000000062"/>
<node name="Test Part4" number="0000000044">
<node name="testpart4sub1" number="0000000084"/>
</node>
<node name="Test Part3" number="0000000043">
<node name="Test Part5" number="0000000045"/>
<node name="Test Part6" number="0000000046">
<node name="Test334344" number="0000000047"/>
<node name="Test Part4" number="0000000044">
<node name="testpart4sub1" number="0000000084"/>
</node>
</node>
</node>
<node name="Testpartsub3" number="0000000082">
<node name="Test2" number="0000000021"/>
</node>
</node>

这是我的代码:

DocumentBuilderFactory xmlDocFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder xmlDocBuilder = xmlDocFactory.newDocumentBuilder();
System.out.println("**Creating XML File ...");

xmlDocument = xmlDocBuilder.newDocument();
rootElement = xmlDocument.createElement("node");
rootElement.setAttribute("number", wtpart.getNumber());
rootElement.setAttribute("name", wtpart.getName());
xmlDocument.appendChild(rootElement);

Iterator<Integer> iter = alLevel.iterator();

for (int i=1, j=i+1; i<alNumber.size() && i<alName.size() && i<alLevel.size(); i++,j++) {

if (alLevel.get(i) == 1) {
Element element = xmlDocument.createElement("node");
element.setAttribute("number", alNumber.get(i));
element.setAttribute("name", alName.get(i));
rootElement.appendChild(element);
prevElement = element;
}
else if (j<=i) {
if (alLevel.get(j)>alLevel.get(i)){
if (alLevel.get(j) != 1) {
Element element = xmlDocument.createElement("node");
element.setAttribute("number", alNumber.get(i));
element.setAttribute("name", alName.get(i));
prevElement.appendChild(element);
prevElement = element;
}}
else {
Element element = xmlDocument.createElement("node");
element.setAttribute("number", alNumber.get(i));
element.setAttribute("name", alName.get(i));
prevElement.appendChild(element);
}
}
else {
Element element = xmlDocument.createElement("node");
element.setAttribute("number", alNumber.get(i));
element.setAttribute("name", alName.get(i));
prevElement.appendChild(element);
}
}

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(xmlDocument);
StreamResult result = new StreamResult(new File("E:\\temp", "Test.xml") );
transformer.transform(source, result);

最佳答案

谢谢奥本兰德。 n 个级别都以这种方式工作。

for (int i = 0; i < alNumber.size(); i++) {
int currentLevel = alLevel.get(i);
Element node = xmlDocument.createElement("node");
node.setAttribute("number", alNumber.get(i));
node.setAttribute("name", alName.get(i));

int levelDifference = previousLevel - currentLevel + 1;

while(levelDifference > 0 ) {
elementStack.remove(elementStack.size() - 1);
levelDifference--;
}

if(i == 0) {
xmlDocument.appendChild(node);
} else {
Element parentElement = elementStack.get(currentLevel - 1);
parentElement.appendChild(node);
elementStack.set(currentLevel - 1, parentElement);
}
elementStack.add(node);
previousLevel = currentLevel;
}

关于java - 创建具有多个子元素的 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43535897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com