想让过去一类投例外在这里:

FooClass fooClass = (FooClass ) unmarshaller.unmarshal(inputStream);

抛出这个例外:

java.lang.ClassCastException: javax.xml.bind.JAXBElement

我不懂这个-为的类产生的xjc.蝙蝠工具和课程产生了我不会改变了-因此不应有任何铸造的问题在这里-反编组器真的应该给我回一类,可以转换为FooClass.

任何想法什么我做错了什么?

有帮助吗?

解决方案

是否FooClass具有XmlRootElement注释?如果没有,尝试:

Source source = new StreamSource(inputStream);
JAXBElement<FooClass> root = unmarshaller.unmarshal(source, FooClass.class);
FooClass foo = root.getValue();

这是基于在非官方JAXB指南

其他提示

在使用的JAXBElement到JAXBIntrospector得到schemaObject像>>

JAXBContext jaxbContext = JAXBContext.newInstance(Class.forName(className));
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object schemaObject = JAXBIntrospector.getValue(unmarshaller.unmarshal(new ByteArrayInputStream(xmlString.getBytes())));

参见:确实JAXB时的Unmarshaller.unmarshal返回的JAXBElement 或MySchemaObject?

我今天遇到了同样的问题,在这里看到了答案,做了一些研究,在我看来,最通用的解决方案是使用 JAXBIntrospector 。因此 -

FooClass fooClass = (FooClass ) unmarshaller.unmarshal(inputStream);

应写为

FooClass fooClass = (FooClass) JAXBIntrospector.getValue(unmarshaller.unmarshal(inputStream));

甚至更好,使它更通用 -

T t = (T) JAXBIntrospector.getValue(unmarshaller.unmarshal(inputStream));

为了更充分地解释读 这篇文章.事实证明,你的文件必须适当地加以设定,即必须有一些根本的元素,包括所有其他元素。

XJC并尝试把 @XmlRootElement 注释上的一类,我们产生自一个复杂的类型。的确切条件是有点丑,但基本想法是,如果我们能够静态地保证一个复杂的类型不会使用的多种不同标签名称,我们把 @XmlRootElement.

我想看看XML文件,并确保它大致是你希望看到什么。

我还暂时代码更改为:

Object o = unmarshaller.unmarshal(inputStream);
System.out.println(o.getClass());

如果第一个failes那么类铸件解组方法内发生的事情,如果成功,那么你就可以看到你又回到实际的类,然后找出它为什么不是你希望它是什么。

我们花了太多时间与JAXB工厂类坐立不安,以满足反编组。我们已经了解到,使用解组的的调用JAXB生成的对象工厂工作正常的。希望示例代码赎回人的无奈:

System.out.println("Processing generic-type unmarshaller: ");
MessageClass mcObject = unmarshalXml(MessageClass.class, msgQryStreamSource,
    NAMESPACE + "." + "MessageClass");

public static <T> T unmarshalXml(Class<T> clazz, StreamSource queryResults,
    String contextNamespace)
    {
        T resultObject = null;
        try {
            //Create instance of the JAXBContext from the class-name
            JAXBContext jc;
            jc = JAXBContext.newInstance(Class.forName(clazz.getName()));
            Unmarshaller u = jc.createUnmarshaller();
            resultObject = clazz.cast(u.unmarshal(queryResults));
            }
              //Put your own error-handling here.
        catch(JAXBException e)
        {
            e.printStackTrace();
        }
        catch (ClassCastException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        return clazz.cast(resultObject);
    }

从同事预览答案,以防万一有人建筑仍然在寻找一个答案。

我不得不具有我的方案的根元素的问题被定义为:

<schema>
  <element name="foo" type="bar" />
  <complexType name="bar" />
</schema>

和因此我在得到一个角色例外:

try {            
        javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(mobilityConfigType.getClass().getPackage().getName());            
        javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        File f = FileUtil.toFile(this.getPrimaryFile());            
        mobilityConfigType = (MobilityModelConfigType)unmarshaller.unmarshal(FileUtil.toFile(this.getPrimaryFile()));
    } catch (javax.xml.bind.JAXBException ex) {            
        java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
    }

我所做的改变在try块的第一行:

javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(mobilityConfigType.getClass().getName());

这解决了这个问题对我来说。

您绝对肯定FooClass是你通过它的XML输入源的根元素?和解组将返回XJC创建的根元素的目的。

有时你有与多个不同的根元素(例如在XSD WSDL中定义)一个XSD定义和在这种情况下,生成的类缺少@XmlRootElement。因此,作为用户mbrauh已经写你必须得到的JAXBElement的价值。在我的情况下,我使用的:

FooClass request = ((JAXBElement< FooClass >) marshaller.unmarshal(new StreamSource(classPathResource.getInputStream()))).getValue();

因此,使用泛型可以很容易地避免双重型铸件。

指定@XmlRootElement(名称= “specifyName”,命名空间= “命名空间”)来变换对象。

我也遇到了“Javax.xml.bind.JAXBElement不能转换到”错误,发现这个非常简单的解决方案:

FooClass fooClass = (FooClass) ((JAXBElement) u.unmarshal(new File("xml/foo.xml")) ).getValue();

由于,显然,则返回类型的JAXBElement的目的,需要强制转换它的值来代替。

来源: https://forums.oracle.com/thread/1625944

尝试这种情况:

JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement element = (JAXBElement) unmarshaller.unmarshal( new StringReader(xmlString));
Foo foo = (Foo)element;

在我的情况,我试图从SOAPUI应用程序发送SOAP上访时得到的错误。我需要设置属性“带空格”为true跳过该错误。

当调试接收到的内容,是与下一个内容的列表:

[0] = "\n"
[1] = JAXBElement
[2] = "\n"

希望帮助别人。

如果你有机会,你可以修改XSD。 对我来说,当我从XML生成与IDEA的XSD这个问题追加。

使用此xml:

<?xml version="1.0"?>
<schema>
  <element name="foo" type="bar" />
  <complexType name="bar" />
</schema>

IDEA产生这样一个XSD和JAXB不会产生根元素:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="schema" type="schemaType"/>
  <xs:complexType name="schemaType">
    <xs:sequence>
      <xs:element type="elementType" name="element"/>
      <xs:element type="complexTypeType" name="complexType"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="elementType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="name"/>
        <xs:attribute type="xs:string" name="type"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="complexTypeType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="name"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>

但是,如果你修改这样的XSD(为了获得修改根元素“模式” XS:复杂类型的标签的xs内:元素):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="schema">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="elementType" name="element"/>
        <xs:element type="complexTypeType" name="complexType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="schemaType">
    <xs:sequence>
      <xs:element type="elementType" name="element"/>
      <xs:element type="complexTypeType" name="complexType"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="elementType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="name"/>
        <xs:attribute type="xs:string" name="type"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="complexTypeType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="name"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>

JAXB将生成的根元素!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top