0

I have created a very basic XML for understanding JAXB Concept. This is the XML File

    <?xml version='1.0' encoding='UTF-8' standalone='yes'?>

    <Abc>

        <Module> India </Module>

     </Abc> 

The Java Class created is,

package oracle.ERP.Cloud.Client2;

import java.util.ArrayList;  
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

//Below annotation defines root element of XML file
@XmlRootElement
public class Abc{

 private String Module;

 public String getModule() {
   System.out.println("Hi");
  return Module;
 }

 @XmlElement
 public void setModule(String Module) {
  this.Module = Module;
 }

}

Java file for Unmarshalling is

package oracle.ERP.Cloud.client2;

import java.io.File;

import java.io.PrintStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import oracle.ERP.Cloud.Client2.Abc;
import oracle.ERP.Cloud.client.Country;

public class JAXBXMLToJava {

    public static void main(String Args[]) {
      try {
        JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{oracle.ERP.Cloud.Client2.Abc.class});
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        File XMLFile = new File("C:\\Users\\NRENTALA\\Desktop\\Analysis\\AbcXML.xml");
        oracle.ERP.Cloud.Client2.Abc summary = oracle.ERP.Cloud.Client2.Abc)unmarshaller.unmarshal(XMLFile);
        System.out.println("Country Name is : "+ summary.getModule());
      }

      catch(JAXBException e) {
           e.printStackTrace();
      }
    }
}

When I try to compile this I am getting this error

    javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Abc"). Expected elements are <{}abc>

Please help me in finding out what is the problem?? Trying this for the first time.

Nelson
  • 23
  • 1
  • 4

1 Answers1

2

XML is case sensitive, so if your schema specifies the root element to be called "abc" (as the error message suggests), it will not be able to parse "Abc". Have a look at this question.

To achieve case insensitive parsing, have a look at this blog by Blaise Doughan, one of the people behind specification and implementation of JAXB.

Community
  • 1
  • 1
ftr
  • 2,105
  • 16
  • 29
  • Thankyou very much. It has solved the problem. But What if the XML elements contains all upper case letters? How do we parse such an XML. I actually need to parse an XML which has all uppercase letters. – Nelson Oct 13 '14 at 11:48