0

I have the following problem, with JAXB library.

XML

<par>
   <name>content</name>
   <value>This is a value</value>
</par>
<par>
    <name>map</name>
    <value>
      <info>
         <obj>obj1</obj>
      </info>
    </value>
</par>

Class Java

public static class Par {

    @XmlElement(name = "name")
    private String name;

    @XmlElement(name = "value")
    private Info valueObject;

    @XmlAnyElement(lax = true)
    private Node valueString;
}

public static class Info {

    @XmlElement(name = "obj")
    private String obj;
}

the problem is that the Info field is mapped correctly, so in the value valueObject i have the full object Info. But the string This is a value is not mapped, because JAXB library maps only the first field called "value".

What can I do to map the value string?

I found on StackOverflow one similar question, but in the response they said to use the object Node (DOM library). So, the field is treated as a String, and in a second moment they used another time the JAXB library to map the complex Object. So, this is not a good solution for my script because I have a list of string and a list of object; then, if the list is very long I will have to read the list.

What can I do ? Can you help me ?

Thanks a lot

DrBomber
  • 25
  • 7
  • I don't see how your `valueObject` could work for the second example, because the annotations show it's an element named `` of type `Info`, which has an element named `` of type `String`. Nowhere is the element named `` defined, so ` obj1 ` should not be mapped, only ` obj1 ` would be. – Andreas Sep 22 '20 at 20:04
  • Since the `` element can contain text content and/or element content, it needs to be defined as "mixed" content, and you'll have to process the mixed list yourself to get the text and/or object. See e.g. [How to deal with JAXB ComplexType with MixedContent data?](https://stackoverflow.com/q/12568247/5221149) – Andreas Sep 22 '20 at 20:07

0 Answers0