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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:23:59+00:00 2026-05-23T15:23:59+00:00

I am sorry if this is a simple fix (I’m sure it probably is)

  • 0

I am sorry if this is a simple fix (I’m sure it probably is) but after a few hours of me and my friend google, I came up empty.

I have an XML file that I have retrieved from a server (using httpclient for cookie handling goodness). I now wish to search through the XML. The XML defines a set of playing cards with attributes. As an example it would be something like this.

<CardInfo>
     <Type>
           Player
     </Type>
     <ID>
           674868
    </ID>
</CardInfo>

There would obviously be a few more attributes within this, but that is to just illustrate the example.
There may be many of these ‘cardinfo’ per XML I pull, I would like some way of filtering the XML to store each card, and have the attributes of the card in an easy to access form. I obviously do not expect all of this done for me, but thought the context may be important for any solutions.

I’m sorry to ask this, and feel terrible for doing so, but even XML parsing and storage is difficult for an android nooby! (why can’t they let us do it in python for the love of god).

  • 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-23T15:24:00+00:00Added an answer on May 23, 2026 at 3:24 pm

    Use an XML pull-parser. It gives you the XML document is its logical pieces, part (event) per part. So for each CardInfo element, you create a new CardInto object, and for element name ‘Type’ you set the CardInfo type and for element name ‘ID’ you set the CardInfo type etc.

    Update
    Add read and write methods to your objects, or create a standalone class for read/write to and from XML:

    This example is for the StAX API, but I guess you get the picture (the methods and event types are the same, but have difference names).

    In MyObject:

    public void read(XMLStreamReader reader) throws XMLStreamException {
        int event; 
    
        // read to the first tag, so we are at level 1
        do {
            event = reader.next();
            if(event == XMLStreamConstants.START_ELEMENT) {
                break;
            }
    
        } while(reader.hasNext());
    
        int level = 1;
        do {
            event = reader.next();
            if(event == XMLStreamConstants.START_ELEMENT) {
                level++; // increment
    
                String localName = reader.getLocalName();
    
                if(localName.equals("Domain")) {
                    event = reader.next();
                    if(event == XMLStreamConstants.CHARACTERS) {
                        domain = reader.getText();
                    }
                } else if(localName.equals("URL")) {
                    event = reader.next();
                    if(event == XMLStreamConstants.CHARACTERS) {
                        url = reader.getText();
                    }
                } else if(localName.equals("Headers")) {
                    readHeaders(reader);
                    level--;
                } else throw new IllegalArgumentException("Unexpected element " + localName + " at " + reader.getLocation());
            }
    
    
            if(event == XMLStreamConstants.END_ELEMENT) { 
                level--; // decrement
            }
        } while(level > 0); // simple level check
    
    
    }
    

    Parse the subtype called headers:

    <Headers>
        <Header name="" value=""/>
        <Header name="" value=""/>
        <Header name="" value=""/>
    </Headers>
    

    using the code

    private void readHeaders(XMLStreamReader reader) throws XMLStreamException {
        int level = 1;
    
        int event;
        do {
            event = reader.next();
            if(event == XMLStreamConstants.START_ELEMENT) {
                level++;
    
                String localName = reader.getLocalName();
    
                if(localName.equals("Header")) {
                    CodeRequestHeader header = new CodeRequestHeader();
                    header.setName(reader.getAttributeValue(null, "name"));
                    header.setValue(reader.getAttributeValue(null, "value"));
                    headers.add(header);
                }
            } else if(event == XMLStreamConstants.END_ELEMENT) {
                level--;
            }
        } while(level > 0);
    }
    

    Now there are multiple ways of doing this stuff, but the important things are these:

    1. First go to level 1; read the root element
    2. Start a do-while loop at level 1
    3. Increment and decrement level on start and end elements so the level is correct
    4. Use the level to control the while loop, continue as long as level > 0
    5. For subelements (i.e. when some tag contains multiple tags which are mapped to object entities), create a new method repeating steps 2-4. Then decrement level by one when the method is used.

    If you use this pattern, you can pull-parse n-dimensional XML documents in an orderly and strict, proper way. Strictly speaking, the rules means that should be a ‘readHeader’ method in the readHeaders method, but it is not necessary if there are no subelements in the Header element. Have fun 😉

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

Sidebar

Related Questions

Hey guys, sorry for this probably fairly simple question but I'm starting to get
Update: After playing around with this for a few hours, went with a multi-query
Ok sorry this might seem like a dumb question but I cannot figure this
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
Sorry if this is a little off-topic for regular stackoverflow questions, but we're tearing
Sorry, if this is a noobish question, but I'm just getting started with Rails
I hope this question takes a simple fix, and I am just missing something
Sorry if this is a comp-sci 101 question. I'm just unsure if I'm missing
I'm sorry for this very newbish question, I'm not much given into web development.

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.