Java/XML/XmlRootElement

Материал из Java эксперт
Перейти к: навигация, поиск

Mark a POJO with XmlRootElement with name and namespace

   <source lang="java">

import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "person", namespace = "http://www.example.ru/myperson") public class MyPerson {

 private String first;
 private String last;
 public static void main(String[] args) throws JAXBException {
   MyPerson p = new MyPerson();
   p.first = "l";
   p.last = "h";
   JAXBContext context = JAXBContext.newInstance(MyPerson.class);
   Marshaller marshaller = context.createMarshaller();
   marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
   marshaller.marshal(p, System.out);
 }
 public String getFirst() {
   return first;
 }
 public void setFirst(String first) {
   this.first = first;
 }
 public String getLast() {
   return last;
 }
 public void setLast(String last) {
   this.last = last;
 }

}

</source>