braincell
braincell

Reputation: 562

JAXB xml tag in file starting with number, possible?

I have a very big xml with many nested tags for which I generated a java class.

One of the tags starts with number <3DSecure></3DSecure>

I had to manually set this tag only, in Java I mapped to threeDSecure.

I know this is against XML Conventions but is it possible to override this check? Otherwise I will have to drop JAXB and setup the xml manually because I don't control the API which expects this XML.

When unmarshalling/marshalling I get the error:

[org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:505)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:206)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169)

Upvotes: 1

Views: 497

Answers (2)

bdoughan
bdoughan

Reputation: 149007

You can use the -nv flag, do disable validation of the XML schema when generating classes from an XML schema.

XJC Call

xjc -nv schema.xsd 

XML Schema (schema.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/schema"
    xmlns:tns="http://www.example.org/schema" 
    elementFormDefault="qualified">

    <complexType name="foo">
       <sequence>
           <element name="3DSecure" type="string"/>
       </sequence>
    </complexType>

</schema>

Generated Class (Foo)

package org.example.schema;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo", propOrder = {"_3DSecure"})
public class Foo {

    @XmlElement(name = "3DSecure", required = true)
    protected String _3DSecure;

    public String get3DSecure() {
        return _3DSecure;
    }

    public void set3DSecure(String value) {
        this._3DSecure = value;
    }

}

Upvotes: 1

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Bind your Java class property using @XmlElement annotation's name attribute as

 @XmlRootElement
 public class JAXBModel {

     @XmlElement(name="3DSecure")
     public String threeDSecure;

     // ...
 }

Upvotes: 0

Related Questions