JAXB学习笔记(四)(一)

2014-11-24 02:57:44 · 作者: · 浏览: 2

然后有人又说,我想给节点加属性怎么办,看下面例子
这里用一种怪异的xml形势来说明怎么处理,至于为什么使用这样的xml格式,实际项目中就会有这种非正常思维的情况,人们总喜欢用节点属性(attributer)值来表示节点值(textValue),而让节点值为空,来达到所谓的结构清晰

Java代码
package cn.uyunsky.blog.xml.demo4;

import java.io.StringReader;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

/**
*

attribute属性值不会加,看这个例子


*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Persion {

private DemoField userid;

private DemoField username;

private DemoField birthday;

public DemoField getUserid() {
return userid;
}

public void setUserid(DemoField userid) {
this.userid = userid;
}

public DemoField getUsername() {
return username;
}

public void setUsername(DemoField username) {
this.username = username;
}

public DemoField getBirthday() {
return birthday;
}

public void setBirthday(DemoField birthday) {
this.birthday = birthday;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Persion [userid=");
builder.append(userid);
builder.append(", username=");
builder.append(username);
builder.append(", birthday=");
builder.append(birthday);
builder.append("]");
return builder.toString();
}

public static void main(String[] args) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Persion.class);
Persion persion = new Persion();

DemoField userid = new DemoField();
userid.setName("userid");
userid.setValue("112");
persion.setUserid(userid);

DemoField username = new DemoField();
username.setName("username");
username.setValue("就不告诉你");
persion.setUsername(username);

DemoField birthday = new DemoField();
birthday.setName("birthday");
birthday.setValue(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
persion.setBirthday(birthday);


Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

//换个显示方式
marshaller.marshal(persion, System.out);

StringWriter writer = new StringWriter();
marshaller.marshal(persion, writer);
String xml = writer.getBuffer().toString();

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object bean = unmarshaller.unmarshal(new StringReader(xml));
System.out.println(bean);

} catch (JAXBException e) {
e.printStackTrace();
}
}

}
DemoField对象来表示一个节点
Java代码
package cn.uyunsky.blog.xml.demo4;

import javax.xml.bind.annotation.XmlAccessType;