Design Paradigm to create an object from an XML file

To assign uni, I write a java program that must save and load the hierarchy of objects into XML files. For example, a program creates a Zoo class with a list of subclasses of "Animals", saves it in XML, and loads it again the next time the program starts.

The file may look like

<Zoo> <Animals> <Lion> <name>Leo</name> <age>5</age> <roar>fearsome</roar> </Lion> <Elephant> <name>Dumbo</name> <age>10</age> <ears>let him fly</ears> </Elephant> </Animals> </Zoo> 

There are a small finite number of subclasses of Animal I that need to be supported (~ 5). Each subclass of an animal has individual attributes (e.g., Ears and Roar). I am confused as to what is the best design template for creating an object and creating a file.

I currently have an XMLCreator class with methods like void createZooElement(Zoo) , void createLionElement(Lion) , etc. etc., and the XMLReader class with private Zoo createZoo(File) , private Lion createLionObject(Element) .

Is this a good way to go if it was the code you expect from others in the future? Or should each object have a constructor method that takes a file / element as a parameter and another method that returns a / XMLElement file? What is the path with maximum encapsulation / maintainability?

+2
source share
5 answers

I would just use JAXB , which allows you to combine the tree of objects (annotated classes) into XML and decouple the XML from the tree of objects. There are other Object to XML APIs, but JAXB comes with Java SE and works well.

+9
source

Using XStream , you can serialize most Java objects without any mapping . The names of the objects become the names of the elements in the generated XML, and the lines inside the classes form the contents of the XML element. Classes that you serialize with XStream should not implement the Serializable interface. XStream is a serialization tool, not a data binding tool, which means that it does not generate classes from an XML file or XML Schema (XSD)

+2
source

In my opinion, you need a Builder template or Factory template. Check out Wikipedia for more details. I would go with the Factory pattern because I am more familiar with it. Instead of calling it XMLCreator , I would call it AnimalFactory :

 public class AnimalFactory () { private static final AnimalFactory instance = new AnimalFactory (); // I make this private so that I can be sure there is only one instance // of the object. This is another pattern, the `Singleton` private AnimalFactory () {}; // This is how you would access the instance public static AnimalFactory getInstance () { return instance; } // This is where you create the animal instances. public Lion getLion () {...} public Elephant getElephant () {...} } 
+1
source

You need SAXParser with ContentHandler .

You will need to implement startElement ... endElement ... and several others.

You can change the parsing strategy by reading an element in the startElement methods.

It should be easy, I think.

This article should read well.


Also, if you continue to create methods like void createZooElement(Zoo) , you'll end up with unmanageable hundreds of methods at the end of the day.

0
source

There are many good options. To bind xml data.

XMLBean requires an XML Schema.

JiBX , castor , requires a mapping file.

0
source

Source: https://habr.com/ru/post/1440295/


All Articles