gpt4 book ai didi

java - org.xml.sax.SAXParseException : processing instructions must not start with xml

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

我在解析写入和读取 xml 文件时遇到此错误。

这些是写入和读取的函数:

protected void write_xml_file(String file_name) {
//if (file_name == null) file_name = "spells.xml";
FileOutputStream fos;
try {
fos = openFileOutput(file_name, Context.MODE_APPEND);
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, "UTF-8");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "spells");
for (int j = 0; j < 3; j++) {
serializer.startTag(null, "spell");
serializer.text("asd" +j);
serializer.endTag(null, "spell");
}
serializer.endDocument();
serializer.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}

protected void read_xml_file(String file_name,Context context) {
try {
String path=context.getFilesDir() + File.separator + file_name;
File fXmlFile = new File(context.getFilesDir() + File.separator + file_name);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("spells");


for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

//System.out.println("\nCurrent Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

//System.out.println("spell id : " + eElement.getAttribute("id"));
//System.out.println("name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
//System.out.println("description : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
//System.out.println("school : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());

}
}


} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

这是错误:

org.xml.sax.SAXParseException: processing instructions must not start with xml (position:unknown @6:10 in java.io.InputStreamReader@b64434e)

最佳答案

org.xml.sax.SAXParseException: processing instructions must not start with xml (position:unknown @6:10 in java.io.InputStreamReader@b64434e)

看起来你有<?xml version="1.0" encoding="utf-8"?>在您的 XML 文件中。删除它然后尝试一下!

关于java - org.xml.sax.SAXParseException : processing instructions must not start with xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45186406/

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