by Johan Degraeve » Fri Mar 26, 2010 9:07 am
I've written a small package which uses the SAX Parser to parse XML. The package allows to create your own object hierarchy, where each XML Element type is represented by one class.
For example in your case you would need first to write your own classes
public class name() {
.. methods to get and set the name
}
public class description() {
...
}
public class item () {
private name aName;
private description aDescription
.....
}
public class items () {
ArrayList<item> itemlist.
...
}
Next thing to do would be to let every class above implement an Interface from my Package :
public class item() implements XMLElement ...
This interface defines a few methods which need to be implemented and allow to
- add children elements
- store attributes,
- store text (trimmed or untrimmed with String.trim(), as you wish)
...
Then you can parse your xml which is retrieved via a URL, a hardcoded String or a file read from the res folder in your android app and it will build up the complete object hierarchy.
I plan to put this on google code, as open source but need to add an example. If you could write your code that will store all your objects (ie complete what I just wrote above)
and if you allow me to use your code in my documentation, then I'll extend your code and send you the complete source code which allows you to parse XML.
There a few preresuisites :
- per tag that can occur in your XML, you need to have a class with the same name, in your case : "items", "item", "description", "compo" and "recip".
You may if you want prefix all classes with a fixed part, eg prefix = MYCLASSES, then your classes will be "MYCLASSESitems", "MYCLASSESitem", "MYCLASSESdescription", "MYCLASSEScompo" and "MYCLASSESrecip".
- each of your classes that represent an XML element must have a default constructor
Also I would need from you a sample XML file, either on the Internet or hardcoded in a String object, or as a file.
regards,
Johan