我正在使用 Jaxb 生成一些 XML,看起来相当不错。这是一个片段:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<ns2:oval_system_characteristics xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5 oval-system-characteristics-schema.xsd http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#esx esx-system-characteristics-schema.xsd"
xmlns:ns2="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5" xmlns="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:ns3="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#esx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...
<ns3:visdkmanagedobject_item id="1">
<ns3:property>isolation.tools.diskWiper.disable</ns3:property>
<ns3:value datatype="boolean">true</ns3:value>
</ns3:visdkmanagedobject_item>
这是符合标准的 XML。不幸的是,我受到下游系统的限制,它只接受以下格式的 XML:
<visdkmanagedobject_item id="1" xmlns="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#esx">
<property>isolation.tools.diskWiper.disable</property>
<value datatype="boolean">true</value>
</visdkmanagedobject_item>
其中某些元素通过 xmlns 属性命名。
现在的问题是,如何让 Jaxb 停止(做正确的事情)使用前缀命名元素并开始使用 xmlns 属性命名元素?
I'm generating some XML with Jaxb that looks pretty good. Here's a snipit:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<ns2:oval_system_characteristics xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5 oval-system-characteristics-schema.xsd http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#esx esx-system-characteristics-schema.xsd"
xmlns:ns2="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5" xmlns="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:ns3="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#esx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...
<ns3:visdkmanagedobject_item id="1">
<ns3:property>isolation.tools.diskWiper.disable</ns3:property>
<ns3:value datatype="boolean">true</ns3:value>
</ns3:visdkmanagedobject_item>
This is standards compliant XML. Unfortunately the I'm limited by the constraints of the downstream system, it only accepts XML formatted this way:
<visdkmanagedobject_item id="1" xmlns="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#esx">
<property>isolation.tools.diskWiper.disable</property>
<value datatype="boolean">true</value>
</visdkmanagedobject_item>
Where certain elements are namespaced through the xmlns attribute.
Now for the question, how do I get Jaxb to stop (doing the right thing) namespacing the elements with a prefix and start namespacing the elements with the xmlns attribute?
您可以通过在包级别利用
@XmlSchema
注释来设置默认命名空间。这是通过利用package-info
类(如下例)来完成的:com.example.package-info
了解更多信息
You can setup a default namespace by leveraging the
@XmlSchema
annotation at the package level. This is done by leveraging apackage-info
class (example below):com.example.package-info
For More Information