0

I am trying to serialize a couple of objects to xml with simplexml in Java. I have the following objects:

@Root()
class foo {
    @ElementList
    List<bar> foo;
}

@Root()
class bar {
    @Element
    String bar;
}

and the output is:

<foo>
    <bar>
        <bar>a</bar>
    </bar>
    <bar>
        <bar>b</bar>
    </bar>
    ...
</foo>

but, what i want is:

<foo>
    <bar>a</bar>
    <bar>b</bar>
    ...
</foo>

Any idea how this can be achieved? I'm working with legacy code and unfortunately I can't change the xml structure at all. I have a feeling that I probably need to create a custom serializer for the foo object so that the outer bar isn't serialized. Thanks!

1
  • If you can't change XML, why don't you change Java class? For example List<bar> foo; -> List<String> foo; Commented Apr 7, 2015 at 12:26

1 Answer 1

1

I think you can use the @Text annotation to add text to the bar element:

@Root()
class bar {
    @Text
    String bar;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.