37198

SAXParser with local XML file

Question:

I've read several topic on how to read local XML file.

Most of them are using openRawResource(). However it's much more slower compared to getXML().

I wanted to use getXML for my project, it's there any way to use getXML with XMLReader.parse(), as SAX InputSource does not accept XmlResourceParser as parameter.

<strong>Edit:</strong> btw, when do the startElement endElement will be called? Is it right after XMLReader.parse? Is it possible to restart the parsing from the top when I read a certain value of attributes?

Thank you very much

Answer1:

You can use it(assume that you are keeping your xml in assets folder)

InputStream response=context.getAssets().open("your.xml");

and then

Xml.parse(response, Xml.Encoding.UTF_8,rootElement.getContentHandler());

Answer2:

try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); MyXMLHandler myXMLHandler = new MyXMLHandler(getApplicationContext()); reader.setContentHandler(myXMLHandler); reader.parse(new InputSource(getAssets().open("Getalloffersbyzipcoderesponse_xml.xml"))); // listView.setAdapter(new EventListAdapter(getApplicationContext(), 0, myXMLHandler.getEventList())); listView.setAdapter(new OffersListAdapter(getApplicationContext(), 0, myXMLHandler.getOffersList())); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }

Answer3:

If you want to use the 'getXML()'-function, you can check out this tutorial, whic show's how to use the 'XmlResourceParser'-Interface: <a href="http://android-er.blogspot.com/2010/04/read-xml-resources-in-android-using.html" rel="nofollow">Link</a>

Anothet way would be if you put the XML files in the '<a href="http://developer.android.com/reference/android/content/res/Resources.html#openRawResource%28int,%20android.util.TypedValue%29" rel="nofollow">openRawResource</a>'-method, which returns an 'InputStream' on a given Resource ID, which you can then pass to the normal 'SAXParser'.

Recommend

  • PHP, MySQL - load around 100k records and export them to xml
  • Error while generating pdf from Html file in Java using iText [closed]
  • SAXParser: handle only specific parent childs
  • XML parsing error (RESOLVED) XML Sample
  • finding symmetric difference/unique elements in multiple arrays in javascript
  • Rails 4.0.0 jQuery Mobile button icons not showing in production
  • Keep Sql Connection open for iterating many requests? Or close each step?
  • Simple test app deploys to Heroku but won't run
  • Deserialize Dictionary
  • Binary Tree Traversal Sum Of Each Depth
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • Avoid registering duplicate broadcast receivers in Android
  • Deploying a CodeRush plugin from the Community Site
  • how to get data attributes of dynamically generated element
  • What is the correct way to synchronize a shared, static object in Java?
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • jQuery: add elements until a particular height is reached
  • Regex to match a string not followed by anything
  • Combining two different ActiveRecord collections into one
  • ListItem.Attributes.Add not working
  • Parse a date string in a specific locale (not timezone!)
  • How to change the font size of a single index for UISegmentedControl?
  • How to use carriage return with multiple line?
  • Play WS (2.2.1): post/put large request
  • Change multiple background-images with jQuery
  • Algorithm for a smudge tool?
  • Xamarin Forms - UWP Fonts
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Master page gives error
  • HTML download movie download link
  • Can Jackson SerializationFeature be overridden per field or class?
  • Sony Xperia Z Tablet not found by adb
  • Deserializing XML into class C#
  • Why winpcap requires both .lib and .dll to run?
  • Is there a mandatory requirement to switch app.yaml?
  • Android Studio and gradle
  • How to set the response of a form post action to a iframe source?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • How to Embed XSL into XML