gpt4 book ai didi

java - 解析 XML 文件时出现异常

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

当我调用 mehtod getIdFromDatabase 从我的 XML 数据库获取 id 时,但显示异常 [ fatal error ] :1:1:序言中不允许内容。

调用方法getIdFromDatabase:

nameLayer =getIdLayerFromDatabase("//Project/Layer[Name="+"'"+Name+"'"+"]/@idLayer","ProjectDataBase.xml");

java中的方法:

 public int getIdFromDatabase(String PathXPath, String Path) throws JAXBException {
int maxid = -1, id = -1;
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = domFactory.newDocumentBuilder();
//***** exception here******************************
Document dDoc = builder.parse(new InputSource(new ByteArrayInputStream(Path.getBytes("UTF-8"))));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xPath.evaluate(PathXPath, dDoc, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); i++) {
id = Integer.parseInt(nl.item(i).getNodeValue());
}

} catch (Exception e) {
e.printStackTrace();
}
return id;
}

XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<Project xmlns="http://www.example.org/ProjectDataBase" name="شئؤ" location="Projects//شئؤ//ProjectDataBase.xml" CreationDate="2014-07-20">
<Layer idLayer="0">
<Name>طبقة 1</Name>
</Layer>
</Project>

最佳答案

问题是您正在尝试解析路径而不是文件。更改行:

Document dDoc = builder.parse(new InputSource(new ByteArrayInputStream(Path.getBytes("UTF-8"))));

作者:

Document dDoc = builder.parse(new FileInputStream(Path));

关于java - 解析 XML 文件时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24851581/

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