gpt4 book ai didi

java - 尝试用java中的迭代器构建XML文件

转载 作者:行者123 更新时间:2023-12-02 05:50:49 26 4
gpt4 key购买 nike

我已经编写了一些代码,可以通过 java 中的系统属性创建 XML 文件。它完全按照我想要的方式工作,但我真的(真的)不喜欢我最终只使用 5 个 if 语句来让它工作,因为我注意到没有一个属性超出 4 个分隔子字符串。 我更喜欢使用迭代器和某种 hasNext() 方法来继续附加到元素上,直到字符串末尾,但我无法解决任何问题。我找不到一种方法将最新的标签附加到循环中的最后一个标签/将值添加到元素的末尾。

这是我目前所拥有的让程序运行的快速修复方法。

Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("JAVA");
doc.appendChild(rootElement);

Iterator it = hm.entrySet().iterator();
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
String keyString = (String)entry.getKey();
String val = (String)entry.getValue();
java.util.List<String> sa = Arrays.asList(keyString.split("\\."));

Iterator ait = sa.iterator();

Element tag = doc.createElement((String) ait.next());
rootElement.appendChild(tag);

Element tag2 = null;
Element tag3 = null;
Element tag4 = null;
Element tag5 = null;

while(ait.hasNext())
{
if(ait.hasNext())
{
tag2 = doc.createElement((String)ait.next());
tag.appendChild(tag2);
if(!ait.hasNext())
tag2.appendChild(doc.createTextNode(val));

}
if(ait.hasNext())
{
tag3=doc.createElement((String)ait.next());
tag2.appendChild(tag3);
if(!ait.hasNext())
tag3.appendChild(doc.createTextNode(val));
}
if(ait.hasNext())
{
tag4=doc.createElement((String)ait.next());
tag3.appendChild(tag4);
if(!ait.hasNext())
tag4.appendChild(doc.createTextNode(val));
}
if(ait.hasNext())
{
tag5=doc.createElement((String)ait.next());
tag5.appendChild(tag5);
if(!ait.hasNext())
tag5.appendChild(doc.createTextNode(val));
}

}

}

Transformer transformer = null;
TransformerFactory transformerFactory = TransformerFactory.newInstance();
try
{
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("XMLtester"));

transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(source, result);

} catch (TransformerConfigurationException e)
{
e.printStackTrace();
} catch (TransformerException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
System.out.println("ERROR: " + e.toString());
}
System.out.println("File saved!");

如果有人对如何使其更加灵活或更优雅以便接受任意数量的分隔子字符串有任何想法,我会很感激。

最佳答案

我想这就是你想要的。

Element destination = rootElement;
Element tag = null;
while (ait.hasNext()) {
tag = doc.createElement((String) ait.next());
destination.appendChild(tag);
destination = tag;
}
destination.appendChild(doc.createTextNode(val));

示例输出:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<JAVA>
<java>
<runtime>
<name>Java(TM) SE Runtime Environment</name>
</runtime>
</java>
<sun>
<boot>
<library>
<path>/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/jre/lib</path>
</library>
</boot>
</sun>
<java>
<vm>
<version>23.0-b19</version>
</vm>
</java>
<user>
<country>
<format>GB</format>
</country>
</user>

完整的工作示例:

import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
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.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class StackOverflow23556822 {

public static void main(String[] args) throws ParserConfigurationException,
TransformerException {
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("JAVA");
doc.appendChild(rootElement);

Iterator<?> it = System.getProperties().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next();
String keyString = (String) entry.getKey();
String val = (String) entry.getValue();
List<String> sa = Arrays.asList(keyString.split("\\."));

Iterator<?> ait = sa.iterator();

Element destination = rootElement;
Element tag = null;
while (ait.hasNext()) {
tag = doc.createElement((String) ait.next());
destination.appendChild(tag);
destination = tag;
}
destination.appendChild(doc.createTextNode(val));

}

Transformer transformer = null;
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("xml-test.xml"));

transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(source, result);

System.out.println("File saved!");
}
}

关于java - 尝试用java中的迭代器构建XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23556822/

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