Link to home
Start Free TrialLog in
Avatar of AssetFX
AssetFXFlag for Australia

asked on

Parsing FileInputStream for xml on Android

Hi,
I am running Android 4.0 API Level 14

This is my xml file
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<local_root />

Open in new window

This is my java
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
	
fileRead = new FileInputStream(localXMLfile);
Document doc = db.parse(fileRead);

doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("local_root");

Open in new window

It is throwing the following exception
org.xml.sax.SAXParseException: Unexpected token (position:TEXT ?xml version='1....@2:1 in java.io.InputStreamReader@413b7ab8)
on this line:
Document doc = db.parse(fileRead);

Open in new window


I thought it might be something to do with the UTF-8 encoding.

Any help would be greatly appreciated!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Make sure there's no BOM at the beginning of the file. Save the file in a simple text editor and it shouldn't get one
Avatar of AssetFX

ASKER

Hi,

this xml is created dynamically so saving each time in a text editor doesn't really cut it.

However just added in a check to see if it had BOM and it does not. The check comes from http://stackoverflow.com/questions/1835430/byte-order-mark-screws-up-file-reading-in-java
ASKER CERTIFIED SOLUTION
Avatar of Sharon Seth
Sharon Seth
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
See if you can attach one here without altering it in any way