Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6013565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:34:02+00:00 2026-05-23T02:34:02+00:00

Let’s say the following XML is given: <?xml version=1.0 encoding=UTF-8?> <ResC> <Err text=Error text

  • 0

Let’s say the following XML is given:

<?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 may appear on every level of the XML.

Using Simple I would like to deserialize this XML. So, I have 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 have to annotate the 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 may appear? This seems like a lot of overhead. If so, then I would have to check every single object if it contains an error.

Is there any better and easier way? 🙂

Thanks,
Robert

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:34:03+00:00Added an answer on May 23, 2026 at 2:34 am

    The important thing to remember is that Simple XML should be able to follow any structure that you can logically generate using classes. So you could just create a BaseClass that uses an error interface and applies the Decorator pattern so that it passes all of that through to a concrete error class without any of the implementing objects needing to know what they have been given.

    That probably made no sense. How about I just show you…okay…I just went away and implemented exactly what I was thinking and here are the results (full code link):

    The 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 just runs simple and displays the results.

    The 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 everything should extend if it wants ‘Err’.

    The Error interface:

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

    The 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;
        }
    
    }
    

    The actual implementing classes are after this point. You will see that they are rather trivial because the real work is being handled in the classes above.

    The Con class:

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

    The 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;
    }
    

    The ConRes class:

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

    The 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 is all that there is to it. Pretty simple right. I was able to bang that all out in ten minutes. It actually took me longer to write this response than it took me to write the code that I am giving you. If you do not understand anything about the code that I have just written then please let me know. I hope this helps you to understand how you might go about doing something like this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have saved this kind of data (some text '.date(d).' some text)
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say, I've got a XmlNode: <A>1</A> How to remove a text from it,
Let's say i have this block of code, <div id=id1> This is some text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.