然后有人又说,我想给节点加属性怎么办,看下面例子
这里用一种怪异的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来控制是否节点属性和节点值就好了
分享到:
相关推荐
JAXB学习笔记 项目为了进行xml与java bean的互转,参考了各位网友的推荐,最后选型为JAXB,闲暇之余整理了一下笔记。废话不多说,上代码
NULL 博文链接:https://kamuikyo.iteye.com/blog/1182316
多个关键字请用空格分隔,最多填写5个。点击右侧Tag快速添加 多个关键字请用空格分隔,最多填写5个。点击右侧Tag快速添加
jaxb学习资料
JAXB的使用JAXB的使用JAXB的使用JAXB的使用
无需安装,解压后即可。jaxb-api.jar,jaxb-xjc.jar,jaxb-impl.jar,activation.jar等相关jar包在lib文件夹下。
jaxb-api jaxb-impl jar
JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件
JAXB的安装包及插件
NULL 博文链接:https://lijie-insist.iteye.com/blog/2160015
在使用webservice,mule esb等需要jaxb的项目里经常会出现 JAXB 2.0 API is being loaded from the bootstrap classloader这个错误,按照打出的信息Use the endorsed directory mechanism to place jaxb-api.jar in ...
jaxb-api jaxb-impl jaxb-xjc jaxws-rt 这四个文件对应的jar包
JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术
java -jar JAXB2_20060607.jar On Windows, you can just double-click the jar file to execute. Release Notes Browse the release notes online, including what's new. Technical Support Please subscribe to...
JAXB的HelloWorld源码,适合想学习JAXB技术的新手学习。
使用jaxb生成XML例子,含有例子和注解解析
一个JAXB2的基础教程,教你快速运用JAXB通过XSD绑定XML和读写XML
jaxb-2.2.jar jaxb jax
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-api-2.3.0.jar
NULL 博文链接:https://xuxiheng.iteye.com/blog/2233860