【发布时间】:2014-10-13 11:31:42
【问题描述】:
我创建了一个非常基本的 XML 来理解 JAXB 概念。这是 XML 文件
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<Abc>
<Module> India </Module>
</Abc>
创建的 Java 类是,
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;
}
}
用于 Unmarshalling 的 Java 文件是
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();
}
}
}
当我尝试编译时,我得到了这个错误
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Abc"). Expected elements are <{}abc>
请帮我找出问题所在??第一次尝试。
【问题讨论】:
-
为什么你的代码在 oracle 的包中?你是如何生成你的类的?
-
你能发布 oracle.ERP.Cloud.Client2.Abc 总结吗?