简体   繁体   中英

xml unexpected token error in android

I try parsing xml received the following error: "unexpected token error"

here myxml file

<?xml version="1.0" encoding="utf-8"?>
    <records>   
        <record date="11/12">
            <profile>
                <name>john</name>
                <sex>male</sex>
                <age>18</age>
            </profile>
            <profile>
                <name>bill</name>
                <sex>male</sex>
                <age>20</age>
            </profile>
            <profile>
                <name>jully</name>
                <sex>female</sex>
                <age>22</age>
            </profile>
        </record>
    </records>

and xml parsing code

DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new InputSource(new InputStreamReader(ist, "UTF-8"))); 

xml file is wrong? or... help would be appreciated

It might depends on the parser as well. I think there is a relate question KXmlParser throws "Unexpected token" exception at the start of RSS pasing :

Quoting vmironov 's answer:

So one reason might be that the xml file doesn't actually start with <?xml version="1.0" encoding="utf-8"?>. It starts with three special bytes EF BB BF which are Byte order mark .

InputStreamReader ( Which you are using here) doesn't handle these bytes automatically, so you have to handle them manually. The simplest way to it is to use BOMInpustStream available in Commons IO library.

Hope this helps.

I think your XML File is pretty fine.

But, if the xml is in a File ... Why don't you use 'FileInputStream' intead of 'InputSource'?:

        Document doc = db.parse(new FileInputStream(xmlFile) );

By the way, I'm goint to put here a small App that do whatyou wish and doesn't crash. It creates your xml file in the SDCArd, and then it sucesfully reads that xml and build the 'Document' object:

package com.example.xmlt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;


public class MainActivity extends Activity {


    public static String MYDIRECTORY = "MyDirectory";

    public static String FILENAME = "file1.xml";

    public static String XMLCONTENT =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
            "<records>\n"+
                "<record date=\"11/12\">\n"+
                    "<profile>\n"+
                        "<name>john</name>\n"+
                        "<sex>male</sex>\n"+
                        "<age>18</age>\n"+
                    "</profile>\n"+
                    "<profile>\n"+
                        "<name>bill</name>\n"+
                        "<sex>male</sex>\n"+
                        "<age>20</age>\n"+
                    "</profile>\n"+
                    "<profile>\n"+
                        "<name>jully</name>\n"+
                        "<sex>female</sex>\n"+
                        "<age>22</age>\n"+
                    "</profile>\n"+
               "</record>\n"+
            "</records>\n";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try{

            File xmlFile = createFile(getApplicationContext(),FILENAME,XMLCONTENT);

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new FileInputStream(xmlFile) );

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

    }



    private static File createFile(Context context, String filename, String fileContent)
            throws  FileNotFoundException, IOException{

        String baseDir = Environment.getExternalStorageDirectory().getPath();
        File f1 = new File( baseDir+ "/"+ MYDIRECTORY +"/");
        f1.mkdirs();

        File f2 = new File( baseDir+ "/"+ MYDIRECTORY +"/" + filename);
        FileOutputStream outputStream = new FileOutputStream(f2);
        outputStream.write(fileContent.getBytes());
        outputStream.close();
        return f2;
    }
}

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