gpt4 book ai didi

java - 使用 moxy 编码和解码更改 xml

转载 作者:行者123 更新时间:2023-11-30 04:22:57 25 4
gpt4 key购买 nike

我正在尝试创建 this kind(xsd inside)的文件。一些例子是 here 。由于根元素和其他一些常量元素中的常量值,我使用 eclipse 生成了一个模板:

<?xml version="1.0" encoding="UTF-8"?>
<invoice:response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:invoice="http://www.forum-datenaustausch.ch/invoice" xmlns="http://www.forum-datenaustausch.ch/invoice" xsi:schemaLocation="http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd" language="de">
<invoice:processing>
<invoice:transport from="" to="">
<invoice:via sequence_id="0" via=""/>
</invoice:transport>
</invoice:processing>
<invoice:payload response_timestamp="0">
<invoice:invoice request_date="2001-12-31T12:00:00" request_id="" request_timestamp="0"/>
</invoice:payload>
</invoice:response>

但是简单的解码和编码会改变内容:

<?xml version="1.0" encoding="UTF-8"?>
<response xmlns="http://www.forum-datenaustausch.ch/invoice" xmlns:ns1="http://www.w3.org/2000/09/xmldsig#" xmlns:ns0="http://www.w3.org/2001/04/xmlenc#" language="de">
<processing>
<transport from="" to="">
<via via="" sequence_id="0"/>
</transport>
</processing>
<payload response_timestamp="0">
<invoice request_timestamp="0" request_date="2001-12-31T12:00:00.0" request_id=""/>
</payload>
</response>

由于某种原因,架构位置属性消失了。这可以在编码之前手动添加。第二个问题是,所有前缀都消失了。我不知道谁使用了生成的 xml(他们是否使用手写代码解码?有或没有验证?)。因此,我想要一个与给定示例最相似且有效的输出。那么有没有办法保持现有元素和属性不变,并让 moxy 为每个元素添加命名空间前缀?

最佳答案

以下内容应该有所帮助。这个问题也在 EclipseLink 论坛上得到处理:

<小时/>

for some reason the schema location attribute is gone.

您可以在 Marshaller 上指定以下属性来输出架构位置:

marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd");
<小时/>

2nd problem is, all prefixes are gone.

命名空间前缀消失了,但命名空间限定是相同的(所有元素都具有相同的本地名称和命名空间 URI)。在第一个文档中,invoice 前缀被分配给 http://www.forum-datenaustausch.ch/invoice 命名空间,在第二个文档中,该命名空间被分配为默认命名空间

<小时/>

在设计时控制命名空间前缀

您可以通过利用 @XmlSchema 注释来提供有关应使用哪些命名空间前缀的 MOXy 提示(请参阅: http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html )。

包信息

@XmlSchema(
elementFormDefault=XmlNsForm.QUALIFIED,
namespace="http://www.forum-datenaustausch.ch/invoice",
xmlns={
@XmlNs(prefix="invoice", namespaceURI="http://www.forum-datenaustausch.ch/invoice"),
@XmlNs(prefix="ds", namespaceURI="http://www.w3.org/2000/09/xmldsig#"),
@XmlNs(prefix="xenc", namespaceURI="http://www.w3.org/2001/04/xmlenc#")
}
)
package forum16559889;

import javax.xml.bind.annotation.*;

回应

package forum16559889;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Response {
}

演示

package forum16559889;

import javax.xml.bind.*;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Response.class);

Response response = new Response();

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd");
marshaller.marshal(response, System.out);
}

}

输出

<?xml version="1.0" encoding="UTF-8"?>
<invoice:response xsi:schemaLocation="http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:invoice="http://www.forum-datenaustausch.ch/invoice" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

在运行时控制命名空间前缀

您可以利用 MOXy 的 NamespacePrefixMapper 扩展来控制运行时使用的命名空间前缀。

package forum16559889;

import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.oxm.NamespacePrefixMapper;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Response.class);

Response response = new Response();

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd");

marshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, new NamespacePrefixMapper() {

@Override
public String getPreferredPrefix(String namespaceUri,
String suggestion, boolean requirePrefix) {
if("http://www.forum-datenaustausch.ch/invoice".equals(namespaceUri)) {
return "invoice";
} else if("http://www.w3.org/2000/09/xmldsig#".equals(namespaceUri)) {
return "ds";
} else if("http://www.w3.org/2001/04/xmlenc#".equals(namespaceUri)) {
return "xenc";
} else {
return null;
}
}

});

marshaller.marshal(response, System.out);
}

}

关于java - 使用 moxy 编码和解码更改 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16559889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com