Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 996
Lovababu Avatar asked Nov 20 '25 23:11

Lovababu


1 Answers

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 Namespace-prefix.

like image 93
Zubair Ashraf Avatar answered Nov 23 '25 17:11

Zubair Ashraf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!