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

  • Home
  • SEARCH
  • 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 9154841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:29:38+00:00 2026-06-17T12:29:38+00:00

Possible Duplicate: how to create an xml api that will give data to my

  • 0

Possible Duplicate:
how to create an xml api that will give data to my android app

I have asked this previously too but this time I have done some research and came back as I didn’t get any answer for my problem.

I have a data.xml file which has data that I want to give to my android app. I have done the android-end coding and I think its flawless. But the data is not being retrieved from the file into my app. My file structure goes this way:

(a)data.xml
(b)Main
(c)HandlingXMLStuff
(d)XMLDataCollected

(a)data.xml

<?xml version="1.0" encoding="utf-8"?>
  <document version="first">
    <stuff code="firststuff">
      <item1 build="first">This is first item.</item1>
      <item2 build="second">This is second item.</item2>
      <item3 build="third">This is third item.</item3>
    </stuff>
    <stuff code="secondtstuff">
      <item1 build="first">This is first item.</item1>
      <item2 build="second">This is second item.</item2>
      <item3 build="third">This is third item.</item3>
    </stuff>
    <stuff code="thirdstuff">
      <item1 build="first">This is first item.</item1>
      <item2 build="second">This is second item.</item2>
      <item3 build="third">This is third item.</item3>
    </stuff>
  </document>

(b)Main

package com.xyz.xmlpar1;
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.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener {
  static final String BaseURL="http://www.xyz.com/data.xml?code=";
  Button b;
  TextView tv;
  EditText et1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    et1=(EditText) findViewById(R.id.editText1);
    b=(Button) findViewById(R.id.button1);
    tv=(TextView) findViewById(R.id.textView1);
    b.setOnClickListener(this);
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    String item=et1.getText().toString();
    StringBuilder URL=new StringBuilder(BaseURL);
    URL.append(item);
    String fullURL=URL.toString();
    try{
      URL website=new URL(fullURL);
      SAXParserFactory spf=SAXParserFactory.newInstance();
      SAXParser sp=spf.newSAXParser();
      XMLReader xr=sp.getXMLReader();
      HandlingXMLStuff doingWork=new HandlingXMLStuff();
      xr.setContentHandler(doingWork);
      xr.parse(new InputSource(website.openStream()));
      String information=doingWork.getInformation();
      tv.setText(information);
    }catch(Exception e){
      tv.setText("error");
    }
  }
}

(c)HandlingXMLStuff

package com.xyz.xmlpar1;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class HandlingXMLStuff extends DefaultHandler {
  XMLDataCollected info=new XMLDataCollected();
  public String getInformation(){
    return info.datatoString();
  }
  @Override
  public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
    // TODO Auto-generated method stub
    if(localName.equals("code")){
      String element=attributes.getValue("build");
      info.setData(element);
    }
  }
}

(d)XMLDataCollected

package com.xyz.xmlpar1;
public class XMLDataCollected {
  String datac=null;
  public void setData(String c){
    datac=c;
  }
  public String datatoString(){
    return datac;
  }
}

So when I type first or second or third it should retrieve the data. But it does not.
What am I doing wrong?

  • 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-17T12:29:39+00:00Added an answer on June 17, 2026 at 12:29 pm

    It looks like the problem is with your handler. You are only adding a startElement function, but really you need to at least deal with starting and ending a tag.

    Also, your localName.equals("code") should actually look for the name of the tag i.e.localName.equals("stuff")

    There is a full example here: http://www.mysamplecode.com/2011/11/android-parse-xml-file-example-using.html and plenty of posts here on http://stackoverflow.com to get you a fully working parser using SAX.

    I would definitely suggest trying to debug your code to see what is actually happening when you run it. You can do this by using the debug mode in your IDE or by simply throwing some System.out.println() or Log.d() statements in there.

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

Sidebar

Related Questions

Possible Duplicate: Create a date with T-SQL I've a data table that stores each
Possible Duplicate: Create Browser-Bookmark from app I have tried to create a bookmark for
Possible Duplicate: How do you create a Perl module? I have the script that
Possible Duplicate: Create provisioning profile in iphone application i developed my iphone app and
Possible Duplicate: Create an alert on any view controller after Facebook request:didFailWithError: I have
Possible Duplicate: How can I create a registeration form connected with an xml webservice?
Possible Duplicate: how to add button dynamically in android? I need to create a
Possible Duplicate: Can The Android drawable directory contain subdirectories? How to organize graphics/java/xml file
Possible Duplicate: How to create XSD file programmatically in C#? I have an XDocument
Possible Duplicate: foreach and simplexml I have got my XML document loading correct but

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.