前言:
今天兄弟们对“jaxb解析带命名空间的xml”都比较着重,兄弟们都需要知道一些“jaxb解析带命名空间的xml”的相关资讯。那么小编也在网摘上网罗了一些有关“jaxb解析带命名空间的xml””的相关知识,希望同学们能喜欢,大家一起来学习一下吧!需要完成的内容是,使用java生成下面这种格式的XML
<soapenv:Envelope xmlns:soapenv="; xmlns:con=";> <soapenv:Header/> <soapenv:Body> <con:ConnectionDeviceDeviation> <con:x>123.738416194836</con:x> <con:y>31.5165519232721</con:y> </con:ConnectionDeviceDeviation > </soapenv:Body></soapenv:Envelope>
使用的类库:JAXB
定义包描述,这样采用JAXB生成的时候,它会将定义的namespaceURI与prefix匹配上。
package-info.java
@XmlSchema( elementFormDefault= XmlNsForm.QUALIFIED, xmlns={ @XmlNs(prefix="soapenv", namespaceURI=";), @XmlNs(prefix="con", namespaceURI=";) })import javax.xml.bind.annotation.XmlNs;import javax.xml.bind.annotation.XmlNsForm;import javax.xml.bind.annotation.XmlSchema;
放置一个空类在与package-info.java平级处,这样继承这个类的子类可以同样使用包名名。
Envelope.java
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import javax.xml.bind.PropertyException;import javax.xml.bind.annotation.*;public class Envelope {}
开发中实现自身业务的XML java类
EnvelopeRequest.java
import com.jb.adev.util.ws.xml.JaxbXmlUtil;import com.jb.adev.util.ws.xml.request.Envelope;import javax.xml.bind.JAXBException;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlElementWrapper;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlType;import java.util.ArrayList;import java.util.List;@XmlRootElement(namespace=";, name = "Envelope")@XmlType(propOrder = {"header", "body"})public class EnvelopeRequest extends Envelope { private List<ConnectionDeviceDeviation> body = new ArrayList<>(); private String header = ""; @XmlElementWrapper(namespace=";, name = "Body") @XmlElement(namespace=";, name = "ConnectionDeviceDeviation") public List<ConnectionDeviceDeviation> getBody() { return body; } public void setBody(ConnectionDeviceDeviation wcd) { this.body.add(wcd); } @XmlElement(namespace=";, name = "Header") public String getHeader() { return header; } public void setHeader(String header) { this.header = header; } }
业务自身的传参类
ConnectionDeviceDeviation.java
import javax.xml.bind.annotation.XmlElement;public class ConnectionDeviceDeviation { private String x; private String y; @XmlElement(namespace=";, name = "x") public String getX() { return x; } @XmlElement(namespace=";, name = "y") public String getY() { return y; } public void setX(String x) { this.x = x; } public void setY(String y) { this.y = y; }}
测试
public static void main(String[] args) throws JAXBException { JAXBContext ctx = JAXBContext.newInstance(EnvelopeRequest.class); EnvelopeRequest root = new EnvelopeRequest(); ConnectionDeviceDeviation dev = new ConnectionDeviceDeviation(); dev.setUserId("1"); dev.setWlgOid("1"); dev.setX("123"); dev.setY("456"); root.setBody(dev); Marshaller m = ctx.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(root, System.out);}
运行结果
标签: #jaxb解析带命名空间的xml #java生成xml文件 #生成xmljava #java创建xml