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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:12:52+00:00 2026-05-18T08:12:52+00:00

I need help :D. I am using web service that returns similar xml file

  • 0

I need help :D. I am using web service that returns similar xml file to the one below:

<books>
<item_1>
    <name></name>
    <type></type>
</item_1>
<item_2>
    <name></name>
    <type></type>
</item_2>
<item_3>
    <name></name>
    <type></type>
</item_3>
...
...</books>

The problem is that I don’t know how to parse it in Android. Everything would be fine if the child elements of the ‘books’ tag would have the same name (item instead of: item_1, item_2, item_3…) Then I could easily use SAX parser.

Do you know how I could pare this kind of document in Android?

Thanks,
marqs

EDITED:

My problem is that the child elements of <<>books> tag ale numbered (item1,item2,item3 and so on) so I cannot identify them. That would not be a problem if the ‘books’ tag would have only ‘item’ child – so then I could use SAX parser and fetch ‘item’ list

  • 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-18T08:12:53+00:00Added an answer on May 18, 2026 at 8:12 am

    The problem that you’ve described doesn’t sound all that hard: If you can safely assume that every child of the books element is an “item”, then you can invoke your “item handler” for each child.

    Here’s pseudocode that (I think) would work, assuming that <books> is always and only the top-level element of the document:

    class BookHandler extends DefaultHandler {
       public void startElement(String namespaceURI, String localName, 
                    String qName, Attributes atts) {
            if (localName.equals("books") {
                // Don't need to do anything with the top level element
                return;
            }
            else {
                handleItem(namespaceURI, localName, qName, atts);
            }
        }
    
        public void  endElement(String namespaceURI, String localName, String qName) {
             if localName.equals("books") {
                // Stop parsing and exit.
             }
             else {
                // Stop parsing one item
             }
        }
    
        private void handleItem(String namespaceURI, String localName, 
                     String qName, Attributes atts) {
             // Handle one item, including handling the "name" and 
             // "type" child attributes
             if (localName.equals("name") {
                // handle the name
             }
             else if (localName.equals("type") {
                // handle the type
             }
        }
    }
    

    Even as pseudocode, that’s over-simplistic and ugly. An alternative approach, but which may be overblown for your needs, is to decompose your application into multiple ContentHandler classes, passing off responsibility as you reach the beginning or ending of certain elements.

    For example: Let’s assume that an instance of BookHandler is passed in to the parser.parse() call, to handle the top-level elements. Then:

    class BookHandler extends DefaultHandler {
       private ContentHandler m_parentHandler;
       private ContentHandler m_childHandler = null;
    
       // ... == appropriate args for the DefaultHandler constructor
       public BookHandler(ContentHandler parent, Attributes atts, ...) {
            super(...);
            m_parentHandler = parent;
            parent.getXMLReader().setHandler(this);
       }
    
       public void startElement(String namespaceURI, String localName, 
                    String qName, Attributes atts) {
            if (localName.equals("books") {
                // Don't need to do anything with the top level element
                return;
            }
            else {
                // Assume it's a new item element. (Note: ItemHandler's constructor
                // changes the parser's ContentHandler.)
                m_childHandler = new ItemHandler(this, atts, ...);
            }
        }
    
        public void  endElement(String namespaceURI, String localName, String qName) {
             if localName.equals("books") {
                // Stop parsing and exit.
             }
             else {
                // Note that this won't be called for "item" elements, UNLESS the
                // ItemHandler's endElement method explicitly invokes this method.
    
                // Stop parsing one item
    
             }
        }    
    }
    
    
    class ItemHandler extends DefaultHandler {
       private ContentHandler m_parentHandler;
    
        // ItemInfo is a class that holds all info about the current item
       private ItemInfo m_ItemInfo = null;
    
       // ... == appropriate args for the DefaultHandler constructor
       public ItemHandler(ContentHandler parent, Attributes atts, ...) {
            super(...);
            m_parentHandler = parent;
            m_ItemInfo = new ItemInfo(atts);
            parent.getXMLReader().setHandler(this);
       }
    
       public void startElement(String namespaceURI, String localName, 
                    String qName, Attributes atts) {
            if (localName.equals("name") {
                 // Handle the name. Update the current item as needed.
            }
            else  if (localName.equals("type") {
                 // Handle the type. Update the current item as needed.
            }
        }
    
        public void  endElement(String namespaceURI, String localName, String qName) {
             if localName.equals("name" || localName.equals("type") {
                 // Do nothing (?)
             }
             else {
                // Stop parsing the current item; 
                // let the parent class handle the next element.
                getXMLReader().setHandler(m_parentHandler);
    
                // OPTIONALLY -- depending on your app's needs -- call the
                // parent's endElement() method to let it know that we reached
                // the end of an item.
                m_parentHandler.endElement(namespaceURI, localName, qName);
             }
        }   
    }
    

    This scheme offers more flexibility, and more possibilities for reuse. For example, potentially the “books” element could now be a child of some other element, say “departments”, without requiring much if any changes to these classes.

    The pseudo-code isn’t perfect by any means. (For one thing, I’d want to think some more about where the handlers are switched off — in the parent class, or in the child. For another thing, I may have gone overboard in saving references to both parents and children in these classes.) But I hope it gives you ideas that you can start working with.

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

Sidebar

Related Questions

I have a web service which returns a string representing an Xml file. The
I really need help with interfaces in general... Any resources that you guys would
I have a regex call that I need help with. I haven't posted my
I need to create a .Net web service (WCF is out of the question)
I'm in the unfortunate situation where I need to consume web services that do
I need to write a web service on a .NET platform for an IPhone
I am trying to execute a user-defined Oracle function that returns a RefCursor using
I am working on a project that is using a Java service to encrypt
I recently posted a question about a WCF Restful web service that I am
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat

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.