私はJAXBを初めて使用し、特定のXMLをアンマーシャリングしようとしています。
XMLは次のとおりです。
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="3.0">
<Schema xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:sap="http://www.sap.com/Protocols/SAPData" xmlns="http://schemas.microsoft.com/ado/2009/11/edm" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" Namespace="smallApp">
</Schema>
</edmx:DataServices>
次のようなクラスのスキーマを作成しました:
Edmx.java
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "edmx:Edmx")
public class Edmx {
@XmlElement(name = "edmx:DataServices")
private DataService dataService;
public DataService getDataService() {
return dataService;
}
}
DataService.java
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "edmx:DataServices")
public class DataService {
@XmlElement(name = "Schema")
private Schema schema;
public Schema getSchema() {
return schema;
}
}
およびSchema.java
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Schema")
public class Schema {
}
次に、Edmx.classを使用して上記のXMLをアンマーシャリングするサンプルメソッドを作成しました。
public class Parser {
public static void main(String[] args) {
File fXmlFile = new File("somePathToXML");
try {
InputStream inputStream = new FileInputStream(fXmlFile);
Edmx edmx = load(Edmx.class, inputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* loads an object from an xml file
* @param <T>
* @param type
* @param file
* @return
*/
public static <T> T load(Class<? extends T> type, InputStream stream) {
try {
if(stream.available() == 0) {
return null;
}
JAXBContext context = JAXBContext.newInstance(type);
Unmarshaller um = context.createUnmarshaller();
@SuppressWarnings("unchecked")
T obj = (T) um.unmarshal(stream);
return obj;
} catch (JAXBException jaxbe) {
System.out.println("failed to read from InputStream");
jaxbe.printStackTrace();
return null;
} catch (IOException ioe) {
System.out.println("InputStream is empty");
return null;
}
}
}
私が取得し続けるエラーは次のとおりです。
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.microsoft.com/ado/2007/06/edmx", local:"Edmx"). Expected elements are <{}Schema>,<{}edmx:DataServices>,<{}edmx:Edmx>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
私にはそれが期待しない要素がないので、これは奇妙です:(
誰かがアイデアを持っていますか?