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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:58:36+00:00 2026-05-31T11:58:36+00:00

I retrieve XML from a server, save it into an SD card, then parse

  • 0

I retrieve XML from a server, save it into an SD card, then parse that XML. I get this exception:

03-19 13:53:26.943: E/AndroidRuntime(12512): FATAL EXCEPTION: main
03-19 13:53:26.943: E/AndroidRuntime(12512): java.lang.NullPointerException.

I am using this code:

/** Create Object For SiteList Class */
SitesList sitesList = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    DownloadFromUrl("http://www.androidpeople.com/wp- content/uploads/2010/06/example.xml","example.xml");
    /** Create a new layout to display the view */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);
    /** Create a new textview array to display the results */
    TextView name[];
    TextView website[];
    TextView category[];
    try {
        /** Handling XML */
        String path = Environment.getExternalStorageDirectory() +"../xmls"+"/example.xml";
        File file = new File(path);
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        DataHandler myXMLHandler = new DataHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(new InputStreamReader(new    FileInputStream(file))));//parse(new InputSource(sourceUrl.openStream()));
    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }
    /** Get result from DataHandler SitlesList Object */
    sitesList = DataHandler.sitesList;
    /** Assign textview array lenght by arraylist size */
    name = new TextView[sitesList.getName().size()];
    website = new TextView[sitesList.getWebsite().size()];
    category = new TextView[sitesList.getCategory().size()];
    /** Set the result text in textview and add it to layout */
    for (int i = 0; i < sitesList.getName().size(); i++) {
        name[i] = new TextView(this);
        name[i].setText("Name = "+sitesList.getName().get(i));
        website[i] = new TextView(this);
        website[i].setText("Website = "+sitesList.getWebsite().get(i));
        category[i] = new TextView(this);
        category[i].setText("Website Category = "+sitesList.getCategory().get(i));
        layout.addView(name[i]);
        layout.addView(website[i]);
        layout.addView(category[i]);
    }
    /** Set the layout view to display */
    setContentView(layout);
}

private void DownloadFromUrl(String DownloadUrl, String fileName)
{
    URL url;
    File root = android.os.Environment.getExternalStorageDirectory();
    File dir = new File (root.getAbsolutePath() + "/xmls");
    if(dir.exists()==false) {
        dir.mkdirs();
    }
    try {
        url = new URL(DownloadUrl);
        File file = new File(dir, fileName);
        Log.d("DownloadManager", "download begining");
        Log.d("DownloadManager", "download url:" + url);
        Log.d("DownloadManager", "downloaded file name:" + fileName);
        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();
        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }
        /* Convert the Bytes read to a String. */
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        //Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } //you can write here any link
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

And my handler class is :

Boolean currentElement = false;
Context theContext;
String currentValue = null;
public static SitesList sitesList = null;

public static SitesList getSitesList()
{
    return sitesList;
}

public static void setSitesList(SitesList sitesList)
{
    DataHandler.sitesList = sitesList;
}

@Override
public void startElement(String uri, String localName, String qName,
                         Attributes attributes) throws SAXException
{
    super.startElement(uri, localName, qName, attributes);
    currentElement = true;
    if (localName.equals("maintag"))
    {
        /** Start */
        sitesList = new SitesList();
    }
    else if (localName.equals("website"))
    {
        /** Get attribute value */
        String attr = attributes.getValue("category");
        sitesList.setCategory(attr);
    }
}

@Override
public void endElement(String uri, String localName, String qName)
    throws SAXException {
    super.endElement(uri, localName, qName);
    currentElement = false;
    /** set value */
    if (localName.equalsIgnoreCase("name"))
        sitesList.setName(currentValue);
    else if (localName.equalsIgnoreCase("website"))
        sitesList.setWebsite(currentValue);
}

@Override
public void characters(char[] ch, int start, int length)
    throws SAXException {
// TODO Auto-generated method stub
    super.characters(ch, start, length);
    if (currentElement)
    {
        currentValue = new String(ch, start, length);
        currentElement = false;
    }
}

And my list class is:

/** Variables */
private ArrayList name = new ArrayList();
private ArrayList website = new ArrayList();
private ArrayList category = new ArrayList();

/** In Setter method default it will return arraylist
 * change that to add */
public ArrayList getName() {
    return name;
}
public void setName(String name) {
    this.name.add(name);
}

public ArrayList getWebsite() {
    return website;
}
public void setWebsite(String website) {
    this.website.add(website);
}

public ArrayList getCategory() {
    return category;
}
public void setCategory(String category) {
    this.category.add(category);
}

The XML file is downloaded and saved into the SD card properly but not parsed.

  • 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-05-31T11:58:37+00:00Added an answer on May 31, 2026 at 11:58 am

    SAX (Simple API for XML) is an event-based sequential access parser API developed by the XML-DEV mailing list for XML documents. SAX provides a mechanism for reading data from an XML document that is an alternative to that provided by the Document Object Model (DOM). Where the DOM operates on the document as a whole, SAX parsers operate on each piece of the XML document sequentially

    Please refer this link for your solution.

    It will help you.

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

Sidebar

Related Questions

Hi I get problem retrieve XML data from SQL2000 server. The table structure is
I need to retrieve information from a SQL server and insert into a XML
I'm using an XMLHttpRequest to retrieve XML from the server, and I'd like to
I want to retrieve a Map from a using JAX-RS (text/xml) @GET public Map<String,String>
I want to retrieve the XML data from SQL Server database and bound it
i retrieve data from upc database and then want to insert the data into
I'm trying to parse the XML HttpResponse i get from a HttpPost to a
I'm trying to use e4x to retrieve xml from the result event thrown when
I'm trying to retrieve an XML stream from a URL. For most URLs my
From the XML below I'm trying to retrieve the second element in the sequence.

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.