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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:44:35+00:00 2026-05-20T08:44:35+00:00

i am newbie to android. i have searched code and done xml parsing but

  • 0

i am newbie to android.
i have searched code and done xml parsing but its in listview…
i need that it should be in tableview
please help me out…
my code is as follows:

class MyXMLHandler

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {

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

public static SitesList getSitesList() {
    return sitesList;
}

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

/** Called when tag starts ( ex:- <name>AndroidPeople</name>
 * -- <name> )*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

    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);
    }

    }

    /** Called when tag closing ( ex:- <name>AndroidPeople</name>
 * -- </name> )*/
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

    currentElement = false;

    /** set value */
        if (localName.equalsIgnoreCase("name"))
            sitesList.setName(currentValue);
        else if (localName.equalsIgnoreCase("website"))
            sitesList.setWebsite(currentValue);

    }

/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
     * -- to get AndroidPeople Character ) */
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }

    }

}

Second class SitesList

import java.util.ArrayList;

/** Contains getter and setter method for variables  */
public class SitesList {

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

    /** In Setter method default it will return arraylist
     *  change that to add  */

    public ArrayList<String> getName() {
        return name;
    }

    public void setName(String name) {
        this.name.add(name);
    }

    public ArrayList<String> getWebsite() {
        return website;
    }

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

  /**public ArrayList<String> getCategory() {
        return category;
   }

    public void setCategory(String category) {
        this.category.add(category);
   }**/

}

and third class is MyParsingExample

import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class XMLParsingExample extends Activity {

/** 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);

    /** 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 */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
        URL sourceUrl = new URL("http://ipaddress/test/file.xml");

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

    /** Get result from MyXMLHandler SitlesList Object */
    sitesList = MyXMLHandler.sitesList;

    /** Assign textview array lenght by arraylist size */
    name = new TextView[sitesList.getName().size()];
    website = new TextView[sitesList.getName().size()];
   //category = new TextView[sitesList.getName().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);

}

and xml file is

<maintag>
<!-- Table item -->
<item>
  <name>sam</name>
  <website>sam</website>
</item>
  • 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-20T08:44:36+00:00Added an answer on May 20, 2026 at 8:44 am

    Okay Uff Chaaanleeenge Accepted

    I think i can guess what you want to do. First of all, basicly your code does the following:

    • Parsing your XML File and putting the results into SitesList
    • Creating all the layout stuff programaticly
    • For every Item which has been parsed it generates a new TextView

    So there is no ListView in there.

    You now want to put the parsed values into a TableView. So just let me say, that how you build up your code for generating the layout isn’t the best to keep your Presentation Stuff away from your code. Android is very well designed for that purpose. You should have a closer look to http://developer.android.com/guide/topics/resources/index.html especcially to the layout resources. You can do your layout stuff in xml files.

    But back to your current problem. To Show your stuff in a table you have to edit your MyParsingExample activity.

    (I can’t test the code at the moment but it should work as follows:)

    /** Create a new layout to display the view */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);
    

    At this point you should use a TableLayout like this for example:

    /** Create a new layout to display the view */
    TableLayout layout = new TableLayout(this);
    

    So the tableLayout is just another way to layout other components.

    Now you need to have a look at this part:

    /** Assign textview array lenght by arraylist size */
    name = new TextView[sitesList.getName().size()];
    website = new TextView[sitesList.getName().size()];
    //category = new TextView[sitesList.getName().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]);
    }
    

    Thats the point where the Textviews has been generated. You need to substitude it by some code which generates Rows into the the tableview. As i said i cannot test the code at the moment. But i am reffering to:

    http://developer.android.com/reference/android/widget/TableLayout.html
    and
    http://developer.android.com/reference/android/widget/TableRow.html

    For example like this:

    /** Assign textview array lenght by arraylist size */
    name = new TextView[sitesList.getName().size()];
    website = new TextView[sitesList.getName().size()];
    //category = new TextView[sitesList.getName().size()];
    
    /** Add rows for every entry and add it to layout */
    for (int i = 0; i < sitesList.getName().size(); i++) {
        //we can leave that part as it is
        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));
    
        //now we create a new row
        TableRow row = new TableRow(this);
    
        //we add the textviews to the row
        row.addView(name[i]);
        row.addView(website[i]);
    
        //and we add the row to the tablelayout
        layout.addView(row);
    }
    

    I really hope that helps you to go on with that. I in general recommend to have a look at http://developer.android.com/guide/topics/resources/available-resources.html this is the best page to start learning android programming.

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

Sidebar

Related Questions

Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
Java Newbie here. I have a JFrame that I added to my netbeans project,
I am a newbie android developer and so far have only got Hello World
I am newbie to android. I have client server based application. Server keeps on
[Android Newbie alert] I need to capture the contents of a WebView in a
.NET newbie here... I'd like to make a button in a Windows form that
Total newbie question but this is driving me mad! I'm trying this: myInt =
I'm a newbie to pgsql. I have few questionss on it: 1) I know
I am a newbie to Android and the Eclipse development environment and would like
As a newbie to Android development, I'm trying to do the basics and build

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.