2

I have a XML file in which I have certain attributes. And I have implementation classes to parse the file to get the values. But When I tested with missing values it works fine. My program wants to throw error or warning when it runs. Please suggest me a solution.

sample "hello.xml"

<head>
<welcome>Hi</welcome>
<id>1</id>
<uname>chill</uname>
<persons>
<person>
<name>1</name>
<age>23</age>
<city>huils</city>
</person>
<person>
<name>2</name>
<age>23</age>
<city>huils</city>
</person>
<person>
<name>3</name>

</person>
</persons>
</head>

Now in person 3 I have missing attributes and tags age and city. But my program works fine without throwing error. Whats wrong.

Implementation program :

@XmlElementWrapper(name="welcome")
    public void setwelcome(String welcome) {
        welcome= welcome;
    }

@XmlElement (name = "id")
    public void setid(String id) {
        id= id;
    }

    @XmlElement (name = "id")
    public void setid(String id) {
        id= id;
    }

    @XmlElement (name = "name")
    public void setname(String name) {
        name= name;
    }


    @XmlElement(name="age")
    public void setage(String age) {
        age= age;
    }

   @XmlElement(name="city")
    public void setage(String city) {
        city= city;
    }
BelieveToLive
  • 281
  • 1
  • 4
  • 18
  • My though about `required=true` is that not used for nothing, serving only for documentation scope. You could just define a `afterUnmarshal() `method on your class which programmatically validates the state of the object, throwing an exception if it doesn't like it. But if you want a validation that works, you should take in consideration the creation of an `XSD` schema for you `XML`. – Xstian Apr 13 '15 at 12:51
  • My xml is big and how to generate xsd from xml? is that any other means? – BelieveToLive Apr 13 '15 at 13:53
  • You could generate XSD from java classes [see this question](http://stackoverflow.com/questions/5067617/generate-xsd-files-from-java-classes), or from XML [this](http://xmlgrid.net/xml2xsd.html) site. See also [here](http://stackoverflow.com/questions/10017139/how-to-create-a-xsd-scheme-from-a-class). – Xstian Apr 13 '15 at 14:05

2 Answers2

0

You have to mark desired elements as mandatory ones with a required annotation property. Default value is false and thus missing elements don't trigger an error.

@XMLElement(name=<name>, required=true)

As for attributes it works the same way, you have to mark them as required.

@XmlAttribute(required=true)
S. Pauk
  • 5,208
  • 4
  • 31
  • 41
  • @BelieveToLive could you show the whole JAXB annotated class and also the code responsible for unmarshalling? – S. Pauk Apr 13 '15 at 09:55
-1

Generally it is a good idea not to write the JAXB java code by hand but to generate it from an xsd file. You can use the xsd also for validating your xml to prevent any parsing issue during mapping it to java objects. If you do it in that way and your xml does not match then you will get more sophisticated error messages which helps to identify the problem.

Here is how to generate JAXB java classes from xsd in Eclipse.

Laszlo Hirdi
  • 1,140
  • 12
  • 18
  • Why is it not a good idea to write the JAXB Java code by hand? It was designed to be written by hand with generation from a schema offered as a convenience mechanism. – bdoughan Apr 13 '15 at 09:08