gpt4 book ai didi

java - 使用 InputSource 将 XML 解析为字符串给出空文档

转载 作者:行者123 更新时间:2023-12-01 20:25:43 30 4
gpt4 key购买 nike

我正在尝试将 XML 解析为字符串。无论我是否使用 RESTful 调用的响应或简单字符串,当我解析它时,我都会得到一个空文档。我发现有几篇文章告诉我遵循代码(如下),但它似乎对我不起作用。我做错了什么?

import org.xml.sax.InputSource;
import org.w3c.dom.Document;
...
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String xml = responseEntity.getBody();
//String xml = "<foo\>";
Document doc = db.parse(new InputSource(new StringReader(xml)));

我关注了:In Java, how do I parse XML as a String instead of a file?

最佳答案

结果调试器显示[#document: null] 但事实上并非如此。我能够在 parse() 之后继续并找到我需要的元素。以前从未见过!

关于java - 使用 InputSource 将 XML 解析为字符串给出空文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58917162/

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