1

I have a xml file and I want to represent it in java objects in best way (using the best data structures.) What is the best architecture for it.

Ex:

<Parent>
<child1 par=aaa par2=bbb>
<userName>xxx</userName>
<password>xxx</password>

</child1>

<child2>
<child22>

---------

</child22>
</child2>
</parent>

Here how do I manage the java classes and variable ?

Malintha
  • 4,512
  • 9
  • 48
  • 82

3 Answers3

2

This is what JAXB was born for. It's the built-in Java object-to-XML binding API.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

There's plenty of information readily available on Java XML parsers. Check out this thread for some great discussion and recommendations.

Community
  • 1
  • 1
jtravaglini
  • 1,676
  • 11
  • 19
0
Class Parent {
    // List containing child class objects
    List<child> childList = new ArrayList<child>();

    getChildList(){}
    setChildList(){}

}

Class Child {
    String username;
    String password;

    //getter/setters for username and password.//
}

The child class contains 2 strings to represent "username" and "password". This class represents the child node in the given xml.

The parent class contains a list of child class objects. This class represents the parent tag in the xml and list of child class objects is for the child nodes in the xml.

The XML can be parsed and the values for each node can be stored using the above architecture.

Alternatively, JAXB can be used to automate this conversion from XML to JAVA objects.

Evandro Teixeira
  • 321
  • 3
  • 18
r3ap3r
  • 2,775
  • 2
  • 17
  • 16
  • Flagged up in the lqp queue. Your answer needs to be fleshed out to minimally include an explanation of what your code is doing and how it solves the op's question. Code only answers are likely to attract down votes and possibly even close votes. Please use 'edit' to bring your answer into a context where readers can profit from it. – Gayot Fow Aug 18 '13 at 23:26