How to bind an xml element to a member variable of an object?

I am trying to decouple xml with an object using moxy.Below is an xml sample.

<root>
    <name>
        <firstname>value</firstname>
    </name> 
    <address>value of address</address>
</root>

And below is the class I'm trying to display.

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="root")
@XmlAccessorType(XmlAccessType.FIELD)

public class Response {
  @XmlPath("name/firstname/text()")
  String name;
  Address address;
}

class Address {
  String addressline;
}

Now, how do I get the values ​​of the address tag in XML and bind it to the address bar variable of the Address class.

+4
source share
2 answers

You need to use annotation @XmlValuein the property addressline.

@XmlAccessorType(XmlAccessType.FIELD)
class Address {
    @XmlValue
    String addressline;
}
0
source

this is the answer to a similar (but not quite the same) question that was related here:

. ( ) @ XmlValue getMessageText(), @XmlElement. "XmlValue", , XmlElement.

XmlValue . , :

  • @ XmlAccessorType (XmlAccessType.NONE)

-, - . , JABX get/set XML , -, XmlValue, XML POJO ( ).

  @XmlAccessorType( XmlAccessType.NONE )
  @XmlRootElement(name = "announcement")
  public class Announcement
  {
      ... 

      @XmlValue
      public  String getMessageText(){
          return this.messageText;
      }
  }

. JAXB , , , , . , , , , .

0

Source: https://habr.com/ru/post/1543350/


All Articles