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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:57:29+00:00 2026-05-27T04:57:29+00:00

I’m working on a small app that uses and xml file to print an

  • 0

I’m working on a small app that uses and xml file to print an ArrayList of chapters which in turn points to a specific html file.

I used this tutorial to get me started: http://www.anddev.org/novice-tutorials-f8/parsing-xml-from-the-net-using-the-saxparser-t353.html

My xml file looks something like this:

<chapters>
    <chapter title="Förutsättningar">
        <page title="Sida 3" url="sida_3.html" />
        <page title="Sida 4" url="sida_4.html" />
    </chapter>
</chapters>

Using the tutorial above I’ve managed to output each chapter node into an ArrayList with a onListItemClick function on each item. So far so good.

The problem I’m having is that I can’t figure out how to get a specific child node and load the html file when I click an item. I’m pretty new to Android.

Any ideas? I would really appreciate ANY help on the subject.

Here’s my source:

ParsingXML.java

public class ParsingXML extends ListActivity {
private final String MY_DEBUG_TAG = "XmlParser";
public String lang = null;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setTitle("Lastsäkring");

    Bundle bundle = this.getIntent().getExtras();
    lang = bundle.getString("lang");

    Log.i("ParsingXML", "Chosen language: " + this.lang + ", Type: " + this.lang.getClass().getName());

    TextView tv = new TextView(this);

    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();

        XMLReader xr = sp.getXMLReader();

        ExampleHandler myExampleHandler = new ExampleHandler();
        xr.setContentHandler(myExampleHandler);

        /*
         * If XML-file is located online (needs internet permissions in the manifest):
         * URL url = new URL("http://dev.houdini.se/android/demo.xml");
         * xr.parse(new InputSource(url.openStream()));
         */

        if(this.lang.equals("en"))
            xr.parse(new InputSource(this.getResources().openRawResource(R.raw.en_content)));
        else
            xr.parse(new InputSource(this.getResources().openRawResource(R.raw.sv_content)));

        ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();                       
        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, parsedExampleDataSet.toArrayList()));

    } catch(Exception e) {
        tv.setText("Error: " + e.getMessage());
        Log.e(MY_DEBUG_TAG, "XmlParseError", e);
        this.setContentView(tv);
    }
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Context context = getApplicationContext();
    int duration = Toast.LENGTH_SHORT;

    CharSequence text = "Clicked position: " + position + ", id: " + id;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();


    /*switch( position )
    {
       case 0:                                  
                Bundle bundle = new Bundle();
                bundle.putString("WindowTitle", "TESTA");
                Intent intent = new Intent(this, TextPage.class);
                intent.putExtras(bundle);
                startActivity(intent);
                break;                  
       case 1:  
            Intent video = new Intent(this, Video.class);
            startActivity(video);
           break;

       case 2:  
            Intent swipe = new Intent(this, Swipe.class);
            startActivity(swipe);
          break;
    }*/
}

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.settings:
            Intent prefsActivity = new Intent(getBaseContext(), Preferences.class);
            startActivity(prefsActivity);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

ExampleHandler.java

public class ExampleHandler extends DefaultHandler {
private boolean in_chapters = false;
private boolean in_chapter = false;
private boolean in_page = false;

private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();

public ParsedExampleDataSet getParsedData() {
    return this.myParsedExampleDataSet;
}

@Override
public void startDocument() throws SAXException {
    this.myParsedExampleDataSet = new ParsedExampleDataSet();
}

@Override
public void endDocument() throws SAXException {
    // Nothing to do
}

@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if(localName.equals("chapters")) {
        this.in_chapters = true;
    } else if(localName.equals("chapter")) {
        this.in_chapter = true;
        String attrValue = atts.getValue("title");
        myParsedExampleDataSet.setExtractedString(attrValue);
    } else if(localName.equals("page")) {
        this.in_page = true;
    }
}

@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    if(localName.equals("chapters")) {
        this.in_chapters = false;
    } else if(localName.equals("chapter")) {
        this.in_chapter = false;
    } else if(localName.equals("page")) {
        this.in_page = false;
    }
}

@Override
public void characters(char ch[], int start, int length) {
    if(this.in_page == true) {
        myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
    }
}

}

ParsedExampleDataSet.java

public class ParsedExampleDataSet {
private String extractedString = "";
private ArrayList<String> myArr = new ArrayList<String>();
private int extractedInt = 0;

public ArrayList<String> getExtractedString() {
    //return extractedString; Function Type = String
    return myArr;
}

public void setExtractedString(String extractedString) {
    //this.extractedString += extractedString + "\n";
    myArr.add(extractedString);
}

public int getExtractedInt() {
    return extractedInt;
}

public void setExtractedInt(int extractedInt) {
    this.extractedInt = extractedInt;
}

public String toString() {
    return "NODER\n" + this.extractedString;
}

public ArrayList<String> toArrayList() {
    return this.myArr;
}
}
  • 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-27T04:57:30+00:00Added an answer on May 27, 2026 at 4:57 am

    First create proper data structure:

    public class PageNode {
        public String title;
        public String url;
        /* Getters/setter/constructor etc. if you feel like*/
        public String toString() {
           return title;
        }
    }
    
    public class ChapterNode {
        public String title;
        public ArrayList<PageNode> pages = new ArrayList<PageNode>();
        /* Getters/setter/constructor etc. if you feel like*/
    }
    

    And parse the xml accordingly. Example:

    ArrayList<ChapterNode> chapters = new ArrayList<ChapterNode>();
    ChapterNode chapterNode = null;
    
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        if(localName.equals("chapters")) {
        } else if(localName.equals("chapter")) {
            chapterNode = new ChapterNode();
            chapterNode.title = atts.getValue("title");
        } else if(localName.equals("page")) {
            PageNode pageNode = new PageNode();
            pageNode.title = atts.getValue("title");
            pageNode.url = atts.getValue("url");
            chapterNode.pages.add(pageNode);
        }
    }
    
    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        if(localName.equals("chapters")) {
        } else if(localName.equals("chapter")) {
            chapters.add(chapterNode);
            chapterNode = null;
        } else if(localName.equals("page")) {
        }
    }
    

    Then you can access the pageNode like this:

    PageNode pageNode = chapterNode.pages.get(position);
    

    And set adapter like this:

    this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, chapterNode.pages));
    
    • 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 working with an upstream system that sometimes sends me text destined for HTML/XML
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

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.