具有自定义方案的 JAXB 对象
JAXB object with custom scheme
我想制作这个 XML:
<payment xmlns="http://www.elastic-payments.com/schema/payment">
<merchant-account-id>1233</merchant-account-id>
........
</payment>
我用 JAXB 试过这个:
@XmlRootElement(name = "payment")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace="http://www.elastic-payments.com/schema/payment")
public class AuthorizeRequest {
@XmlElement(name = "merchant-account-id")
public String merchantAccountId;
..........
}
但我只得到这个:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<payment>
<merchant-account-id>1233</merchant-account-id>
</payment>
你知道我如何为根标签 payment
设置自定义 xmlns
吗?
Do you know how I can set custom xmlns for the root tag payment?
删除@XmlType 并在您的@XmlRootElement 中添加命名空间。像这样。
@XmlRootElement(name = "payment", namespace="http://www.elastic-payments.com/schema/payment")
@XmlAccessorType(XmlAccessType.FIELD)
public class AuthorizeRequest {
@XmlElement(name = "merchant-account-id")
public String merchantAccountId;
..........
}
输出会像这样,你可能不想要
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:payment xmlns:ns2="http://www.elastic-payments.com/schema/payment">
<merchant-account-id>1233</merchant-account-id>
</ns2:payment>
如果要删除此 header (<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
),请将 JAXB_FRAGMENT 设置为 true。
JAXBContext jaxbContext = JAXBContext.newInstance(AuthorizeRequest.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); //remove header
AuthorizeRequest authorizeRequest = new AuthorizeRequest();
authorizeRequest.setMerchantAccountId("123123");
//jaxbMarshaller.marshal(authorizeRequest,System.out); //print to console
最后删除额外的 ns2 select AuthorizeRequest.class 所在的包并创建一个 package-info.java 文件并添加以下注释。如果需要,您可以更改它
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.elastic-payments.com/schema/payment", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = { @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://www.elastic-payments.com/schema/payment", prefix = "") })
package com.foo.bar;
我想制作这个 XML:
<payment xmlns="http://www.elastic-payments.com/schema/payment">
<merchant-account-id>1233</merchant-account-id>
........
</payment>
我用 JAXB 试过这个:
@XmlRootElement(name = "payment")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace="http://www.elastic-payments.com/schema/payment")
public class AuthorizeRequest {
@XmlElement(name = "merchant-account-id")
public String merchantAccountId;
..........
}
但我只得到这个:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<payment>
<merchant-account-id>1233</merchant-account-id>
</payment>
你知道我如何为根标签 payment
设置自定义 xmlns
吗?
Do you know how I can set custom xmlns for the root tag payment?
删除@XmlType 并在您的@XmlRootElement 中添加命名空间。像这样。
@XmlRootElement(name = "payment", namespace="http://www.elastic-payments.com/schema/payment")
@XmlAccessorType(XmlAccessType.FIELD)
public class AuthorizeRequest {
@XmlElement(name = "merchant-account-id")
public String merchantAccountId;
..........
}
输出会像这样,你可能不想要
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:payment xmlns:ns2="http://www.elastic-payments.com/schema/payment">
<merchant-account-id>1233</merchant-account-id>
</ns2:payment>
如果要删除此 header (<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
),请将 JAXB_FRAGMENT 设置为 true。
JAXBContext jaxbContext = JAXBContext.newInstance(AuthorizeRequest.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); //remove header
AuthorizeRequest authorizeRequest = new AuthorizeRequest();
authorizeRequest.setMerchantAccountId("123123");
//jaxbMarshaller.marshal(authorizeRequest,System.out); //print to console
最后删除额外的 ns2 select AuthorizeRequest.class 所在的包并创建一个 package-info.java 文件并添加以下注释。如果需要,您可以更改它
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.elastic-payments.com/schema/payment", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = { @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://www.elastic-payments.com/schema/payment", prefix = "") })
package com.foo.bar;