Getting XML data in child node as string using JAXB

I am using JAXB to convert XML (parent XML, FIXML) to String. In this XML, one child node has another XML (child XML, FpML) data for which I had a schema (xsd). In the schema for parent XML, the type of element that has child XML is defined as String. I need child XML code that should appear as a string. Please let me know what change I need to make. I am relatively new to JAXB. Thanks at Advance .. !!

Cheers, Sakthi S

+3
source share
2 answers

UPDATE

Based on your comments:

.. .. @Blaise Doughan @CoolBeans , , , xml "String Value" XML . "" . , , . .. , . S

@XmlAnyElement DomHandler . :


# 2

, ? , "@XmlAnyElement", java .

JAXB dom, XmlAnyElement :

XJC

xjc -d out -b bindings.xml dom.xsd

dom.xsd

<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.example.com/default"
    targetNamespace="http://www.example.com/default">

    <xs:element name="customer">
        <xs:complexType>
        <xs:sequence>
            <xs:element ref="address"/>
        </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="address">
        <xs:complexType>
        <xs:attribute name="street" type="xs:string"/>
        </xs:complexType>
    </xs:element>

</xs:schema>

bindings.xml

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

    <jxb:bindings schemaLocation="string.xsd">
            <jxb:bindings node="//xs:element[@name='customer']/xs:complexType/xs:sequence/xs:element[@ref='address']">
                <jxb:dom/>
            </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>

Customer @XmlAnyElement:

package com.example._default;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.w3c.dom.Element;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "address"
})
@XmlRootElement(name = "customer")
public class Customer {

    @XmlAnyElement
    protected Element address;

    public Element getAddress() {
        return address;
    }

    public void setAddress(Element value) {
        this.address = value;
    }

}

, @XmlValue. , :

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD
public class Child {

    @XmlValue
    private String value;

}

:

<child>String Value</child>

XML- xs: string.

<xs:element name="child" type="xs:string"/>
+3

@Column(name="xml")
@XmlJavaTypeAdapter(StringXmlAdapter.class)
public String getXml() {
    return this.xml;
}

..

public class StringXmlAdapter extends XmlAdapter<Object, String>
{
   private static final String XMLSTART = "<?xml version='1.0' encoding='UTF-8'?>";

    @Override
    public Object marshal(String v) throws Exception {

        return v;
    }

    @Override
    public String unmarshal(Object v) throws Exception {
        Document document = ((Node) v).getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document
            .getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        String str = serializer.writeToString((Node) v);
        String normalizedXml = normalizeXml(str);
        return normalizedXml;
    }

    private String normalizeXml(String xml){
        int beginIndex = xml.indexOf('>',XMLSTART.length()+1)+1;
        int endIndex = xml.lastIndexOf('<');
        return xml.substring(beginIndex, endIndex);
    }
}

,

<xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">
<inner><something>lalala</something></inner></xml>

, xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xs = "http://www.w3.org/2001/XMLSchema" XSI: = ": "

+1

All Articles