gpt4 book ai didi

java - 在 Java 中展平 XML 文档

转载 作者:行者123 更新时间:2023-11-29 08:33:07 24 4
gpt4 key购买 nike

有没有办法通过将所有嵌套标签/数组移除为简单的键/值对来展平 XML 文档。比如我有一个例子

<root>
<a>
<b>some-value1</b>
<c>some-value2</c>
</a>
<a>
<b>some-value3</b>
<c>some-value4</c>
</a>
<p>some-value-p</p>

root.a.0.b=some-value1
root.a.0.c=some-value2
root.a.1.b=some-value3
root.a.1.c=some-value4
root.p=some-value-p

我正在寻找 Java 或 Scala 的解决方案?

最佳答案

这是一个没有外部依赖的解决方案。你可以try it !

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class Main {

private static final String xml =
"<root>\n" +
" <a>\n" +
" <b>some-value1</b>\n" +
" <c>some-value2</c>\n" +
" </a>\n" +
" <a>\n" +
" <b>some-value3</b>\n" +
" <c>some-value4</c>\n" +
" </a>\n" +
" <p>some-value-p</p>\n" +
"</root>";

public static void main(String[] args) {
try {
byte[] bytes = xml.getBytes(StandardCharsets.UTF_8);
try (InputStream input = new ByteArrayInputStream(bytes)) {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
Element root = document.getDocumentElement();
flattXml("", root);
}
} catch (IOException | ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
}

private static void flattXml(String currentPath, Node currentNode) {
if (currentNode.getNodeType() == Node.TEXT_NODE &&
!currentNode.getNodeValue().trim().isEmpty()) {
System.out.println(currentPath + "=" + currentNode.getNodeValue());
} else {
NodeList childNodes = currentNode.getChildNodes();
int length = childNodes.getLength();
String nextPath = currentPath.isEmpty()
? currentNode.getNodeName()
: currentPath + "." + currentNode.getNodeName();
for (int i = 0; i < length; i++) {
Node item = childNodes.item(i);
flattXml(nextPath, item);
}
}
}

}

控制台输出:

root.a.b=some-value1
root.a.c=some-value2
root.a.b=some-value3
root.a.c=some-value4
root.p=some-value-p

关于java - 在 Java 中展平 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46299727/

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