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