1

I have my own complexType in schema that should look in XML something like this:

<dynamic-content language-id="en_US"><!--[CDATA[256]]--></dynamic-content>

Therefore element containing some text always wrapped in CDATA and having attribute. This is what I've done so far in article-content.xsd:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ibacz.eu/veraext/xsd"
xmlns:tns="http://www.ibacz.eu/veraext/xsd" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:complexType name="cdata">
        <xs:attribute name="language-id" type="xs:string" />
    </xs:complexType>

    <xs:element name="root"> 
        <xs:complexType>
            <xs:sequence>
                <xs:element name="dynamic-element" maxOccurs="unbounded">
                  <xs:complexType>
                    <xs:sequence>
                        <xs:element name="dynamic-content" type="tns:cdata" />
                    </xs:sequence>
                    <xs:attribute name="name" type="xs:string" />
                    <xs:attribute name="index" type="xs:integer" />
                    <xs:attribute name="type" type="xs:string" />
                    <xs:attribute name="index-type" type="xs:string" />
                  </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="available-locales" type="xs:string" />
            <xs:attribute name="default-locale" type="xs:string" />
        </xs:complexType>
    </xs:element>
</xs:schema>

and bindings.xsd:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
           version="2.1">

    <jaxb:bindings schemaLocation="article-content.xsd" node="/xs:schema">
        <jaxb:bindings node="xs:complexType[@name='cdata']">
            <xjc:javaType name="String" adapter="eu.ibacz.veraext.backofficebridge.core.util.AdapterCDATA"/>
        </jaxb:bindings>
    </jaxb:bindings> 

</jaxb:bindings>

What I can't get rid of is compiler was unable to honor this conversion customization. It is attached to a wrong place, or its inconsistent with other bindings.

Please what should I do? I'm fighting with this problem for hours and it is a real blocker for me.

Martin D
  • 667
  • 1
  • 10
  • 25
  • What is the purpose of the adapater? Which classes are you using as generic parameters with `XmlAdapter`? – laune Aug 08 '14 at 05:08
  • @laune - the purpose is to wrap/unwrap String with CDATA tags, I'm using . – Martin D Aug 11 '14 at 06:09
  • This isn't necessary. The underlying XML writer takes care of rendering any content in a way according to XML rules. If everything is escaped properly, CDATA tags aren't necessary. Or, looking at it the other way, there's no way of forcing CDATA tags, which are merely a convenience for entering content using a plain old text editor. – laune Aug 11 '14 at 14:08
  • @laune - but I'm solving a bit different problem: I'm working on one side with standardized data (where I can be 100% sure there'll be CDATA tags) and on the other with imports that can't (and almost for sure won't be) escaped. – Martin D Aug 11 '14 at 20:22
  • 1
    I think you don't understand the rules in connection with CDATA tags, JAXB and marshalling. When you store a String into some property of a Java class, it contains char values, and they'll be written to the output using correct escaping and encoding, not matter the character or the encoding. If you try to be clever and store a String value enclosed in CDATA tags, *they* will be escaped, as well as other characters between these tags. There is absolutely no way you can change that - except by writing an XML file using println: then you're on you own. – laune Aug 12 '14 at 08:16
  • @laune - Thank you! You solved my problem by figuring out there is none. :D I really didn't thought that JAXB would escape all "invalid" characters. Therefore, I don't need my CDATA-custom-type binding. – Martin D Aug 22 '14 at 18:50

1 Answers1

0

As a partial answer you need to fix your cdata type as follows so you can have text content:

    <complexType name="cdata">
        <simpleContent>
            <extension base="string">
                <attribute name="language" type="string"/>
            </extension>
        </simpleContent>
    </complexType>

Off hand I'm not sure which node you need to target in the binding file, but you could try simpleContent.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks for reply but when I change my type as you suggest, I get `Unexpected appears at line 7 column 24.` – Martin D Aug 07 '14 at 14:13
  • @MartinDulak - You will need to add the `xs` prefix to match the namespace qualification in your XML Schema. – bdoughan Aug 07 '14 at 14:18
  • This is how you can see that I'm too much tired of solving this. :) OK, now when I change bindings to `node="xs:complexType[@name='cdata']/xs:simpleContent"` I get ` customization in this context must be nested (JAXB spec sec 6.8.1)`. – Martin D Aug 07 '14 at 14:26
  • Probably the entire question + answer should be removed (due to OPs final statement below the Q). – laune Aug 23 '14 at 06:08