`
kamuikyo
  • 浏览: 28249 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

JAXB学习笔记(四)

 
阅读更多

然后有人又说,我想给节点加属性怎么办,看下面例子

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

 

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;

/**
 * <p>attribute属性值不会加,看这个例子</p>
 */
@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对象来表示一个节点

package cn.uyunsky.blog.xml.demo4;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
public class DemoField {

	@XmlAttribute
	private String name;

	@XmlValue
	private String value;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("DemoField [name=");
		builder.append(name);
		builder.append(", value=");
		builder.append(value);
		builder.append("]");
		return builder.toString();
	}
	
	

}

 输出结果

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persion>
    <userid name="userid">112</userid>
    <username name="username">就不告诉你</username>
    <birthday name="birthday">2011-09-28</birthday>
</persion>
Persion [userid=DemoField [name=userid, value=112], username=DemoField [name=username, value=就不告诉你], birthday=DemoField [name=birthday, value=2011-09-28]]
 总之,通过如上代码能明白通过@XmlAttribute、@XmlValue来控制是否节点属性和节点值就好了

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics