XML Dom Parser throws error input source needs either stream

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

XML Dom Parser throws error input source needs either stream

Postby makrandmane » Mon Mar 23, 2009 1:12 pm

Hello

I am new to android development, right now i am doing a simple project that receive data from XML and display on screen.
for retrieving data from XMl file I have used Dom parser but at run time it will throw an error. The SAX Exception " input source needs either stream or reader"

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.   Document dom;
  2.  
  3.                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  4.  
  5.                 DocumentBuilder db = dbf.newDocumentBuilder();
  6.  
  7.                 dom = db.parse("assets/myfile.xml");
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


This code is working fine with out side android or other java project.

I have tried for
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                File f = new File ("assets/myfile.xml");
  2.  
  3.                InputStream str =  f.toURL().openStream();
  4.  
  5.                dom = db.parse(str);
Parsed in 0.055 seconds, using GeSHi 1.0.8.4

and same for by using reader. but sill i got same kind of error,

Please tell me what is correct way to use Stream or Reader.

Thanks in advance :)
makrandmane
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon Mar 23, 2009 1:07 pm

Top

Postby Alexandro » Wed Mar 25, 2009 5:12 pm

To read assets files use AssetsManager

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. AssetManager assetManager = context.getResources().getAssets();
  3.  
  4.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


So your code will be

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. // getResources is a method of Context
  3.  
  4. AssetManager assetManager = getResources().getAssets();
  5.  
  6. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  7.  
  8. DocumentBuilder db = dbf.newDocumentBuilder();
  9.  
  10. db.parse(assetManager.open("assets/myfile.xml"));
  11.  
  12.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
Alexandro
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 05, 2009 5:42 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google [Bot] and 6 guests