pstrag
pstrag

Reputation: 617

SimpleXML framework: element with element or text

I must to parse XML which can have two forms:

<Command><Variable/></Command>

or:

<Command>some text</Command>

How can I do this? When I try to declare both @Element and @Text in class responsible for Command parsing then exception is thrown when I try to parse XML to instance of this class.

My current version of code:

@Root(name = "Command", strict = false)
public class AppCommand {

    @Element(name = "Variable", required = false)
    @Getter
    private Variable variable;

    @Text(required = false)
    @Getter
    private String content;

}

And exception is: Text annotation @org.simpleframework.xml.Text(required=false, empty=, data=false) on field 'content' private java.lang.String com.example.AppCommand.content used with elements in class com.example.AppCommand

Upvotes: 2

Views: 1694

Answers (2)

pstrag
pstrag

Reputation: 617

My solution (not beautiful, but works and doesn't require much work to implement):

    private static class SerializerWithPreprocessor extends Persister {

        public SerializerWithPreprocessor(RegistryMatcher matcher, Format format) {
            super(matcher, format);
        }

        @Override
        public <T> T read(Class<? extends T> type, String source) throws Exception {
            source = source.replaceFirst("<Command (.*)>([[\\w||[+=]]&&[^<>]]+)</Command>", "<Command $1><Content>$2</Content></Command>");
            return super.read(type, source);
        }
    }

So I just created new Serializer class. This class use regular expressions to change Text element inside Command into normal Element. Then I can use:

@Root(name = "Command", strict = false)
public class AppCommand {

    @Element(name = "Variable", required = false)
    @Getter
    private Variable variable;

    @Element(name = "Content", required = false)
    @Getter
    private String content;
}

and during deserialization everything works like I wanted to.

Upvotes: 2

user1907906
user1907906

Reputation:

Yes, Simple can't deal with this.

Command.java:

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Text;

@Root
public class Command {

    @Element(required = false, name = "Variable")
    private Variable variable;

    @Text(required = false)
    private String text;    
}

Variable.java:

class Variable {
}

SOPlayground.java:

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

public class SOPlayground {

    public static void main(String[] args) throws Exception {
        Serializer serializer = new Persister();
        String xml1 = "<Command><Variable/></Command>";
        String xml2 = "<Command>some text</Command>";

        serializer.validate(Command.class, xml1);
        serializer.validate(Command.class, xml2);
    }

}

This does compile but it does not run:

Exception in thread "main" org.simpleframework.xml.core.TextException: Text annotation @org.simpleframework.xml.Text(data=false, required=false, empty=) on field 'text' private java.lang.String de.lhorn.so.Command.text used with elements in class de.lhorn.so.Command

It looks like can not have both @Element and @Text members.

Upvotes: 1

Related Questions