クラスのキャスト例外にしようとした場合にunmarshall xml?
質問
ることができるよう過去のクラスをキャストの例外はこちら
FooClass fooClass = (FooClass ) unmarshaller.unmarshal(inputStream);
投この例外:
java.lang.ClassCastException: javax.xml.bind.JAXBElement
かわからないこのクラスにより作成したxjc.バットツールの授業を行っていることを明らかにしていない変更は全くありません鋳造問題のunmarshallerかどうかを調べをする必要がありますの入ったクラスにキャストできることをFooClass.
そのアイデアしないということは間違いだったのか?
解決
FooClass
はXmlRootElement
アノテーションを持っていますか?ない場合は、試してみてください。
Source source = new StreamSource(inputStream);
JAXBElement<FooClass> root = unmarshaller.unmarshal(source, FooClass.class);
FooClass foo = root.getValue();
これはに基づいています非公式のJAXBガイドでます。
他のヒント
使用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の
また、同じ問題を見つけました回答はこちらとした研究を見て私は最も一般的な方法は使用 JAXBIntrospector.そのため-
FooClass fooClass = (FooClass ) unmarshaller.unmarshal(inputStream);
されたものとして
FooClass fooClass = (FooClass) JAXBIntrospector.getValue(unmarshaller.unmarshal(inputStream));
もようがますます重要になっていく。-
T t = (T) JAXBIntrospector.getValue(unmarshaller.unmarshal(inputStream));
充実説明読ん<のhref = "http://web.archive.org/web/20090121025810/http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.htmlのために"REL =" nofollowをnoreferrer ">この記事を。それはすなわち、他のすべての要素を網羅するいくつかのルート要素が存在する必要があり、あなたのXSDが正しく設定されなければならないことが判明します。
XJCたちは複合型から生成するクラスに
@XmlRootElement
注釈を入れてみてくださいありません。正確な条件は多少醜いですが、基本的な考え方は、我々は静的に複合型は、複数の異なるタグ名で使用されないことを保証することができれば、我々は@XmlRootElement
を置くということです。
私は、XMLファイルを見て、それはあなたが見ることを期待するものおおよそであることを確認してくださいと思います。
私はまた、一時的にコードを変更したいです
Object o = unmarshaller.unmarshal(inputStream);
System.out.println(o.getClass());
最初の1 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定義を持っている(例えばXSDはWSDLで定義されている)、その場合に生成されたクラスが@XmlRootElementが欠落しています。だから、ユーザーmbrauhがすでに書いたように、あなたは、JAXBElementの値を取得する必要があります。私の場合は、使用します:
FooClass request = ((JAXBElement< FooClass >) marshaller.unmarshal(new StreamSource(classPathResource.getInputStream()))).getValue();
だから、簡単にdouble型キャストを避けることができるジェネリック医薬品を使用します。
オブジェクトを変換する@XmlRootElement(名= "specifyName"、名前空間を= "名前空間")を指定します。
私はまた、「Javax.xml.bind.JAXBElementをキャストすることはできませんに」エラーが発生したこの非常に簡単な解決策ます:
FooClass fooClass = (FooClass) ((JAXBElement) u.unmarshal(new File("xml/foo.xml")) ).getValue();
どうやら、タイプJAXBElementののオブジェクトが返され、以来、、あなたの代わりに、その値を型キャストする必要があります。
これを試してください:
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement element = (JAXBElement) unmarshaller.unmarshal( new StringReader(xmlString));
Foo foo = (Foo)element;
私の場合、私はエラーを取得します。私は、このエラーをスキップする場合はtrueにプロパティ「ストリップ空白」を設定する必要があります。
受信したコンテンツをデバッグする場合、次のコンテンツを持つリストです。
[0] = "\n"
[1] = JAXBElement
[2] = "\n"
希望誰かを助けます。
あなたがアクセス権を持っていて、XSDを変更することができます。 私はIDEAとXMLから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内部のcomplexType:要素)
<?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はルート要素を生成します!