Jaxb处理obj(泛型)<--->xml

  最近调试要求xml报文交互项目,之前就应用过Jaxb来处理object互转xml,只是此次项目中接口报文较多,且相似,所以将过去的代码要重新封装重构下。

如下报文,外层不变,只有body内容不固定,所以用到泛型加以控制:

<?xml version="1.0" encoding="UTF-8"?>
<Document>
    <Request>
        <Head>
            <Version>Version</Version>
            <orgId>orgId</CertId>
        </Head>
        <Body>
            <infoList>
                <info>
                    <name>name</name>
                    <dept>dept</dept>
                </info>
                <info>
                    <name>111111111</name>
                </info>
            </infoList>
        </Body>
    </Request>
</Document>

 

实体Document

package com.test;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "request"
})
@XmlRootElement(name = "Document")
public class Document {
	
    @XmlElements({
	@XmlElement(name = "Request", type = com.test.intfc.Request.class)
    })
    protected BaseRequest<? extends BaseBody> request;

    setter()/getter()
}


实体BaseRequest

package com.test;

@XmlTransient
public abstract class BaseRequest<T extends BaseBody> {

    protected Head head;
    protected T body;
    
    public abstract Head getHead();
    public abstract void setHead(Head value);
    public abstract T getBody();
    public abstract void setBody(T value);
}

 

实体Head     ......略

实体Basebody

package com.test;

@XmlTransient
public abstract class BaseBody {}


实体Request

package com.test.intfc;

@XmlRootElement(name = "Request")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(name = "", propOrder = { "head", "body" })
public class Request extends BaseRequest<Body> {

	@XmlElement(name = "Head", required = true)
	@Override
	public Head getHead() {
		return head;
	}

	@Override
	public void setHead(Head value) {
		this.head = value;
	}

	@XmlElement(name = "Body", required = true)
	@Override
	public Body getBody() {
		return body;
	}

	@Override
	public void setBody(Body value) {
		this.body = value;
	}
}

 

实体Body

package com.test.intfc;

@XmlRootElement(name = "Body")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "name",
    "dept"
})
public class Body extends BaseBody {
    // 简写....
    @XmlElement(name = "name", required = true)
    protected String name;
    
    @XmlElement(name = "dept", required = true)
    protected String dept;
    
    ...setter()/getter()
}


测试

package com.test;

public class Test {
     public static void main(String[] args) {
		......

		Request req = new Request();
		Document doc = new Document();
		doc.setRequest(req);
		JaxbBinder reqBinder = new JaxbBinder(Document.class);

		String xmlReq = reqBinder.toXml(doc, true);

    }
}


JaxbBinder从网上搜,一堆堆的例子。。。。

 

 xml -->  xsd --> Object 方式快速生成的

命令如:$ xjc -d . -p com.test ./example.xsd

生成的Object实体需要自己按上面的修改,注意“@注解”的应用,否则将报异常:[javax.xml.bind.JAXBException:类 org.test.******。或其任何超类是 已知此上下文。]

 

 

 

 

根据上面的XML文件可以创建以下Java类: ```java import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "Results") public class BlcInOutStoreResponse { @XmlElement(name = "InOutStoreHead") private InOutStoreHead inOutStoreHead; @XmlElement(name = "InOutStoreList") private InOutStoreList inOutStoreList; @XmlElement(name = "InOutStorePassPortList") private InOutStorePassPortList inOutStorePassPortList; // getters and setters } @XmlAccessorType(XmlAccessType.FIELD) class InOutStoreHead { @XmlElement(name = "oid") private int oid; @XmlElement(name = "sasStockPreentNo") private int sasStockPreentNo; @XmlElement(name = "sasStockNo") private int sasStockNo; @XmlElement(name = "chgTmsCnt") private int chgTmsCnt; // getters and setters } @XmlAccessorType(XmlAccessType.FIELD) class InOutStoreList { @XmlElement(name = "headOid") private int headOid; @XmlElement(name = "sasStockSeqno") private int sasStockSeqno; @XmlElement(name = "chgTmsCnt") private int chgTmsCnt; @XmlElement(name = "gdsMtno") private int gdsMtno; // getters and setters } @XmlAccessorType(XmlAccessType.FIELD) class InOutStorePassPortList { @XmlElement(name = "PassportNo") private int passportNo; // getters and setters } ``` 这里使用了JAXB(Java Architecture for XML Binding)来进行XML和Java类之间的映射。注意要添加相应的依赖,比如在Maven项目中可以添加以下依赖: ```xml <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.2</version> </dependency> ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值