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).
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:
Parse the subtype called headers:
using the code
Now there are multiple ways of doing this stuff, but the important things are these:
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 😉