M
M
MaxLich2018-11-29 17:40:44
Java
MaxLich, 2018-11-29 17:40:44

How do I get JAXB to properly serialize a dynamically inflated nested object?

Hello. There is such an element in the scheme:

spoiler
<xs:element name="CustomAttributes" minOccurs="0">
        <xs:annotation>
          <xs:documentation>Дополнительные атрибуты документа</xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:any processContents="lax"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>

And there are schemas for elements that can be nested in CustomAttributes:
spoiler
<xs:element name="ServiceProperties">
        <xs:annotation>
            <xs:documentation xml:lang="ru">МЖИ:Лицензирование предпринимательской деятельност</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:all>
                <xs:element name="adress_house" minOccurs="0" type="xs:string">
                    <xs:annotation>
                        <xs:documentation xml:lang="ru">Адрес дома</xs:documentation>
                    </xs:annotation>
                </xs:element>
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>

The following classes were generated:
spoiler
@XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "CustomAttributes", propOrder = {
        "any"
    })
    public static class CustomAttributes {

        @XmlAnyElement(lax = true)
        protected Object any;

        /**
         * Gets the value of the any property.
         * 
         * @return
         *     possible object is
         *     {@link Element }
         *     {@link Object }
         *     
         */
        public Object getAny() {
            return any;
        }

        /**
         * Sets the value of the any property.
         * 
         * @param value
         *     allowed object is
         *     {@link Element }
         *     {@link Object }
         *     
         */
        public void setAny(Object value) {
            this.any = value;
        }

    }
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {

})
@XmlRootElement(name = "ServiceProperties")
public class ServiceProperties {

    @XmlElement(name = "adress_house")
    protected String adressHouse;

    /**
     * Gets the value of the adressHouse property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getAdressHouse() {
        return adressHouse;
    }

    /**
     * Sets the value of the adressHouse property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setAdressHouse(String value) {
        this.adressHouse = value;
    }
}

It is necessary to get the xml of the form:
<CustomAttributes>
                <ServiceProperties>
                    <adressHouse >123</adressHouse >
                </ServiceProperties>
            </CustomAttributes>

My code:
ServiceProperties serviceProperties = new ServiceProperties();
             serviceProperties.setAdressHouse("123");
            customAttributes.setAny(serviceProperties );

Does not work. It simply calls toString()( ) on that object serviceProperties.
Tried through JAXBElement, but also does not help. Is there any normal solution?
(So ​​far, the idea is only to serialize separately serviceProperties, and then throw it into the method setAny(); but this is some kind of crutch solution)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxLich, 2018-11-29
@MaxLich

Everything is working. It turned out that when creating the context, it was necessary to register the package in which lies ServiceProperties:
Also, in order to avoid problems with namespaces, I transferred a file package-info.javafrom the main package to this package. The file has the following content:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://firma.ru/schema/v6_1/", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ru.my-firm.app.my-project.jaxbobjects;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question