3

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?

Daniel Glauser
  • 181
  • 2
  • 8

2 Answers2

2

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;

For More Information

bdoughan
  • 147,609
  • 23
  • 300
  • 400
0

there are a couple of ways to attack this problem. it might be as simple as this, or you may need to take complete control of the namespace prefixes. (btw, that second xml is not "incorrect", it's just using a default namespace. it would be incorrect, however, for a system to require a specific prefix for the xml).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118