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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:35:58+00:00 2026-06-13T01:35:58+00:00

I’m parsing XML from URL. What changes has been made to parse same XML

  • 0

I’m parsing XML from URL. What changes has been made to parse same XML file from raw folder. Have any idea to how to reduce code ?

This my xml file :umesh.xml

<?xml version="1.0" encoding="utf-8"?>
<appdata>
<brand name="Lovely Products">
<product>Hat</product>
<product>Gloves</product>
</brand>
<brand name="Great Things">
<product>Table</product>
<product>Chair</product>
<product>Bed</product>
</brand>
</appdata> 

Below is my java file :

  1. DataHandler.java

    package com.umesh.xmlparsing;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;
    
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.DefaultHandler;
    
    import android.content.Context;
    import android.content.res.XmlResourceParser;
    import android.graphics.Color;
    import android.util.Log;
    import android.widget.TextView;
    
    
    
    public class DataHandler extends DefaultHandler{
    
    //list for imported product data
    private ArrayList<TextView> theViews;
    //string to track each entry
    private String currBrand = "";
    //flag to keep track of XML processing
    private boolean isProduct = false;
    //context for user interface
    private Context theContext;
    //constructor
    public DataHandler(Context cont) {
        super();
        theViews = new ArrayList<TextView>();
        theContext = cont;
    }
    
    
    //start of the XML document
    public void startDocument () { Log.i("DataHandler", "Start of XML document"); }
    
    //end of the XML document
    public void endDocument () { Log.i("DataHandler", "End of XML document"); }
    
    //opening element tag
    public void startElement (String uri, String name, String qName, Attributes atts)
    {
        //handle the start of an element
    
        //find out if the element is a brand
        if(qName.equals("brand"))
        {
            //set product tag to false
            isProduct = false;
            //create View item for brand display
            TextView brandView = new TextView(theContext);
            brandView.setTextColor(Color.rgb(73, 136, 83));
            //add the attribute value to the displayed text
            String viewText = "Items from " + atts.getValue("name") + ":";
            brandView.setText(viewText);
            //add the new view to the list
            theViews.add(brandView);
        }
        //the element is a product
        else if(qName.equals("product"))
            isProduct = true;
    }
    
    //closing element tag
    public void endElement (String uri, String name, String qName)
    {
        //handle the end of an element
        if(qName.equals("brand"))
        {
            //create a View item for the products
            TextView productView = new TextView(theContext);
            productView.setTextColor(Color.rgb(192, 199, 95));
            //display the compiled items
            productView.setText(currBrand);
            //add to the list
            theViews.add(productView);
            //reset the variable for future items
            currBrand = "";
        }
    }
    
    //element content
    public void characters (char ch[], int start, int length)
    {
        //process the element content
        //string to store the character content
        String currText = "";
        //loop through the character array
        for (int i=start; i<start+length; i++)
        {
            switch (ch[i]) {
            case '\\':
                break;
            case '"':
                break;
            case '\n':
                break;
            case '\r':
                break;
            case '\t':
                break;
            default:
                currText += ch[i];
                break;
            }
        }
        //prepare for the next item
        if(isProduct && currText.length()>0)
            currBrand += currText+"\n";
    }
    
    public ArrayList<TextView> getData()
    {
        //take care of SAX, input and parsing errors
        try
        {
                //set the parsing driver
            System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");
                //create a parser
            SAXParserFactory parseFactory = SAXParserFactory.newInstance();
            SAXParser xmlParser = parseFactory.newSAXParser();
                //get an XML reader
            XMLReader xmlIn = xmlParser.getXMLReader();
                //instruct the app to use this object as the handler
            xmlIn.setContentHandler(this);
                //provide the name and location of the XML file **ALTER THIS FOR YOUR FILE**
            URL xmlURL = new URL("http://mydomain.com/umesh.xml");
    
    
                //open the connection and get an input stream
            URLConnection xmlConn = xmlURL.openConnection();
            InputStreamReader xmlStream = new InputStreamReader(xmlConn.getInputStream());
    
                //build a buffered reader
            BufferedReader xmlBuff = new BufferedReader(xmlStream);
    
            //   uuu   XmlResourceParser todolistXml = getResources().getXml(R.raw.c4mh_clinics);
                //parse the data
            xmlIn.parse(new InputSource(xmlBuff));
        }
        catch(SAXException se) { Log.e("AndroidTestsActivity", 
                "SAX Error " + se.getMessage()); }
        catch(IOException ie) { Log.e("AndroidTestsActivity", 
                "Input Error " + ie.getMessage()); }
        catch(Exception oe) { Log.e("AndroidTestsActivity", 
                "Unspecified Error " + oe.getMessage()); }
            //return the parsed product list
        return theViews;
    }
    
    }
    
  2. XMLParsing.java

    package com.umesh.xmlparsing;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class XMLParsing extends Activity {
    
    TextView tv;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        //get a reference to the layout
        LayoutInflater inflater = getLayoutInflater();
        LinearLayout mainLayout = (LinearLayout) inflater.inflate(R.layout.main,null);
        try
        {
                //create an instance of the DefaultHandler class 
                //**ALTER THIS FOR YOUR CLASS NAME**
            DataHandler handler = new DataHandler(getApplicationContext());
                //get the string list by calling the public method
            ArrayList<TextView> newViews = handler.getData();
                //convert to an array
            Object[] products = newViews.toArray();
                //loop through the items, creating a View item for each
            for(int i=0; i<products.length; i++)
            {
                //add the next View in the list
                mainLayout.addView((TextView)products[i]);
            }
        }
        catch(Exception pce) { Log.e("AndroidTestsActivity", "PCE "+pce.getMessage()); }
    
        setContentView(mainLayout);
    }
    
    
    }
    
  • 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-13T01:35:59+00:00Added an answer on June 13, 2026 at 1:35 am

    Please See below link of my answer, it will solve your problem.

    Local XML Parsing

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

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have an array which has BIG numbers and small numbers in it. I
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from

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.