[QUESTION]Use XML to create object

All your problems with Audio, Video and Images.

[QUESTION]Use XML to create object

Postby rXp » Thu Mar 25, 2010 9:50 am

Hello,

I'm making an application that will use a database of objects.
For example :
<items>
<item id=INT>
<name>String</name>
<recip>boolean</recip>
<compo>String</compo> // = "id;id;id;id"
<description>String</description>
</item>
<item>
[...]
</item>
</items>

How should I set up this ? At the first launch the application should create those or everything should be in the package (res/value) ? If they are in the package could they be modified after ?

Best regards,

rXp>!<
rXp
Junior Developer
Junior Developer
 
Posts: 18
Joined: Thu Mar 25, 2010 9:43 am

Top

Postby 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
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 21 guests