Search code examples
javaxmlxml-parsingxstream

don't read value of element in XML by xstream


i have an XML like below and i use XStream to parse this XML

<Annotation name="uniqueMembers">true</Annotation>

and a class for Annotation :

@XStreamAlias("Annotation")
public class Annotation {
@XStreamAsAttribute
private String name;

private String value;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}
}

i need value have "true" of this tag but when i call

 xStream.fromXML("myXml.xml");

it have null value why? how can i get "true" of this tag?

EDIT : part of MyXml.xml

<Dimension name="Branch" >
    <Hierarchy name="xxx" primaryKey="" hasAll="true" allMemberName="All" >
        <Table schema="vvv" name="ccc" />

        <Level name="State"  captionColumn="STATE_NAME" uniqueMembers="true" type="Integer" />
        <Level name="City"  captionColumn="CITY_NAME" uniqueMembers="true" type="Integer">
            <Annotations>
                <Annotation name="uniqueMembers">true</Annotation>
            </Annotations>
        </Level>
        <Level name="Branch" captionColumn="BRANCH_NAME" uniqueMembers="true" type="Integer" >
            <Annotations>
                <Annotation name="uniqueMembers">true</Annotation>
            </Annotations>
        </Level>
    </Hierarchy>
</Dimension>

Solution

  • As written at the moment, the class you've shown there expects something like

    <Annotation name="something">
      <value>the value</value>
    </Annotation>
    

    To get rid of the <value> element and just use the <Annotation> element's content, you need to use a ToAttributedValueConverter, this blog post has the details but essentially you need to add

    @XStreamConverter(value=ToAttributedValueConverter.class, strings={"value"})
    

    as a class-level annotation on your Annotation class.