gpt4 book ai didi

java - 从 dom 文档创建 json

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

我有一个 org.w3c.dom.Document。生成xml是没有问题的。但是如何从文档中生成 json 呢?

这是获取xml字符串的代码

        TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");

// create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(document);
trans.transform(source, result);
String xmlString = sw.toString();

最佳答案

JSON 和 XML 的数据模型不同,因此不存在从 XML 到 JSON 的规范转换。

如果您可以提供您的数据,也许我们可以提出一些建议。

关于java - 从 dom 文档创建 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6761744/

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