gpt4 book ai didi

java - SaxParseException-文件过早结束

转载 作者:太空宇宙 更新时间:2023-11-04 13:41:14 25 4
gpt4 key购买 nike

        File xml = new File("C:/members.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
documentBuilder = documentBuilderFactory.newDocumentBuilder();

Document document = documentBuilder.parse(xml);


document.getDocumentElement().normalize();

NodeList nodeList = document.getElementsByTagName("member");

实际上这个问题解析(xml)。我收到错误消息“文件提前结束”第一条记录添加xml文件。我找不到真正的解决方案。请帮助我

    <?xml version="1.0" encoding="UTF-8"?>
<members>
<member id="0">
<name>a</name>
<surname>a</surname>
<fatherName>a</fatherName>
<age>0</age>
<email>a@a.com</email>
<username>a</username>
<password>a</password>
</member>
</members>

最佳答案

您的示例有问题 - 以下代码运行时没有错误:

public void test() throws ParserConfigurationException, SAXException, IOException {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<members>"
+ "<member id=\"0\">"
+ "<name>a</name>"
+ "<surname>a</surname>"
+ "<fatherName>a</fatherName>"
+ "<age>0</age>"
+ "<email>a@a.com</email>"
+ "<username>a</username>"
+ "<password>a</password>"
+ "</member>"
+ "</members>";
//File xml = new File("C:/members.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

Document document = documentBuilder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));

document.getDocumentElement().normalize();

NodeList nodeList = document.getElementsByTagName("member");
}

关于java - SaxParseException-文件过早结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31240979/

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