java - 使用 xml 解析时显示 null 的文档
问题描述
解析文档时出现错误。它显示一个空文档
错误:[请求处理失败;嵌套异常 java.lang.NullPointerException] 与根本原因 java.lang.NullPointerException
String subBuildFile = "E:\Data Sync 4.4\WEB-INF\Temp_Project\Project_3";
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(subBuildFile));
Node s = doc.getElementsByTagName("userList").item(1);
for(int i =2; i < doc.getElementsByTagName("user").getLength(); i++) {
doc.getElementsByTagName("userList").item(i)
.removeChild(doc.getElementsByTagName("userList").item(i));
}
解决方案
你的问题是你使用InputSource
不正确。
您正在调用new InputSource(String)
并且该构造函数是这样说的:
/**
* Create a new input source with a system identifier.
* ...
* If the system identifier is a URL, it must be fully
* resolved (it may not be a relative URL).
*
* @param systemId The system identifier (URI).
*/
public InputSource (String systemId){...}
而是使用此构造函数:
/** Create a new input source with a character stream. */
public InputSource (Reader characterStream) { ... }
并像这样使用它:
Document doc = dBuilder.parse(new InputSource(new StringReader(subBuildFile)));
推荐阅读
- excel - 条件函数如何与 Excel 等中的 find 函数结合使用?
- swiftui - SwiftUI,列表视图中项目的唯一步进器
- php - 未捕获的错误:找不到类 'MongoDB\BSON\UTCDateTime'
- javascript - 如何解决 TypeError: catalogs.map is not a function in react.js app
- xamarin - 使用 Azure“文本到语音”的 .NET 标准库是否应该受到平台影响?
- c# - 我如何连接 Blazor 和 SOAP api
- javascript - 为什么我得到的项目比我存储到数据库中的项目多?
- c - C中的非法指令4
- node.js - http-proxy 错误:必须提供正确的 URL 作为目标
- python - PIP 使用 C++ 构建安装 Python 模块 || 致命错误 C1083:无法打开包含文件:'winpty.h':没有这样的文件或目录