Android read from XML optimization Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android read from XML optimization without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m using the following code to read data from an XML file stored in my assets folder. It’s a simple XML file with the structure as below. The file has about 10 nodes (about 100 rows in total). But on my Galaxy S2 this piece of code takes 10 seconds to execute. Is ther a way to do this faster? THank you! XML Structure:

    <?xml version="1.0" encoding="utf-8"?>
<root>
    <node>
        <name>test</name>
        <picture>test1</picture>
        <thumbnail>asa</thumbnail>
        <location>fdsf</location>
        <description>sdfsd</description>
    </node>
    <node>
        <name>test</name>
        <picture>test1</picture>
        <thumbnail>asa</thumbnail>
        <location>fdsf</location>
        <description>sdfsd</description>
    </node>
    <node>
        <name>test</name>
        <picture>test1</picture>
        <thumbnail>asa</thumbnail>
        <location>fdsf</location>
        <description>sdfsd</description>
    </node>

    .......

</root>

And the code snippet :

String XMLText = "";    
InputStream raw = getApplicationContext().getAssets().open(fileName);
            InputStreamReader inputStreamReader = new InputStreamReader(raw);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


            String line = ""; 
            while ((line = bufferedReader.readLine()) != null) {
                XMLText += line;
            }

Answer

You should have to use SAX parser or XmlPullParser. In case if you want to concat/append data to the string then use StringBuilder instead of String type.

We are here to answer your question about Android read from XML optimization - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji