开发者

problem loading and parsing xml from resources

I´m have written a parser which parses an xml file from a from a HttpURLConnection. This works fine.

Problem: I need to rewrite this so that the xml file is loaded from local resources instead of from the internet, but I can't get this to work... Just to give yo an idea how the original web parser looks:

InputStream in=null;

URLConnection connection = url.openConnection(); 
HttpURLConnection httpConnection = (HttpURLConnection)connection; 
int responseCode = httpConnection.getResponseCode(); 

if (responseCode == HttpURLConnection.HTTP_OK) { 


   in = httpConnection.getInputStream(); 
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
   DocumentBuilder db = dbf.newDocumentBuilder();

   Document dom = db.parse(in);     
   Element docEle = dom.getDocumentElement();
   NodeList nl = docEle.getChildNodes();
   for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);

    //START PARSING......

Now here's the code I am using to try to parse from a local resource placed at xml/myfile.xml in the resources folder:

InputStream in=null;
in = mParentActivity.getResources().openRawResource(R.xml.myfile);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
                    
Document dom = builder.parse(in); // THIS IS WHERE I GET AN SAXParseException
                    
Element root = dom.getDocumentElement();
NodeList nl = root.getChildNodes();

for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);

 //START PARSING......

The local xm开发者_StackOverflow社区l file and the web file are exactly the same... If anyone would take a look at it: http://agens.no/Eniro/Android/SEWheel/Category2.plist.xml

And here's the stacktrace:

02-01 16:08:45.546: WARN/System.err(19703): org.xml.sax.SAXParseException: name expected (position:START_TAG @1:309 in java.io.InputStreamReader@47668728)


Found the answer.. When the file was i the res/xml folder, the inputstream showed a lot of invalid characters. When i put it in the res/raw folder it worked fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜