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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:38:27+00:00 2026-06-13T11:38:27+00:00

Assume that I have file XML like this : <?xml version=1.0?> <P> <Content> <id>1016576</id>

  • 0

Assume that I have file XML like this :

<?xml version="1.0"?>
<P>
    <Content>
        <id>1016576</id>
        <date>20.08.2012</date>
        <placeOfBirth>KUALA LUMPUR</placeOfBirth>
    </Content>
    <Content>
        <id>1016620</id>
        <date>20.08.2012</date>
        <placeOfBirth>SINGAPORE</placeOfBirth>
    </Content>
    <Content>
        <id>1020907</id>
        <date>20.08.2012</date>
        <placeOfBirth>SINGAPORE</placeOfBirth>
    </Content>
</P>

I want to parse all the text and insert into database table which have _id, date and placeOfBirth column. I’ve tried this :

    Activity activity1 = this;
    String str="";
    Resources res = activity1.getResources();
    XmlResourceParser xmlPharser = res.getXml(R.xml.fileXML);
    String id,date,pob;
//database
        final databaseHelper myDbHelper = new databaseHelper(this);
        myDbHelper.open();
//insert into table while parsing xml
        try {
            xmlPharser.next();
            int eventType = xmlPharser.getEventType();
            String event = ""+eventType;
            Log.d("Event", event);
            while (eventType != XmlPullParser.END_DOCUMENT)
            {

        if(eventType == XmlPullParser.START_TAG )
            {
               if( xmlPharser.getName() == "id")
               {
                    id=xmlPharser.getText();
               }
               else if ( xmlPharser.getName() == "date" )
               {
                    date = xmlPharser.getText();
               }
               else if ( xmlPharser.getName() == "placeOfBirth" )
               {
                    pob = xmlPharser.getText();
               }
               myDbHelper.insertData(id,date,pob);
               myDbHelper.close();
            }
            eventType = xmlPharser.next();
        }
  } catch (XmlPullParserException e) {
        e.printStackTrace();
  } catch (IOException e) {
        e.printStackTrace();
  }

It doesn’t get any error LogCat display, but it dosen’t test the condition at each START_TAG.

How can I resolve this problem…
All answer would be appreciated..thanks

  • 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-13T11:38:28+00:00Added an answer on June 13, 2026 at 11:38 am

    Parsing XML can get really nasty if you’re not careful, specially with this parsers; you could try some other with simpler APIs or clearer ways of going through the hierarchy (ie. JDOM). You should also take a look at the examples at the Android developer’s site, it’s really straight forward (Parsing XML Data).

    All that been said, I fixed it for you. This should work (at least it does for me). Be careful though, it doesn’t have any error checking on malformed XML or database stuff.

    import java.io.IOException;
    
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    
    import android.app.Activity;
    import android.content.res.XmlResourceParser;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            XmlResourceParser parser = getResources().getXml(R.xml.filexml);
    
            final databaseHelper myDbHelper = null;
    
            try {
                myDbHelper = new databaseHelper(this);
                myDbHelper.open();
                while (parser.next() != XmlPullParser.END_TAG) {
                    if (parser.getEventType() != XmlPullParser.START_TAG) {
                        continue;
                    }
                    String name = parser.getName();
                    if (name.equals("Content")) {
                        String id = null, date = null, pob = null;
                        while (parser.next() != XmlPullParser.END_TAG) {
                            if (parser.getEventType() != XmlPullParser.START_TAG) {
                                continue;
                            }
                            name = parser.getName();
                            if (name.equals("id")) {
                                id = readText(parser);
                            } else if (name.equals("date")) {
                                date = readText(parser);
                            } else if (name.equals("placeOfBirth")) {
                                pob = readText(parser);
                            }
                        }
                        myDbHelper.insertData(id,date,pob);
                    }
                }
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (myDbHelper != null) {
                    myDbHelper.close();
                }
            }
        }
    
        private String readText(XmlPullParser parser) throws IOException,
                XmlPullParserException {
            String result = "";
            if (parser.next() == XmlPullParser.TEXT) {
                result = parser.getText();
                parser.nextTag();
            }
            return result;
        }
    

    One more comment, change the xml file name to lowercase 😉

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

Sidebar

Related Questions

So I have an xml file that is formatted something like this <TopXmlTree> <IndentedItem1>
I have a XML file that looks like this: <container> <bugs> <bug id=b1> <reporter>Tom</reporter>
I have an xml file like this, <?xml version=1.0 encoding=utf-8 ?> <root> <FeaturedProductCategories> <FeaturedProductCategory>
Assume that we have 3 different activities. Each activity has its own XML file
I have a file that consists of concatenated valid XML documents. I'd like to
I have an XML file that contains a version number (major.minor.build) element and I'd
I've got an XML file which looks somehow like this: <?xml version='1.0' encoding='UTF-8'?> <?xml-stylesheet
I have an xml file. <?xml version=1.0 encoding=UTF-8?> <channel> <item>content with special character é</item>
Assume that we have a large file which can be read in chunks of
Assume that I have a text file separated by colons. I understand how to

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.