5

I am currently using JAXB to parse xml files. I generated the classes needed through an xsd file. However, the xml files I receive do not contain all the nodes declared in the generated classes. The following is an example of my xml file's structure:

<root>
<firstChild>12/12/2012</firstChild> 
<secondChild>
<firstGrandChild>
<Id>
  </name>
  <characteristics>Description</characteristics> 
  <code>12345</code>
</Id>
</firstGrandChild>
</secondChild>
</root>

I am confronted with the following two cases :

  1. The node <name> is present in the generated classes but not in the XML files
  2. The node has no value

In both cases, the value is set to null. I would like to be able to differentiate when the node is absent from the XML file and when it's present but has a null value. Despite my searches, I didn't figure out a way to do so. Any help is more than welcome

Thank you so much in advance for your time and help

Regards

MMM
  • 7,221
  • 2
  • 24
  • 42
user2249868
  • 53
  • 1
  • 3
  • xsi:nil is your friend! Read more here http://stackoverflow.com/questions/774192/what-is-the-correct-way-to-represent-null-xml-elements – MariuszS Apr 05 '13 at 16:50
  • Thank you for your answer ! As I understood, this is for setting the node's value to "" instead of null. The problem that I'm facing is that for JAXB, an empty node and an unexistant node in the xml file are handled in the same way. So I can't know whether I received the node in the file or I did and it's value was null – user2249868 Apr 05 '13 at 16:54
  • The following example will help with `xsi:nil`: http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html – bdoughan Apr 05 '13 at 17:27

1 Answers1

3

A JAXB (JSR-222) implementation won't call the set method for absent nodes. You could put logic in your set method to track whether or not it has been called.

public class Foo {

    private String bar;
    private boolean barSet = false;

    public String getBar() {
       return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
        this.barSet = true;
    }

}

UPDATE

JAXB will also treat empty nodes as having a value of empty String.

Java Model

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Root {

    private String foo;
    private String bar;

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

}

Demo

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum15839276/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <foo></foo>
</root>
bdoughan
  • 147,609
  • 23
  • 300
  • 400