简体   繁体   中英

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

I have this error with write and read xml file parsed.

those are function for write and read:

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();
    }
}

This is the error:

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)

It looks like you have <?xml version="1.0" encoding="utf-8"?> in your XML file. Remove it then try it!

This error usually happens when you load several times (at least two times) the file in the stream and then try to parse it. Check if you do not load the file two time in fXmlFile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM