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 8628127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:27:29+00:00 2026-06-12T08:27:29+00:00

I have xml file under ASSETS dir (com.android.project\assets\xml\file.xml). I want to call a (following)

  • 0
  • I have xml file under ASSETS dir (com.android.project\assets\xml\file.xml).
  • I want to call a (following) function to read the xml file and return the contents as String.
  • The function requires the path of the file as string. I don’t know how to give the path of the file as string.

    private String getXML(String path){
    
      String xmlString = null;
    
      AssetManager am = this.getAssets();
      try {
        InputStream is = am.open(path);
        int length = is.available();
        byte[] data = new byte[length];
        is.read(data);
        xmlString = new String(data);
      } catch (IOException e1) {
          e1.printStackTrace();
      }
    
      return xmlString;
    }
    

The file.xml:

    <Items>
        <ItemData>
            <ItemNumber>ABC</ItemNumber>
            <Description>Desc1</Description>        
            <Price>9.95</Price>        
            <Weight>10.00</Weight>    
        </ItemData>    
        <ItemData>        
            <ItemNumber>XYZ</ItemNumber>        
            <Description>"Desc2"</Description>        
            <Price>19.95</Price>
            <Weight>22.22</Weight>
        </ItemData>
    </Items>

QUESTIONS:

  • How can I call the function getXML(String path) with a path as string parameter, if my file is located under \assets\xml\file.xml ?

  • Finally, Is there any better way of reading XML file as a String?

Thank you!

  • 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-12T08:27:30+00:00Added an answer on June 12, 2026 at 8:27 am

    the path is just the path under the assets directory using forward slash (/)

    so assets/x/y.z is referenced as this.getAssets().open(“x/y.z”);

    That isnt the correct way to read the data – Inputstream.read isnt garunteed to read all the data it returns the number of bytes read – likely this will work for smaller file but you might get problems with bigger ones.

    This is general code i read to to read text files, instead of a FileReader use an InputStreamReader

    StringBuilder sw = new StringBuilder();
    BufferedReader reader = new BufferedReader( new FileReader(file));
    String readline = "";
    while ((readline = reader.readLine()) != null) { 
        sw.append(readline);
    }
    String string = sw.toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML file : <?xml version=1.0?> <Table69> </Table69> And want to
I have the following selector defined in an XML file under res/color/redeemlist_item_color.xml : <?xml
I have the following config for persistence.xml file: <?xml version=1.0 encoding=UTF-8?> <persistence xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
I have an xml file with the following structure: <main_tag> <first> <tag1>val1</tag1> <conf> <tag2>val2</tag2>
I have an XML file that I'm trying to read from here , and
I have xml file nameed Hello as under with starting node as under: /
i have an xml file under WEB-INF called pretty-config.xml , it's for prettyFaces rewrite
I have an xml file as under <!--Sample Person Data--> <Person> <PersonDetail PersonId=1> <Name>Priyanka
I have a need to have a config.xml file in my Eclipse project, and
Say I have an XML file like the following: <holidays> <holiday1 val=New Years, 01/01,

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.