1

我是 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;
}
}

数据服务.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)

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

有人有想法吗?

4

1 回答 1

1

您可以执行以下操作:

包信息

您可以使用包级别@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;
    }

}

架构

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

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>

了解更多信息

于 2013-02-14T13:34:14.043 回答