Xml转化为Java,Java转化为Xml[JAXB]

JAXB:这是java处理xml的标准.
说明
[list]
[*] 使用了JAXBContext.createUnmarshaller()转化xml到Java类,这里可以指定命名空间.
[*] 使用了JAXBContext.createMarshaller()转化Java类到xml,这里可以指定命名空间.
[*] 设置JAXBContext.createMarshaller()输出xml的相当属性,格式化,编码,包含头信息,设置命名空间.重命令根元素.
[/list]


package org.frame.base.xml.jdk.bk;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;

import org.frame.base.xml.jdk.Contact;
import org.xml.sax.InputSource;

public class XmlToJava {

/**
* @param args
*/
public static void main(String[] args) {
System.setProperty("jaxp.debug", "1");
JAXBContext wgProviderContext;
try {
wgProviderContext = JAXBContext.newInstance(Contact.class);
Unmarshaller unmarshaller = wgProviderContext.createUnmarshaller();
Marshaller marshaller = wgProviderContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "GB2312");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否包含头信息
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,"http://www.ycl.com/schema/schema http://www.ycl.com/schema/schema.xsd");
// just set format with xml

// 把xml转为Java对象
Contact contact = (Contact) unmarshaller
.unmarshal(getInputSource());
System.out.println(contact);

System.out.println("反序列化对象到xml内容如下:");
JAXBElement<Contact> jaxbElement = new JAXBElement<Contact>(
new QName(null, "contact"), Contact.class, contact);
// 这里可以改写根元素.
marshaller.marshal(jaxbElement, System.out);
// 表现不错啊
} catch (JAXBException ex) {
ex.printStackTrace();
}
}

private static InputSource getInputSource() {
StringBuffer xml = new StringBuffer(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xml.append("<contact xmlns=\"http://www.ycl.com/schema/schema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.ycl.com/schema/schema http://www.ycl.com/schema/schema.xsd\" >");

xml.append("<item width=\"10\">");
xml.append("<uic>1</uic>");
xml.append("<fullName>ycl1</fullName>");
xml.append("<sex>a</sex>");
xml.append("</item>");

xml.append("<item width=\"11\">");
xml.append("<uic>2</uic>");
xml.append("<fullName>ycl2</fullName>");
xml.append("<sex>b</sex>");
xml.append("</item>");

xml.append("</contact>");

InputSource is = new InputSource(new StringReader(xml.toString()));
return is;
}
}




package org.frame.base.xml.jdk;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"items"})
@XmlRootElement(name ="contact",namespace="http://www.ycl.com/schema/schema")
public class Contact {

@XmlElement(name = "item",namespace="http://www.ycl.com/schema/schema")
private List<Item> items;

public Contact() {
items = new ArrayList<Item>();
}

@Override
public String toString() {
// TODO Auto-generated method stub
return items.toString();
}

}




package org.frame.base.xml.jdk;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAccessorType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"fullName","uic","sex"},namespace="http://www.ycl.com/schema/schema")
public class Item {

@XmlElement(name="uic",namespace="http://www.ycl.com/schema/schema")
private String uic;

@XmlElement(name="fullName",namespace="http://www.ycl.com/schema/schema")
private String fullName;

@XmlAttribute(name="width")
private String width;

@XmlElement(name="sex",namespace="http://www.ycl.com/schema/schema")
private String sex;

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getWidth() {
return width;
}

public void setWidth(String width) {
this.width = width;
}

public String getUic() {
return uic;
}
public void setUic(String uic) {
this.uic = uic;
}

public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}

@Override
public String toString(){
return "width:"+width+",uic:"+uic+",fullName:"+fullName+",sex:"+sex;
}
}



测试用例输出结果如下:

[width:10,uic:1,fullName:ycl1,sex:a, width:11,uic:2,fullName:ycl2,sex:b]
反序列化对象到xml内容如下:
<?xml version="1.0" encoding="GB2312" standalone="yes"?>
<contact xsi:schemaLocation="http://www.ycl.com/schema/schema http://www.ycl.com/schema/schema.xsd" xmlns:ns3="http://www.ycl.com/schema/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns3:item width="10">
<ns3:fullName>ycl1</ns3:fullName>
<ns3:uic>1</ns3:uic>
<ns3:sex>a</ns3:sex>
</ns3:item>
<ns3:item width="11">
<ns3:fullName>ycl2</ns3:fullName>
<ns3:uic>2</ns3:uic>
<ns3:sex>b</ns3:sex>
</ns3:item>
</contact>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值