例如,我有一个导入另一个模式的简单模式。第二个模式 (urn:just:attributes, just-attributes.xsd) 只是定义了一个属性组。
<?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"
,也就是说,它们是根据命名空间的名称进行解析的。他们的软件必须是“第三方”才能工作。
有没有人知道解决这个问题的方法,没有在结果字符串中进行查找和替换?也许是自定义绑定规则?