Destroy duplicate XML elements in Simple 2.5.3 (Java)

Let's say the following XML is provided:

<?xml version="1.0" encoding="UTF-8"?> <ResC> <Err text="Error text 1"/> <ConRes> <Err text="Error text 2"/> <ConList> <Err text="Error text 3"/> <Con> <Err text="Error text 4"/> </Con> </ConList> </ConRes> </ResC> 

As you can see, the <Err> element can be displayed at each level of XML.

Using Simple I would like to deserialize this XML. So, I created the following class:

 @Element(required=false) public class Err { @Attribute private String text; public void setText(String text) { this.text = text; } public String getText() { return text; } } 

However, how do I need to comment on classes for <ResC> , <ConRes> , <ConList> and <Con> ? Do I really have to declare an attribute of type <Err> in every single class in which it can appear? This is like overhead. If so, then I will have to check every single object if it contains an error.

Is there a better and easier way ?:-)

Thanks,
Robert

+1
source share
1 answer

It is important to remember that Simple XML must be able to follow any structure that you can logically generate using classes. That way, you can simply create a BaseClass that uses the error interface and apply the Decorator pattern so that it passes all of this through a specific error class without any implementing objects that needed to know what they were given.

That probably doesn't make sense. How about what I just show you ... okay ... I just left and implemented exactly what I was thinking about, and here are the results ( full code link ):

Main file:

 package com.massaiolir.simple.iface; import java.io.File; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; public class Main { public static void main(String[] args) throws Exception { Serializer serial = new Persister(); ResC resc = serial.read(ResC.class, new File("data/testdata.xml")); System.out.println(" == Printing out all of the error text. == "); System.out.println(resc.getErrorText()); System.out.println(resc.conRes.getErrorText()); System.out.println(resc.conRes.conList.getErrorText()); for (Con con : resc.conRes.conList.cons) { System.out.println(con.getErrorText()); } System.out.println(" == Finished printing out all of the error text. == "); } } 

It is simply simple and displays the results.

BaseObject.java class:

 package com.massaiolir.simple.iface; import org.simpleframework.xml.Element; public class BaseObject implements Error { @Element(name = "Err", required = false, type = ConcreteError.class) private Error err; @Override public String getErrorText() { return err.getErrorText(); } @Override public void setErrorText(String errorText) { err.setErrorText(errorText); } } 

This is the class that should be distributed if it wants to "Err".

Error Interface:

 package com.massaiolir.simple.iface; public interface Error { void setErrorText(String errorText); String getErrorText(); } 

ConcreteError class:

 package com.massaiolir.simple.iface; import org.simpleframework.xml.Attribute; public class ConcreteError implements Error { @Attribute private String text; @Override public String getErrorText() { return text; } @Override public void setErrorText(String errorText) { this.text = errorText; } } 

Actual implementation classes after this point. You will see that they are pretty trivial because the real work is handled in the classes above.

Con class:

 package com.massaiolir.simple.iface; public class Con extends BaseObject { } 

ConList Class:

 package com.massaiolir.simple.iface; import java.util.ArrayList; import org.simpleframework.xml.ElementList; public class ConList extends BaseObject { @ElementList(entry = "Con", inline = true) public ArrayList<Con> cons; } 

ConRes Class:

 package com.massaiolir.simple.iface; import org.simpleframework.xml.Element; public class ConRes extends BaseObject { @Element(name = "ConList") public ConList conList; } 

ResC Class:

 package com.massaiolir.simple.iface; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; @Root public class ResC extends BaseObject { @Element(name = "ConRes") public ConRes conRes; } 

And that’s all there is. Pretty simple. I was able to break it all in ten minutes. In fact, it took me longer to write this answer than it took me to write the code that I give you. If you do not understand anything about the code I just wrote, please let me know. Hope this helps you understand how you can do something like this.

0
source

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


All Articles