0

Can anyone help me out with my problem for writing a JAXB java modules from XML Sample XML is as below. The issue I am having is with the Attribute class I am writing. In the XML you can see that the tag can be repeatable and has values. So if I put the List attribute, how can I fetch the value as well.

<top>
<attribute name="root">
<attribute name="a">
<attribute name="b">abValue</attribute>
<attribute name="d">
<attribute name="e">eValue</attribute>
</attribute>
</attribute>
<attribute name="c">cValue</attribute>
</attribute>
</top>

For human readability, here is formatted version:

<top>
    <attribute name="root">
        <attribute name="a">
            <attribute name="b">abValue</attribute>
            <attribute name="d">
                <attribute name="e">eValue</attribute>
            </attribute>
        </attribute>
        <attribute name="c">cValue</attribute>
    </attribute>
</top>

I have been referred to How to deal with JAXB ComplexType with MixedContent data? However If you see the tag will not contain both Value and XML at the same time. Either it will be a List or have value.

import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Top {

    List<Attribute> attribute;

}

I am having the issue with my Attribute class. I have no wsdl. I want a xml to be generated from the Java object after I do some pre-processing on it based upon the name attribute within every tag. It depends upon the values I fetch from the database.

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;

public class Attribute {

    @XmlAttribute(name="name")
    String name;

    List<Attribute> attribute;
}
Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
  • I am not sure I like your element name "attribute" but that is just me. it looks to me like you basically have a top level item `top` which contains one `attribute` element. Each `attribute` element can contain either a list of `attribute` elements or a value. Is this correct? – MartinByers Jul 23 '17 at 08:41
  • yes. I want to write a Java class which can used in JAXB for marshalling and unmarshalling. The main thing is that a tag can have a list of attributes or it can be leaf node as well. So List can be used but the value of it how this can be incorporated. – user2061157 Jul 23 '17 at 09:30
  • Hi, sorry for the slow response. Before we look at the JaxB class structure, please can I ask you to talk through the data that the xml represents. Thanks in advance. Martin – MartinByers Jul 24 '17 at 21:29

0 Answers0