Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to remove not required Elements from generated XML via jaxb
    primarykey
    data
    text
    <p>I want to know if there is anyway for removing not required elements from generated xml using jaxb.I have my xsd element definition as follows.</p> <pre><code> &lt;xsd:element name="Title" maxOccurs="1" minOccurs="0"&gt; &lt;xsd:annotation&gt; &lt;xsd:documentation&gt; A name given to the digital record. &lt;/xsd:documentation&gt; &lt;/xsd:annotation&gt; &lt;xsd:simpleType&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:minLength value="1"&gt;&lt;/xsd:minLength&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:element&gt; </code></pre> <p>As you can see it is not a mandatory element because</p> <blockquote> <p>minOccurs="0"</p> </blockquote> <p>But if it is not empty the length should be 1.</p> <pre><code>&lt;xsd:minLength value="1"&gt;&lt;/xsd:minLength&gt; </code></pre> <p>At the time of marshalling if I left the Title field blank it is throwing the <strong>SAXException</strong> because of min-length restriction. So what I want to do is to remove the whole occurrence of <code>&lt;Title/&gt;</code> from generated XML.Right now i have removed the min-length restriction so it is adding the <code>&lt;Title&gt;</code> element as <strong>EMPTY</strong></p> <pre><code>&lt;Title&gt;&lt;/Title&gt; </code></pre> <p>But I do not want it like this.Any help is appreciated.I am using jaxb 2.0 for Marshalling.</p> <p><strong>UPDATE:</strong></p> <p>Following is my variable definiton : </p> <pre><code> private JAXBContext jaxbContext; private Unmarshaller unmarshaller; private SchemaFactory factory; private Schema schema; private Marshaller marshaller; </code></pre> <p>Marshalling code.</p> <pre><code> jaxbContext = JAXBContext.newInstance(ERecordType.class); marshaller = jaxbContext.createMarshaller(); factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schema = factory.newSchema((new File(xsdLocation))); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); ERecordType e = new ERecordType(); e.setCataloging(rc); /** * Validate Against Schema. */ marshaller.setSchema(schema); /** * Marshal will throw an exception if XML not validated against * schema. */ marshaller.marshal(e, System.out); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload