xjc binding compiler configuration to add xmlns element to the package-info class?

眉间皱痕 提交于 2019-12-11 02:35:34

问题


I am using Gradle to generate jaxb classes in my project. Every thing is working fine but while marshalling the jaxb object we are seeing different namespaces prefixes like ns1, ns2 .. randomly in the output xml. But we dont want this and want to specify specific namespace prefixes for each namespace. I checked here and found the link 15772478 saying we have to have package-info class with xmlns element, How can i say to xjc binding compiler to add xmlns element with prifixes and namespaceURI? below is the gradle configuration i have to generate Jaxb classes from schemas.

  ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask',  classpath:configurations.jaxb.asPath)
  ant.jaxbTargetDir = jaxbTargetDir

  ant.xjc(destdir: '${jaxbTargetDir}', binding: 'xjc-bindings/bindings.jaxb', extension: true) {
  //arg(value: '-npa')
  arg(value: '-readOnly')
  arg(value: file('src/main/webapp/schemas/primary1.xsd'))
  arg(value: file('src/main/webapp/schemas/primary2.xsd'))
  arg(value: file('xjc-bindings/xjc-a.xsd'))
  arg(value: file('xjc-bindings/xjc-b.xsd'))
 }

sample package-info.java generated by xjc binding.

@XmlSchema(namespace = "urn:neustar:names:decedm:1.0")
package biz.neustar.dece.xml.jaxb.decedm;
import javax.xml.bind.annotation.XmlSchema;

I am expecting the package-info class like below.

@XmlSchema(namespace = "<someuri>", 
 elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
 xmlns={
      @XmlNs(prefix="someprefix" , namespaceURI = "<some uri>")
 })
 package biz.neustar.dece.xml.jaxb.core;
 import javax.xml.bind.annotation.XmlNs;
 import javax.xml.bind.annotation.XmlSchema;

Can somebody please suggest me what is the configuration need to achieve this? I don't want to use NamespacePrefixMapper to specify the prefixes.


回答1:


You need to update you binding file like following. It will use eCH-0007 as prefix.

<?xml version="1.0"?>
<jxb:bindings version="1.0"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix"
              xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd
              http://jaxb2-commons.dev.java.net/namespace-prefix http://java.net/projects/jaxb2-commons/sources/svn/content/namespace-prefix/trunk/src/main/resources/prefix-namespace-schema.xsd">

    <jxb:bindings schemaLocation="eCH-0007-3-0.xsd">
        <jxb:schemaBindings>
            <jxb:package name="ch.ech.ech0007.v3" />
        </jxb:schemaBindings>
        <jxb:bindings>
            <namespace:prefix name="eCH-0007" />
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

You can check complete example on this link [https://java.net/projects/jaxb2-commons/pages/Namespace-prefix]



来源:https://stackoverflow.com/questions/27565031/xjc-binding-compiler-configuration-to-add-xmlns-element-to-the-package-info-clas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!