0

I'm a decent C++ programmer, good enough to do what I want. But I'm working on my first Android App (obviously not C++ related), and I'm having an issue where I'd like to translate what I know from C++ over to the XML/Java used in Android Studio.

Basically I have (in C++) an array of structures. And maybe I didn't do the perfect search, but I sure as heck tried to look around for the answer, but I didn't come up with anything.

How would I go about placing an array of structures inside the XML file and utilizing it in Java?

As a bit of a buffer, let me say that I'm not really looking for code, just verification that this is possible, and a method on how to go about it. I don't mind researching to learn what I want, but I haven't come up with anything. Like I said, I probably haven't googled it properly because I'm unsure of exactly how to ask it.

EDIT: So it appears that XML doesn't have a structure (or anything similar? not sure). But I can utilize a Java class with public variables. Now my question is more or less: What would be the best way to go about inserting all the information into the array/class/variables?

In C++ terms, I could neatly place all the info into a text file and then read from it, using a FOR loop to place all the info in the structures. Or, if I don't want to use an outside source/file, I could hardcode the information into each variable. Tedious, but it'd work. I'm not sure, in Android terms, if I could use the same method and pack in a text file with the app, and read from the file using a FOR loop to insert the information into the array/class/variables

class answerStruct
{
    public String a;

    public boolean status;

};



class questionStruct
{
    public String q;

    answerStruct[] answer = new answerStruct[4];

};

I'm not placing this here to brag at my super high tech program, but to give a visual, and frankly that's less I have to write out. This is the method I plan on going with. But, being Java, I'm open to possibly better options. My question still stands as far as inputting information into the variables. Hard code? or does Android/Java allow me to place a text file with my app, and read from it into the variables?

lilgodwin
  • 1,098
  • 3
  • 13
  • 26
  • 1
    Java has no concept of structs. Everything (except for the 6 primitive types) is a class in Java. So the equivalent of an array of structs in C++ is an array (or `ArrayList`) of objects. XML representation is completely up to you. There is no good or wrong. If you can't make up your mind, I'd suggest `` – tofi9 Feb 11 '15 at 04:33
  • @taoufik, Thanks for the prompt reply, I appreciate it and it verified what I recently found (of course, after I actually posted here go figure). If you would, check out the edit to my post. With my recent findings, my question has transformed a bit. Again, thanks. By the way, I'm going to look into your suggestion, so it wasn't for nothing :-) – lilgodwin Feb 11 '15 at 04:48

1 Answers1

0

XML is just a markup language for tree-structured data, and imposes no restrictions on how you name or structure your tree nodes.

What I think that you're looking for is an XML Object Serialiser: a way to serialise your in-memory structure into XML for a more permanent storage, and then at a later run, deserialise it back into memory. There are many XML Serialisers for Java, each with an own proprietary XML format.

I've used Simple XML in the past, and found it easy and flexible.

Community
  • 1
  • 1
tofi9
  • 5,775
  • 4
  • 29
  • 50