解析了写入和读取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:处理指令不能启动 with xml(position:unknown @ 6:10 in java.io.InputStreamReader@b64434e)
答案 0 :(得分:1)
org.xml.sax.SAXParseException:处理指令不能以xml开头(位置:java.io.InputStreamReader@b64434e中的未知@ 6:10)
您的XML文件中似乎有<?xml version="1.0" encoding="utf-8"?>
。删除它,然后尝试!
答案 1 :(得分:0)
当您在流中加载几次(至少两次)文件然后尝试解析它时,通常会发生此错误。检查是否没有两次在fXmlFile中加载文件