1

Hello i am parsing xml by below code

       @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    InputStream is = getResources().openRawResource(R.raw.bd);
    try {
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = docBuilder.parse(is, null);
        doc.getDocumentElement().normalize();
        NodeList my_words = doc.getElementsByTagName("word");
        for (int i = 0; i < 100; i++) {
            System.out.println(((Element) my_words.item(i)).getAttribute("eng"));
            System.out.println(((Element) my_words.item(i)).getAttribute("guj"));
        }
        is.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

but i am getting error like below...

          01-11 16:00:48.709: E/dalvikvm-heap(18535): Out of memory on a 176-byte allocation.
          01-11 16:00:48.709: I/dalvikvm(18535): "main" prio=5 tid=1 RUNNABLE
          01-11 16:00:48.709: I/dalvikvm(18535):   | group="main" sCount=0 dsCount=0 obj=0x40022190 self=0xd018
          01-11 16:00:48.709: I/dalvikvm(18535):   | sysTid=18535 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1345002400
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.Throwable.nativeFillInStackTrace(Native Method)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.Throwable.fillInStackTrace(Throwable.java:135)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.Throwable.<init>(Throwable.java:74)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.Error.<init>(Error.java:38)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.VirtualMachineError.<init>(VirtualMachineError.java:36)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.OutOfMemoryError.<init>(OutOfMemoryError.java:34)
          01-11 16:00:48.709: I/dalvikvm(18535):   at java.util.ArrayList.add(ArrayList.java:-1)
          01-11 16:00:48.709: I/dalvikvm(18535):   at org.apache.harmony.xml.dom.ElementImpl.setAttributeNode(ElementImpl.java:329)
          01-11 16:00:48.709: I/dalvikvm(18535):   at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:355)
          01-11 16:00:48.709: I/dalvikvm(18535):   at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:360)
          01-11 16:00:48.709: I/dalvikvm(18535):   at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:133)
          01-11 16:00:48.709: I/dalvikvm(18535):   at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:132)
          01-11 16:00:48.709: I/dalvikvm(18535):   at com.example.simplexmlpullapp.MainActivity.onCreate(MainActivity.java:32)
          01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
          01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
          01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
          01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.ActivityThread.access$1500(ActivityThread.java:123)
         01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
         01-11 16:00:48.709: I/dalvikvm(18535):   at android.os.Handler.dispatchMessage(Handler.java:99)
         01-11 16:00:48.709: I/dalvikvm(18535):   at android.os.Looper.loop(Looper.java:130)
         01-11 16:00:48.709: I/dalvikvm(18535):   at android.app.ActivityThread.main(ActivityThread.java:3835)
         01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.reflect.Method.invokeNative(Native Method)
         01-11 16:00:48.709: I/dalvikvm(18535):   at java.lang.reflect.Method.invoke(Method.java:507)
         01-11 16:00:48.709: I/dalvikvm(18535):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
         01-11 16:00:48.709: I/dalvikvm(18535):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
         01-11 16:00:48.709: I/dalvikvm(18535):   at dalvik.system.NativeStart.main(Native Method)
         01-11 16:00:48.729: W/dalvikvm(18535): Exception thrown (Ljava/lang/OutOfMemoryError;) while throwing internal exception (Ljava/lang/OutOfMemoryError;)
        01-11 16:00:48.729: W/dalvikvm(18535): Could not resolve class ref'ed in exception catch list (class index 349, exception Ljava/lang/OutOfMemoryError;)
        01-11 16:00:49.209: I/dalvikvm-heap(18535): Clamp target GC heap from 25.906MB to 24.000MB
        01-11 16:00:49.209: D/dalvikvm(18535): GC_CONCURRENT freed <1K, 11% free 22006K/24519K, external 3K/514K, paused 5ms+6ms
        01-11 16:00:49.509: D/dalvikvm(18535): GC_FOR_MALLOC freed 19308K, 89% free 2698K/24519K, external 3K/514K, paused 294ms
        01-11 16:00:49.509: D/AndroidRuntime(18535): Shutting down VM
        01-11 16:00:49.509: W/dalvikvm(18535): threadid=1: thread exiting with uncaught exception (group=0x40018560)
        01-11 16:00:49.509: E/AndroidRuntime(18535): FATAL EXCEPTION: main
        01-11 16:00:49.509: E/AndroidRuntime(18535): java.lang.OutOfMemoryError: [memory exhausted]
        01-11 16:00:49.509: E/AndroidRuntime(18535):    at dalvik.system.NativeStart.main(Native Method)
5
  • How long is your xml file? Commented Jan 11, 2013 at 10:37
  • 4 mb size around 65000 lines Commented Jan 11, 2013 at 10:38
  • 2
    Try to use a SAX parser instead of the DOM. Commented Jan 11, 2013 at 10:40
  • @SiddhpuraAmit I'd say it's brave to process 4MB of XML on a mobile device. XML processing requires some resources; that's also valid for parsing... have you tried how many lines you can successfully parse? Commented Jan 11, 2013 at 10:42
  • I have same query first of all i have try SAX parser but it will break operation bcz of not good format xml on some link it was valid on some link it shows an error that's y i have used DOM parser and it works fine but sometimes on emulator it throw this error of same kind Commented Jan 11, 2013 at 11:39

2 Answers 2

2

SAX XML Parser in Java

SAX Stands for Simple API for XML Parsing. This is an event based XML Parsing and it parse XML file step by step so much suitable for large XML Files. SAX XML Parser fires event when it encountered opening tag, element or attribute and the parsing works accordingly. It’s recommended to use SAX XML parser for parsing large xml files in Java because it doesn't require to load whole XML file in Java and it can read a big XML file in small parts. Java provides support for SAX parser and you can parse any xml file in Java using SAX Parser, I have covered example of reading xml file using SAX Parser here. One disadvantage of using SAX Parser in java is that reading XML file in Java using SAX Parser requires more code in comparison of DOM Parser.

Difference between DOM and SAX XML Parser Here are few high level differences between DOM parser and SAX Parser in Java:

1) DOM parser loads whole xml document in memory while SAX only loads small part of XML file in memory.

2) DOM parser is faster than SAX because it access whole XML document in memory.

3) SAX parser in Java is better suitable for large XML file than DOM Parser because it doesn't require much memory.

4) DOM parser works on Document Object Model while SAX is an event based xml parser.

That’s all on difference between SAX and DOM parsers in Java, now it’s up to you on which XML parser you going to choose. I recommend use DOM parser over SAX parser if XML file is small enough and go with SAX parser if you don’t know size of xml files to be processed or they are large.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent recommendation! I've never seen such a great XML parser for Android!!! It works completely as expected!
1

Based on this answer, I have used XmlPullParser in my project. Its easier to use then SAX and DOM. You can find more about Android Developers Site and Tutorial at Vogella.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.