gpt4 book ai didi

java - 尝试使用 JAXB 解码 EDMX 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:41 25 4
gpt4 key购买 nike

我是 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)

这很奇怪,因为我没有它不期望的元素:(

有人有想法吗?

最佳答案

您可以执行以下操作:

包信息

您可以使用包级别 @XmlSchema 注释来指定模型的命名空间限定。下面我已指定,除非另有说明,否则所有元素都将由 http://schemas.microsoft.com/ado/2007/06/edmx 命名空间限定。我还指定该命名空间的首选前缀是 edmx,并且 http://schemas.microsoft.com/ado/2007/06/edmx 应该是默认命名空间。

@XmlSchema(
namespace="http://schemas.microsoft.com/ado/2007/06/edmx",
elementFormDefault=XmlNsForm.QUALIFIED,
xmlns={
@XmlNs(prefix="edmx", namespaceURI="http://schemas.microsoft.com/ado/2007/06/edmx"),
@XmlNs(prefix="", namespaceURI="http://schemas.microsoft.com/ado/2009/11/edm")
}
)
@XmlAccessorType(XmlAccessType.FIELD)
package forum14875956;

import javax.xml.bind.annotation.*;

Edmx

现在不需要在 Edmx 类上指定命名空间信息。

package forum14875956;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Edmx")
public class Edmx {

@XmlElement(name = "DataServices")
private DataService dataService;

public DataService getDataService() {
return dataService;
}

}

数据服务

DataService 类上,我们将为 schema 属性的映射指定一个命名空间。

package forum14875956;

import javax.xml.bind.annotation.*;

public class DataService {

@XmlElement(name = "Schema", namespace = "http://schemas.microsoft.com/ado/2009/11/edm")
private Schema schema;

public Schema getSchema() {
return schema;
}

}

架构

您可以使用 @XmlType 注释覆盖 Schema 类上属性的命名空间限定。

package forum14875956;

import javax.xml.bind.annotation.XmlType;

@XmlType(namespace="http://schemas.microsoft.com/ado/2009/11/edm")
public class Schema {

}

演示

package forum14875956;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Edmx.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum14875956/input.xml");
Edmx edmx = (Edmx) unmarshaller.unmarshal(xml);

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(edmx, System.out);
}

}

输入.xml/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<edmx:DataServices>
<Schema/>
</edmx:DataServices>
</edmx:Edmx>

了解更多信息

关于java - 尝试使用 JAXB 解码 EDMX 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14875956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com