简体   繁体   中英

Error in JAXB unmarshalling

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM