<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/MySchema"
xmlns:tns="http://www.example.org/MySchema"
elementFormDefault="qualified"
xmlns:ja="urn:just:attributes">
<import schemaLocation="just-attributes.xsd" namespace="urn:just:attributes"/>
<element name="MyElement">
<complexType>
<attributeGroup ref="ja:AttributeGroup"/>
</complexType>
</element>
</schema>
我正在使用Metro xjc Ant任务从此架构生成类。 我遇到的问题是,我正在与第三方应用程序交互时,命名空间非常奇怪。 在这种情况下,我需要一个字符串值,因此我必须对其进行序列化。 我使用样板代码来实现。
private static <T> String marshal(T object) throws JAXBException{
OutputStream outputStream = new ByteArrayOutputStream();
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(object, outputStream);
return outputStream.toString();
}
这让我得到了一个类似的东西。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:MyElement xmlns:ns1="urn:just:attributes" xmlns:ns2="http://www.example.org/MySchema" ns1:attrib1="1234" ns1:attrib2="5678"/>
我遇到的问题是,第三方期望类似于
xmlns:thirdpartyns="urn:just:attributes"
这样的东西,也就是说,他们是基于命名空间给定的名称进行解析。他们的软件必须要使用"thirdpartyns"才能正常工作。除了在生成的字符串中执行查找和替换之外,有没有其他方法可以解决这个问题?也许可以使用自定义绑定规则?
org.glassfish.jaxb.namespacePrefixMapper
。 - undefined