Jaxb:通过xmlns属性而不是元素前缀的本地namspacing? [英] Jaxb: local namspacing via the xmlns attribute instead of the element prefix?

查看:178
本文介绍了Jaxb:通过xmlns属性而不是元素前缀的本地namspacing?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jaxb生成一些看起来非常好的XML。这是一个片段:

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>

这是符合标准的XML。不幸的是,我受限于下游系统的限制,它只接受以这种方式格式化的XML:

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>

其中某些元素通过xmlns属性命名空间。
现在问题,如何让Jaxb停止(做正确的事)使用前缀命名空间元素并使用xmlns属性开始命名元素?

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 类(下面的示例)来完成的:

You can setup a default namespace by leveraging the @XmlSchema annotation at the package level. This is done by leveraging a package-info class (example below):

com.example。 package-info

@XmlSchema(
    namespace = "http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#esx",
    elementFormDefault = XmlNsForm.QUALIFIED)
package com.example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

更多信息

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

这篇关于Jaxb:通过xmlns属性而不是元素前缀的本地namspacing?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆