Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:12:36+00:00 2026-06-13T00:12:36+00:00

I tried to parse my xml file from assets folder, I tried it with

  • 0

I tried to parse my xml file from assets folder, I tried it with android 2.3.5 it works but when I tried it in android 4.0.4 and 4.1 it it doesn’t give any error but I couldn’t get values from xml.

    public class PlistParser {

    // parse Plist and fill in arraylist
    public ArrayList<DataModel> parsePlist(String xml) {
        final ArrayList<DataModel> dataModels = new ArrayList<DataModel>();

        //Get the xml string from assets
        final Document doc =  XMLfromString(xml);
        final NodeList nodes_array = doc.getElementsByTagName("dict");

        //Fill in the list items from the XML document          
        for ( int index = 0; index < nodes_array.getLength(); index++ ) {

            final Node node = nodes_array.item(index);

            if ( node.getNodeType() == Node.ELEMENT_NODE ) {
                final Element e = (Element)nodes_array.item(index);

                final NodeList nodeKey = e.getElementsByTagName("key");
                final NodeList nodeValue = e.getElementsByTagName("string");
                DataModel model = new DataModel();

                for (int i=0; i < nodeValue.getLength(); i++) {

                    final Element eleKey = (Element)nodeKey.item(i);
                    final Element eleString = (Element)nodeValue.item(i);


                    if ( eleString != null ) {

                        String strValue = getValue(eleString, "string");

                        if(getValue(eleKey, "key").equals("isim")) {
                            model = new DataModel();
                            model.setIsim(strValue);
                        } else if(getValue(eleKey, "key").equals("turu")) {
                            model.setTuru(strValue);
                        } else if(getValue(eleKey, "key").equals("kalori")) {
                            model.setKalori(Float.parseFloat(strValue));
                        } else if(getValue(eleKey, "key").equals("protein")) {
                            model.setProtein(Float.parseFloat(strValue));
                        } else if(getValue(eleKey, "key").equals("yag")) {
                            model.setYag(Float.parseFloat(strValue));
                        } 
                        else if(getValue(eleKey, "key").equals("karbonhidrat")) {
                            model.setKarbonhidrat(Float.parseFloat(strValue));
                        } else if(getValue(eleKey, "key").equals("lif")) {
                            model.setLif(Float.parseFloat(strValue));
                        }else if(getValue(eleKey, "key").equals("aciklama")) {
                            if ( strValue == null ) {
                                strValue = "";
                            }
                            model.setAciklama(strValue);
                            dataModels.add(model);
                        }
                    }
                }
            }
        }

        return dataModels;
    }

    // Create xml document object from XML String
    private  Document XMLfromString(String xml) {
        Document doc = null;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);
            doc.getDocumentElement().normalize();
        } catch (ParserConfigurationException e) {
            System.out.println("XML parse error: " + e.getMessage());
            return null;
        } catch (SAXException e) {
            System.out.println("Wrong XML file structure: " + e.getMessage());
            return null;
        } catch (IOException e) {
            System.out.println("I/O exeption: " + e.getMessage());
            return null;
        }

        return doc;
    }

    // fetch value from Text Node only
    private   String getElementValue(Node elem) {
        Node kid;
        if (elem != null) {
            if (elem.hasChildNodes()) {
                for (kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()) {
                    if (kid.getNodeType() == Node.TEXT_NODE) {
                        return kid.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

    /// Fetch value from XML Node
    private   String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return getElementValue(n.item(0));
    }
}    

Here’s the code that takes xml from assets.

private  String readPlistFromAssets() {
    StringBuffer sb = new StringBuffer();
    BufferedReader br=null;

    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open("sample.xml")));         
        String temp;
        while ((temp = br.readLine()) != null)
        sb.append(temp);
    } catch (IOException e) {       
        e.printStackTrace();
    } finally {
        try {
            br.close(); // stop reading
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return sb.toString();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:12:37+00:00Added an answer on June 13, 2026 at 12:12 am

    I figured out I need to do it with asynctask.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse an XML file from URL using JDOM. But when trying
I tried but i was unable to parse this json object which comes from
I try to parse articles from wikipedia. I use the *page-articles.xml file, where they
I am trying to read a xml file from the web and parse it
I'm using a webservice to get a certain xml file from it. It works
I want to parse a XML file from a server on internet and save
i'm trying to parse this xml file but i can't find a way to
I am reading IPaddress from xml file and I put it into IPaddress.parse() and
I'm trying to parse an xml file using LINQ, but as I understand the
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.