1

I need to send this certain type of xml message to a web service;

<Personel>
    <name value="HelpMe"/>
    <surname value="Please"/>
</Personel>

My code is like;

@XmlRootElement(name = "Personel")
@XmlAccessorType(XmlAccessType.FIELD)
public class Personel{

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

    @XmlElement(name = "surname")
    String surname;
}

But this code produces xml like;

<Personel>
    <name>HelpMe<name/>
    <surname>Please<surname/>
</Personel>

I couldn't find a proper way to do this without create name and surname classes with attribute fields named "value".

Ahmet Orhan
  • 304
  • 3
  • 12

2 Answers2

0

If you need below format.

<Personel>
    <name value="HelpMe"/>
    <surname value="Please"/>
</Personel>

Create PersonelName and PersonelSurname and then use these classes as XmlElement in Personel classs.

@XmlAccessorType(XmlAccessType.FIELD)
public class PerosonelName {

   @XmlValue
    String value;

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

}

@XmlAccessorType(XmlAccessType.FIELD)
public class PersonelSurname {

   @XmlValue
    String value;

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

}


@XmlRootElement(name = "Personel")
@XmlAccessorType(XmlAccessType.FIELD)
public class Personel{

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

    @XmlElement(name = "surname")
    String PersonelSurname ;
}
theduck
  • 2,589
  • 13
  • 17
  • 23
Venu Kumar
  • 21
  • 2
0

I've found moxy implementation of jaxb as a solution. It gives the capability giving default attribute keys.

Answer about using moxy as default jaxb implementation : Use Moxy as default JAXB Implementation

@XmlRootElement(name = "Personel")
@XmlAccessorType(XmlAccessType.FIELD)
public class Personel{

    @XmlPath("name/@value")
    String name;

    @XmlPath("surname/@value")
    String surname;
}

So above code generates the following xml as I desired,

<Personel>
    <name value="HelpMe"/>
    <surname value="Please"/>
</Personel>
Ahmet Orhan
  • 304
  • 3
  • 12