JAXB-Return the entire XML object even if i don't have values Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Represent null value as empty element in xml jaxbJAXB empty element unmarshallingASMX WebServices issueRead SOAP XML by ASMX web servcie functionCreate an XSD to handle abstract typesJAXB namespace issue with extended annotated class' memberC#+Magento API V2:The content type text/xml; charset=utf-8,text/xml; charset=UTF-8 of the response message does not matchSOAP response/Request then how to convert it in to PHPHow to remove xmlns:xsi and xsi:type from JAXB marshalled XML fileNamespace issue with pysaml2 XML canonicalizationJava Generate Soap Envelop Message With Same Element Name With Different TypeDatastage Rest call in Hierarchical step

Do square wave exist?

Is there such thing as an Availability Group failover trigger?

Why wasn't DOSKEY integrated with COMMAND.COM?

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

Significance of Cersei's obsession with elephants?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Fantasy story; one type of magic grows in power with use, but the more powerful they are, they more they are drawn to travel to their source

What is homebrew?

Is "Reachable Object" really an NP-complete problem?

What does the "x" in "x86" represent?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

Can anything be seen from the center of the Boötes void? How dark would it be?

How to convince students of the implication truth values?

How to tell that you are a giant?

What's the meaning of "fortified infraction restraint"?

How does the math work when buying airline miles?

Is grep documentation wrong?

What do you call a floor made of glass so you can see through the floor?

Using audio cues to encourage good posture

Is it a good idea to use CNN to classify 1D signal?

Compare a given version number in the form major.minor.build.patch and see if one is less than the other

Is there any way for the UK Prime Minister to make a motion directly dependent on Government confidence?

Do I really need to have a message in a novel to appeal to readers?

Why do we bend a book to keep it straight?



JAXB-Return the entire XML object even if i don't have values



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Represent null value as empty element in xml jaxbJAXB empty element unmarshallingASMX WebServices issueRead SOAP XML by ASMX web servcie functionCreate an XSD to handle abstract typesJAXB namespace issue with extended annotated class' memberC#+Magento API V2:The content type text/xml; charset=utf-8,text/xml; charset=UTF-8 of the response message does not matchSOAP response/Request then how to convert it in to PHPHow to remove xmlns:xsi and xsi:type from JAXB marshalled XML fileNamespace issue with pysaml2 XML canonicalizationJava Generate Soap Envelop Message With Same Element Name With Different TypeDatastage Rest call in Hierarchical step



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm trying to return an xml response with JAXB and the implementation looks like in the example bellow. My question is: There is a way to return the whole xml without setting a value on the field?



 @ResponsePayload
public JAXBElement<Ns2AnfrageBonitaetsauskunftAntwortType> getResponse(@RequestPayload JAXBElement<Ns2AnfrageBonitaetsauskunftTyp> request)

Ns2AnfrageBonitaetsauskunftAntwortType response = new Ns2AnfrageBonitaetsauskunftAntwortType();
response.setSchufaReferenz("test2");
response.setTeilnehmerreferenz("test1");
response.setAktionsdaten("test3");
Ns3BonitaetsauskunftType bonita = new Ns3BonitaetsauskunftType();
bonita.setTeilnehmerkennung("test4");

Ns3VerarbeitungsinformationType verar = new Ns3VerarbeitungsinformationType();
verar.setErgebnistyp("test7");
bonita.setVerarbeitungsinformation(verar);

Ns3VerbraucherdatenAuskunftType daten = new Ns3VerbraucherdatenAuskunftType();
daten.setPersonOhneGeburtsdatum("test6");
bonita.setVerbraucherdaten(daten);
response.setReaktion(bonita);
ObjectFactory objectFactory = new ObjectFactory();
return objectFactory.createBonitaetsauskunft(response);



At the moment the response look like this:



<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<ns3:Bonitaetsauskunft xmlns:ns3="http://ifd-schema.de/BonitaetsauskunftSCHUFA">
<SchufaReferenz>test2</SchufaReferenz>
<Teilnehmerreferenz>test1</Teilnehmerreferenz>
<Aktionsdaten xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">test3</Aktionsdaten>
<Reaktion>
<Teilnehmerkennung>test4</Teilnehmerkennung>
<Verbraucherdaten>
<PersonOhneGeburtsdatum>test6</PersonOhneGeburtsdatum>
</Verbraucherdaten>
<Verarbeitungsinformation>
<Ergebnistyp>test7</Ergebnistyp>
</Verarbeitungsinformation>
</Reaktion>
</ns3:Bonitaetsauskunft>
</env:Body>
</env:Envelope>


I have a lot of more field on which i don't want to set a value but i want them to be in the response. Any ideas?



Thank you










share|improve this question
























  • Having XmlAddapter for each relevant type. The question is, for example for String, how do you then distinguish between an empty String and a null-String? Anyway look at stackoverflow.com/questions/18921196/…

    – Michal
    Mar 22 at 11:12


















0















I'm trying to return an xml response with JAXB and the implementation looks like in the example bellow. My question is: There is a way to return the whole xml without setting a value on the field?



 @ResponsePayload
public JAXBElement<Ns2AnfrageBonitaetsauskunftAntwortType> getResponse(@RequestPayload JAXBElement<Ns2AnfrageBonitaetsauskunftTyp> request)

Ns2AnfrageBonitaetsauskunftAntwortType response = new Ns2AnfrageBonitaetsauskunftAntwortType();
response.setSchufaReferenz("test2");
response.setTeilnehmerreferenz("test1");
response.setAktionsdaten("test3");
Ns3BonitaetsauskunftType bonita = new Ns3BonitaetsauskunftType();
bonita.setTeilnehmerkennung("test4");

Ns3VerarbeitungsinformationType verar = new Ns3VerarbeitungsinformationType();
verar.setErgebnistyp("test7");
bonita.setVerarbeitungsinformation(verar);

Ns3VerbraucherdatenAuskunftType daten = new Ns3VerbraucherdatenAuskunftType();
daten.setPersonOhneGeburtsdatum("test6");
bonita.setVerbraucherdaten(daten);
response.setReaktion(bonita);
ObjectFactory objectFactory = new ObjectFactory();
return objectFactory.createBonitaetsauskunft(response);



At the moment the response look like this:



<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<ns3:Bonitaetsauskunft xmlns:ns3="http://ifd-schema.de/BonitaetsauskunftSCHUFA">
<SchufaReferenz>test2</SchufaReferenz>
<Teilnehmerreferenz>test1</Teilnehmerreferenz>
<Aktionsdaten xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">test3</Aktionsdaten>
<Reaktion>
<Teilnehmerkennung>test4</Teilnehmerkennung>
<Verbraucherdaten>
<PersonOhneGeburtsdatum>test6</PersonOhneGeburtsdatum>
</Verbraucherdaten>
<Verarbeitungsinformation>
<Ergebnistyp>test7</Ergebnistyp>
</Verarbeitungsinformation>
</Reaktion>
</ns3:Bonitaetsauskunft>
</env:Body>
</env:Envelope>


I have a lot of more field on which i don't want to set a value but i want them to be in the response. Any ideas?



Thank you










share|improve this question
























  • Having XmlAddapter for each relevant type. The question is, for example for String, how do you then distinguish between an empty String and a null-String? Anyway look at stackoverflow.com/questions/18921196/…

    – Michal
    Mar 22 at 11:12














0












0








0








I'm trying to return an xml response with JAXB and the implementation looks like in the example bellow. My question is: There is a way to return the whole xml without setting a value on the field?



 @ResponsePayload
public JAXBElement<Ns2AnfrageBonitaetsauskunftAntwortType> getResponse(@RequestPayload JAXBElement<Ns2AnfrageBonitaetsauskunftTyp> request)

Ns2AnfrageBonitaetsauskunftAntwortType response = new Ns2AnfrageBonitaetsauskunftAntwortType();
response.setSchufaReferenz("test2");
response.setTeilnehmerreferenz("test1");
response.setAktionsdaten("test3");
Ns3BonitaetsauskunftType bonita = new Ns3BonitaetsauskunftType();
bonita.setTeilnehmerkennung("test4");

Ns3VerarbeitungsinformationType verar = new Ns3VerarbeitungsinformationType();
verar.setErgebnistyp("test7");
bonita.setVerarbeitungsinformation(verar);

Ns3VerbraucherdatenAuskunftType daten = new Ns3VerbraucherdatenAuskunftType();
daten.setPersonOhneGeburtsdatum("test6");
bonita.setVerbraucherdaten(daten);
response.setReaktion(bonita);
ObjectFactory objectFactory = new ObjectFactory();
return objectFactory.createBonitaetsauskunft(response);



At the moment the response look like this:



<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<ns3:Bonitaetsauskunft xmlns:ns3="http://ifd-schema.de/BonitaetsauskunftSCHUFA">
<SchufaReferenz>test2</SchufaReferenz>
<Teilnehmerreferenz>test1</Teilnehmerreferenz>
<Aktionsdaten xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">test3</Aktionsdaten>
<Reaktion>
<Teilnehmerkennung>test4</Teilnehmerkennung>
<Verbraucherdaten>
<PersonOhneGeburtsdatum>test6</PersonOhneGeburtsdatum>
</Verbraucherdaten>
<Verarbeitungsinformation>
<Ergebnistyp>test7</Ergebnistyp>
</Verarbeitungsinformation>
</Reaktion>
</ns3:Bonitaetsauskunft>
</env:Body>
</env:Envelope>


I have a lot of more field on which i don't want to set a value but i want them to be in the response. Any ideas?



Thank you










share|improve this question
















I'm trying to return an xml response with JAXB and the implementation looks like in the example bellow. My question is: There is a way to return the whole xml without setting a value on the field?



 @ResponsePayload
public JAXBElement<Ns2AnfrageBonitaetsauskunftAntwortType> getResponse(@RequestPayload JAXBElement<Ns2AnfrageBonitaetsauskunftTyp> request)

Ns2AnfrageBonitaetsauskunftAntwortType response = new Ns2AnfrageBonitaetsauskunftAntwortType();
response.setSchufaReferenz("test2");
response.setTeilnehmerreferenz("test1");
response.setAktionsdaten("test3");
Ns3BonitaetsauskunftType bonita = new Ns3BonitaetsauskunftType();
bonita.setTeilnehmerkennung("test4");

Ns3VerarbeitungsinformationType verar = new Ns3VerarbeitungsinformationType();
verar.setErgebnistyp("test7");
bonita.setVerarbeitungsinformation(verar);

Ns3VerbraucherdatenAuskunftType daten = new Ns3VerbraucherdatenAuskunftType();
daten.setPersonOhneGeburtsdatum("test6");
bonita.setVerbraucherdaten(daten);
response.setReaktion(bonita);
ObjectFactory objectFactory = new ObjectFactory();
return objectFactory.createBonitaetsauskunft(response);



At the moment the response look like this:



<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<ns3:Bonitaetsauskunft xmlns:ns3="http://ifd-schema.de/BonitaetsauskunftSCHUFA">
<SchufaReferenz>test2</SchufaReferenz>
<Teilnehmerreferenz>test1</Teilnehmerreferenz>
<Aktionsdaten xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">test3</Aktionsdaten>
<Reaktion>
<Teilnehmerkennung>test4</Teilnehmerkennung>
<Verbraucherdaten>
<PersonOhneGeburtsdatum>test6</PersonOhneGeburtsdatum>
</Verbraucherdaten>
<Verarbeitungsinformation>
<Ergebnistyp>test7</Ergebnistyp>
</Verarbeitungsinformation>
</Reaktion>
</ns3:Bonitaetsauskunft>
</env:Body>
</env:Envelope>


I have a lot of more field on which i don't want to set a value but i want them to be in the response. Any ideas?



Thank you







java xml soap jaxb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 10:30









Thomas Fritsch

5,566122236




5,566122236










asked Mar 22 at 9:38









AlexandraAlexandra

113




113












  • Having XmlAddapter for each relevant type. The question is, for example for String, how do you then distinguish between an empty String and a null-String? Anyway look at stackoverflow.com/questions/18921196/…

    – Michal
    Mar 22 at 11:12


















  • Having XmlAddapter for each relevant type. The question is, for example for String, how do you then distinguish between an empty String and a null-String? Anyway look at stackoverflow.com/questions/18921196/…

    – Michal
    Mar 22 at 11:12

















Having XmlAddapter for each relevant type. The question is, for example for String, how do you then distinguish between an empty String and a null-String? Anyway look at stackoverflow.com/questions/18921196/…

– Michal
Mar 22 at 11:12






Having XmlAddapter for each relevant type. The question is, for example for String, how do you then distinguish between an empty String and a null-String? Anyway look at stackoverflow.com/questions/18921196/…

– Michal
Mar 22 at 11:12













1 Answer
1






active

oldest

votes


















0














You can add to each property nillable attribut using @XmlElement(nillable = true) annotation. From documentation:




Optional. Specifies whether an explicit null value can be assigned to
the element. True enables an instance of the element to have the null
attribute set to true. The null attribute is defined as part of the
XML Schema namespace for instances. Default is false




Fields with this attribute are print as <value xsi:nil="true"/>. Small example:



import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class JaxbApp

public static void main(String[] args) throws Exception
Root dataFile = new Root();
dataFile.setPerson(new Person());

JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);

marshaller.marshal(dataFile, System.out);



@XmlRootElement(name = "roo")
@XmlAccessorType(XmlAccessType.FIELD)
class Root

@XmlElement(nillable = true)
private Integer id;

@XmlElement(nillable = true)
private String value;

@XmlElement(nillable = true)
private Person person;

// getters, setters, toString


@XmlAccessorType(XmlAccessType.FIELD)
class Person

@XmlElement(nillable = true)
private String name;

@XmlElement(nillable = true)
private String lastName;

// getters, setters, toString



prints:



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<roo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
<id xsi:nil="true"/>
<value xsi:nil="true"/>
<person>
<name xsi:nil="true"/>
<lastName xsi:nil="true"/>
</person>
</roo>


In that case you need to change your XSD/WSDL document and add this attribute to each primitive field. Of course, you still need to create new POJO instance like new Person() in above example. You can of course set each property in class to some kind of default value private String value = ""; or write reflection tool which travers all properties and set to some custom values.



See also:



  • Represent null value as empty element in xml jaxb





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296689%2fjaxb-return-the-entire-xml-object-even-if-i-dont-have-values%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can add to each property nillable attribut using @XmlElement(nillable = true) annotation. From documentation:




    Optional. Specifies whether an explicit null value can be assigned to
    the element. True enables an instance of the element to have the null
    attribute set to true. The null attribute is defined as part of the
    XML Schema namespace for instances. Default is false




    Fields with this attribute are print as <value xsi:nil="true"/>. Small example:



    import javax.xml.XMLConstants;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;

    public class JaxbApp

    public static void main(String[] args) throws Exception
    Root dataFile = new Root();
    dataFile.setPerson(new Person());

    JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);

    marshaller.marshal(dataFile, System.out);



    @XmlRootElement(name = "roo")
    @XmlAccessorType(XmlAccessType.FIELD)
    class Root

    @XmlElement(nillable = true)
    private Integer id;

    @XmlElement(nillable = true)
    private String value;

    @XmlElement(nillable = true)
    private Person person;

    // getters, setters, toString


    @XmlAccessorType(XmlAccessType.FIELD)
    class Person

    @XmlElement(nillable = true)
    private String name;

    @XmlElement(nillable = true)
    private String lastName;

    // getters, setters, toString



    prints:



    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <roo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
    <id xsi:nil="true"/>
    <value xsi:nil="true"/>
    <person>
    <name xsi:nil="true"/>
    <lastName xsi:nil="true"/>
    </person>
    </roo>


    In that case you need to change your XSD/WSDL document and add this attribute to each primitive field. Of course, you still need to create new POJO instance like new Person() in above example. You can of course set each property in class to some kind of default value private String value = ""; or write reflection tool which travers all properties and set to some custom values.



    See also:



    • Represent null value as empty element in xml jaxb





    share|improve this answer





























      0














      You can add to each property nillable attribut using @XmlElement(nillable = true) annotation. From documentation:




      Optional. Specifies whether an explicit null value can be assigned to
      the element. True enables an instance of the element to have the null
      attribute set to true. The null attribute is defined as part of the
      XML Schema namespace for instances. Default is false




      Fields with this attribute are print as <value xsi:nil="true"/>. Small example:



      import javax.xml.XMLConstants;
      import javax.xml.bind.JAXBContext;
      import javax.xml.bind.Marshaller;
      import javax.xml.bind.annotation.XmlAccessType;
      import javax.xml.bind.annotation.XmlAccessorType;
      import javax.xml.bind.annotation.XmlElement;
      import javax.xml.bind.annotation.XmlRootElement;

      public class JaxbApp

      public static void main(String[] args) throws Exception
      Root dataFile = new Root();
      dataFile.setPerson(new Person());

      JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
      Marshaller marshaller = jaxbContext.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);

      marshaller.marshal(dataFile, System.out);



      @XmlRootElement(name = "roo")
      @XmlAccessorType(XmlAccessType.FIELD)
      class Root

      @XmlElement(nillable = true)
      private Integer id;

      @XmlElement(nillable = true)
      private String value;

      @XmlElement(nillable = true)
      private Person person;

      // getters, setters, toString


      @XmlAccessorType(XmlAccessType.FIELD)
      class Person

      @XmlElement(nillable = true)
      private String name;

      @XmlElement(nillable = true)
      private String lastName;

      // getters, setters, toString



      prints:



      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <roo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
      <id xsi:nil="true"/>
      <value xsi:nil="true"/>
      <person>
      <name xsi:nil="true"/>
      <lastName xsi:nil="true"/>
      </person>
      </roo>


      In that case you need to change your XSD/WSDL document and add this attribute to each primitive field. Of course, you still need to create new POJO instance like new Person() in above example. You can of course set each property in class to some kind of default value private String value = ""; or write reflection tool which travers all properties and set to some custom values.



      See also:



      • Represent null value as empty element in xml jaxb





      share|improve this answer



























        0












        0








        0







        You can add to each property nillable attribut using @XmlElement(nillable = true) annotation. From documentation:




        Optional. Specifies whether an explicit null value can be assigned to
        the element. True enables an instance of the element to have the null
        attribute set to true. The null attribute is defined as part of the
        XML Schema namespace for instances. Default is false




        Fields with this attribute are print as <value xsi:nil="true"/>. Small example:



        import javax.xml.XMLConstants;
        import javax.xml.bind.JAXBContext;
        import javax.xml.bind.Marshaller;
        import javax.xml.bind.annotation.XmlAccessType;
        import javax.xml.bind.annotation.XmlAccessorType;
        import javax.xml.bind.annotation.XmlElement;
        import javax.xml.bind.annotation.XmlRootElement;

        public class JaxbApp

        public static void main(String[] args) throws Exception
        Root dataFile = new Root();
        dataFile.setPerson(new Person());

        JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);

        marshaller.marshal(dataFile, System.out);



        @XmlRootElement(name = "roo")
        @XmlAccessorType(XmlAccessType.FIELD)
        class Root

        @XmlElement(nillable = true)
        private Integer id;

        @XmlElement(nillable = true)
        private String value;

        @XmlElement(nillable = true)
        private Person person;

        // getters, setters, toString


        @XmlAccessorType(XmlAccessType.FIELD)
        class Person

        @XmlElement(nillable = true)
        private String name;

        @XmlElement(nillable = true)
        private String lastName;

        // getters, setters, toString



        prints:



        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <roo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
        <id xsi:nil="true"/>
        <value xsi:nil="true"/>
        <person>
        <name xsi:nil="true"/>
        <lastName xsi:nil="true"/>
        </person>
        </roo>


        In that case you need to change your XSD/WSDL document and add this attribute to each primitive field. Of course, you still need to create new POJO instance like new Person() in above example. You can of course set each property in class to some kind of default value private String value = ""; or write reflection tool which travers all properties and set to some custom values.



        See also:



        • Represent null value as empty element in xml jaxb





        share|improve this answer















        You can add to each property nillable attribut using @XmlElement(nillable = true) annotation. From documentation:




        Optional. Specifies whether an explicit null value can be assigned to
        the element. True enables an instance of the element to have the null
        attribute set to true. The null attribute is defined as part of the
        XML Schema namespace for instances. Default is false




        Fields with this attribute are print as <value xsi:nil="true"/>. Small example:



        import javax.xml.XMLConstants;
        import javax.xml.bind.JAXBContext;
        import javax.xml.bind.Marshaller;
        import javax.xml.bind.annotation.XmlAccessType;
        import javax.xml.bind.annotation.XmlAccessorType;
        import javax.xml.bind.annotation.XmlElement;
        import javax.xml.bind.annotation.XmlRootElement;

        public class JaxbApp

        public static void main(String[] args) throws Exception
        Root dataFile = new Root();
        dataFile.setPerson(new Person());

        JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);

        marshaller.marshal(dataFile, System.out);



        @XmlRootElement(name = "roo")
        @XmlAccessorType(XmlAccessType.FIELD)
        class Root

        @XmlElement(nillable = true)
        private Integer id;

        @XmlElement(nillable = true)
        private String value;

        @XmlElement(nillable = true)
        private Person person;

        // getters, setters, toString


        @XmlAccessorType(XmlAccessType.FIELD)
        class Person

        @XmlElement(nillable = true)
        private String name;

        @XmlElement(nillable = true)
        private String lastName;

        // getters, setters, toString



        prints:



        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <roo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
        <id xsi:nil="true"/>
        <value xsi:nil="true"/>
        <person>
        <name xsi:nil="true"/>
        <lastName xsi:nil="true"/>
        </person>
        </roo>


        In that case you need to change your XSD/WSDL document and add this attribute to each primitive field. Of course, you still need to create new POJO instance like new Person() in above example. You can of course set each property in class to some kind of default value private String value = ""; or write reflection tool which travers all properties and set to some custom values.



        See also:



        • Represent null value as empty element in xml jaxb






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 23 at 18:13

























        answered Mar 23 at 17:11









        Michał ZioberMichał Ziober

        17.8k1271109




        17.8k1271109





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296689%2fjaxb-return-the-entire-xml-object-even-if-i-dont-have-values%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현