package com.karl.jaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "abc", namespace = "http://www.karl.com")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Star {
private String name = "Lady Gaga";
@XmlElement
int age = 10;
@XmlAttribute
String gender = "female";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "abc", namespace = "http://www.karl.com")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Star {
private String name = "Lady Gaga";
@XmlElement
int age = 10;
@XmlAttribute
String gender = "female";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.karl.jaxb;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class TestJAXB {
public static void main(String[] args) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Star.class);
Marshaller marshaller = context.createMarshaller();
Unmarshaller unmarsheller = context.createUnmarshaller();
Star star = new Star();
marshaller.marshal(star, System.out);
System.out.println();
String xml = "<abc xmlns=\"http://www.karl.com\"><name>Adele</name></abc>";
Star star2 = (Star) unmarsheller.unmarshal(new StringReader(xml));
System.out.println(star2.getName());
}
}
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class TestJAXB {
public static void main(String[] args) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Star.class);
Marshaller marshaller = context.createMarshaller();
Unmarshaller unmarsheller = context.createUnmarshaller();
Star star = new Star();
marshaller.marshal(star, System.out);
System.out.println();
String xml = "<abc xmlns=\"http://www.karl.com\"><name>Adele</name></abc>";
Star star2 = (Star) unmarsheller.unmarshal(new StringReader(xml));
System.out.println(star2.getName());
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:abc xmlns:ns2="http://www.karl.com" gender="female"><age>10</age><name>Lady Gaga</name></ns2:abc>
Lady Gaga