Creating an XML element with xsi:nil and attributes in .Net/Jaxb

Error processing SSI file

Answers

  1. Ira

    • 2018/2/18

    In JAXB you can leverage a JAXBElement for this. The JAXBElement can hold a value which has fields/properties mapped to XML attributes and a flag that tracks whether the element was nil.

    Java Model

    Foo

    Instead of having a field/property of type Bar you specify JAXBElement<Bar>.

    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Foo {
    
        @XmlElementRef(name="bar")
        private JAXBElement<Bar> bar;
    
    }
    

    Bar

    Bar has fields/properties mapped to XML attributes.

    import javax.xml.bind.annotation.XmlAttribute;
    
    public class Bar {
    
        @XmlAttribute
        private String baz;
    
    }
    

    ObjectFactory

    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.*;
    import javax.xml.namespace.QName;
    
    @XmlRegistry
    public class ObjectFactory {
    
        @XmlElementDecl(name="bar")
        public JAXBElement<Bar> createBar(Bar bar) {
            return new JAXBElement<Bar>(new QName("bar"), Bar.class, bar);
        }
    
    }
    
    Demo Code

    Demo

    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Foo.class, ObjectFactory.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum19797412/input.xml");
            Foo foo = (Foo) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(foo, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <foo>
        <bar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" baz="Hello World" xsi:nil="true"/>
    </foo>
    
  2. Vivaan

    • 2018/7/10

    Creating an XML element with xsi:nil and attributes in .Net/Jaxb · java .net xml jaxb xml-nil. I have an XML Schema that says: <xs: 

  3. Keenan

    • 2016/3/31

    In JAXB you can leverage a JAXBElement for this. The JAXBElement can hold a value which has fields/properties mapped to XML attributes and a flag that tracks whether the element was nil.

  4. Marlon

    • 2020/7/20

    A JAXBElement<T> type can also sometimes be generated when a WSDL contains advanced XML schema features such as substitution groups or elements that are 

  5. Iker

    • 2016/8/18

    but as soon as attributes (z.b. xsi:nil) are on the leaf-node, jackson. seems to think its a branch-node instead and throws an exception. while mapping it to an pojo. JAXB does not serialize NULL-values per default. If fields are annotated with @nillable, the field will be serialized as <x xsi:nil="true/>.

  6. D'Amico

    • 2020/5/7

    (element instance's xsi:nil attribute). The declaredType and scope property are the JAXB class binding for the xml type definition.

  7. Mordechai

    • 2018/3/8

    xsi:nil is the correct way to represent a value such that: When the DOM Level 2 call getElementValue() is issued, the NULL value is returned. xsi:nil is also used to indicate a valid element with no content even if that elements content type normally doesn’t allow empty elements.

  8. Matteo

    • 2016/3/12

    NET binds the XML schema construct to System.Nullable<int>[] . The element is optional and nillable. However, when marshalling JAXB marshaller will always 

  9. Shehu

    • 2018/8/18

    Let's see the steps to convert XML document into java object. Create POJO or bind the schema and generate the classes. Create the JAXBContext object. Create the Unmarshaller objects. Call the unmarshal method. Use getter methods of POJO to access the data.

  10. Lefebvre

    • 2021/3/5

    and I would concur with others suggesting jaxb for XML to pojo, well supported lots The xsi:nil attribute is used to indicate that the lastName is null.

  11. Leighton

    • 2019/5/20

    (element instance's xsi:nil attribute) The declaredType and scope property are the JAXB class binding for the xml type definition. Scope is either JAXBElement.GlobalScope or the Java class representing the complex type definition containing the schema element declaration.

  12. Denver

    • 2020/10/5

    The JAXB RI does not call the XmlAdapter mechanism for null values, i.e. xsi:nil=true is similar to SQL NULL but having empty element 

  13. Reginald

    • 2015/12/11

    If we need to get xml tags also for null values object then define these property in model class with attribute nillable=true. 1. 2. @XmlElement 

  14. Ibrahim

    • 2018/5/15

    The XmlArrayAttribute is applied to the members that become XML element arrays. a member as an empty XML tag with the xsi:nil attribute set to true .

Comments are closed.

More Posts