ERLEDIGT
JA
JA
ANTWORTEN
4
4
ZUGRIFFE
2290
2290
EMPFEHLEN
-
Einen schönen Freitag wünsche ich euch!
Ich habe Dr. Google und Krankenpflegerin Sufu mehrfach aufgesucht, aber mein Leider geht weiter.
Worum gehts:
Im folgenden Code seht ihr mein Programm und zwei Klassen.
Ich möchte mit dem Programm (erstmal) nur eine XML Datei einlesen und ein bestimmtes Element (GoodUrls) auf der Konsole ausgeben lassen.
Diverse Tutorial haben mir den Weg geleitet doch folgende Codezeile löst eine Exception aus.
Code :1
Collection eef = (Collection)unmarshaller.unmarshal(new FileInputStream("config.xml"));
Die Fehlermeldung dazu:
Code :1
java.lang.ClassCastException: jaxb.Eef cannot be cast to jaxb.Collection
Die Klasse Eef die verlang wird, bietet mir aber nicht die Möglichkeit auf das XML-Element GoodUrls zu zugreifen. Deswegen möchte ich das Objekt als Collection casten um dann die Methode getGoodUrls() aufzurufen.
Hier nun der Code der beiden Klassen und des Programms:
Programm:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
package jaxb; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; //import javax.xml.bind.Marshaller; import xmlparser.Url; import java.io.*; import java.util.Iterator; import java.util.List; public class A { public static void main(String[] args) { try{ Collection collection = new Collection(); GoodUrls goodUrls = new GoodUrls(); JAXBContext jc = JAXBContext.newInstance("jaxb"); Unmarshaller unmarshaller = jc.createUnmarshaller(); //Eef laut Tutoarial falsch. Richtig ist Collection um GoodUrls zu selektieren Collection eef = (Collection)unmarshaller.unmarshal(new FileInputStream("config.xml")); //System.err.println(unmarshaller.unmarshal(new FileInputStream("config.xml")).getClass().getName()); /* goodUrls = collection.getGoodUrls();
Klasse Eef:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.1-b02-fcs // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2009.01.20 at 02:13:39 PM CET // package jaxb; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** * <p>Java class for anonymous complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element ref="{}config"/> * <element ref="{}signature"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "config", "signature" }) @XmlRootElement(name = "eef") public class Eef { @XmlElement(required = true) protected Config config; @XmlElement(required = true) protected Signature signature; /** * Gets the value of the config property. * * @return * possible object is * {@link Config } * */ public Config getConfig() { return config; } /** * Sets the value of the config property. * * @param value * allowed object is * {@link Config } * */ public void setConfig(Config value) { this.config = value; } /** * Gets the value of the signature property. * * @return * possible object is * {@link Signature } * */ public Signature getSignature() { return signature; } /** * Sets the value of the signature property. * * @param value * allowed object is * {@link Signature } * */ public void setSignature(Signature value) { this.signature = value; } }
Klasse Collection:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.1-b02-fcs // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2009.01.20 at 02:13:39 PM CET // package jaxb; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlSchemaType; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; /** * <p>Java class for anonymous complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element ref="{}bad_urls"/> * <element ref="{}good_urls"/> * <element ref="{}prerequisite_results"/> * <element ref="{}testwords"/> * </sequence> * <attribute name="Name" use="required" type="{http://www.w3.org/2001/XMLSchema}NMTOKEN" /> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "badUrls", "goodUrls", "prerequisiteResults", "testwords" }) @XmlRootElement(name = "collection") public class Collection { @XmlElement(name = "bad_urls", required = true) protected BadUrls badUrls; @XmlElement(name = "good_urls", required = true) protected GoodUrls goodUrls; @XmlElement(name = "prerequisite_results", required = true) protected PrerequisiteResults prerequisiteResults; @XmlElement(required = true) protected String testwords; @XmlAttribute(name = "Name", required = true) @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "NMTOKEN") protected String name; /** * Gets the value of the badUrls property. * * @return * possible object is * {@link BadUrls } * */ public BadUrls getBadUrls() { return badUrls; } /** * Sets the value of the badUrls property. * * @param value * allowed object is * {@link BadUrls } * */ public void setBadUrls(BadUrls value) { this.badUrls = value; } /** * Gets the value of the goodUrls property. * * @return * possible object is * {@link GoodUrls } * */ public GoodUrls getGoodUrls() { return goodUrls; } /** * Sets the value of the goodUrls property. * * @param value * allowed object is * {@link GoodUrls } * */ public void setGoodUrls(GoodUrls value) { this.goodUrls = value; } /** * Gets the value of the prerequisiteResults property. * * @return * possible object is * {@link PrerequisiteResults } * */ public PrerequisiteResults getPrerequisiteResults() { return prerequisiteResults; } /** * Sets the value of the prerequisiteResults property. * * @param value * allowed object is * {@link PrerequisiteResults } * */ public void setPrerequisiteResults(PrerequisiteResults value) { this.prerequisiteResults = value; } /** * Gets the value of the testwords property. * * @return * possible object is * {@link String } * */ public String getTestwords() { return testwords; } /** * Sets the value of the testwords property. * * @param value * allowed object is * {@link String } * */ public void setTestwords(String value) { this.testwords = value; } /** * Gets the value of the name property. * * @return * possible object is * {@link String } * */ public String getName() { return name; } /** * Sets the value of the name property. * * @param value * allowed object is * {@link String } * */ public void setName(String value) { this.name = value; } }
Worin liegt der Fehler?
Gruß
eagle
-
23.01.09 10:46 #2
Wie sieht denn das XML Dokument aus. Scheint, als stünde da eben Eef als XML Rootelement drin.
Gruß
OllieIn theory, there is no difference between theory and practice. In practice, there is!
www.olivergierke.de
-
Ja das stimmt. Hier mal ein kleiner Auszug aus der XML-Datei:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13
<?xml version="1.0" encoding="UTF-8" ?> <eef> <config EnterpriseVersion="'5.0.4'" Schema="2.0"> <collections Count="6"> <collection Name="default_collection"> <bad_urls><![CDATA[ ]]></bad_urls> <good_urls><![CDATA[ / ]]></good_urls> <prerequisite_results><![CDATA[ 20
Aber Eef gibt mir nicht die Möglichkeit auf getGoodUrls() zu zugreifen.
Oder muss ich über das RootElement starten?
Gruß
eagleGeändert von eaglestar (23.01.09 um 10:54 Uhr)
-
23.01.09 11:02 #4
Ja sicher... du kannst doch nicht einfach das Eef auf Collection casten? Woher soll JAXB wissen, was du genau willst? Es bildet dir nur den kompletten XML Baum auf Objekte ab. Wenn du danach auf Teile des Baums zugreifen willst, musst du die Objekte traversieren:
Code java:1 2 3
for (Collection collection : eef.getConfig().getCollections()) { //... }
Ich versteh ehrlich gesagt nicht, wie du darauf kommst, das mit einem Cast erschlagen zu können?
Gruß
OllieIn theory, there is no difference between theory and practice. In practice, there is!
www.olivergierke.de
-
Was soll ich sagen...
Ich glaube selbst das Sprichwort: "Man sieht den Wald vor lauter Bäumen nicht." passt hier nicht ganz.
Habe die Ausgabe hinbekommen!
Hier der Code:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
public class A { public static void main(String[] args) { try{ //Collection collection = new Collection(); GoodUrls goodUrls = new GoodUrls(); JAXBContext jc = JAXBContext.newInstance("jaxb"); Unmarshaller unmarshaller = jc.createUnmarshaller(); Eef eef = (Eef)unmarshaller.unmarshal(new FileInputStream("config.xml")); //System.out.println(eef.getConfig().getCollections()); for (Collection collection : eef.getConfig().getCollections().getCollection()) { System.out.println(collection.getGoodUrls().getContent()); } }catch (Exception e ) { e.printStackTrace(); } } }
Vielen Dank an dich!
Du hast meine Schlaf und mein Wochenende gerettet!
Aber keine Angst, wenn ich wieder feststecke melde ich mich wieder
Gruß
eagle
Ähnliche Themen
-
JAXB Exception Problem
Von dreamer29 im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 0Letzter Beitrag: 21.06.10, 11:37 -
Fehler beim Parsen von XML mit DOM
Von Cronk im Forum JavaAntworten: 5Letzter Beitrag: 28.04.10, 18:11 -
Jaxb Exception beim unmarshaller
Von Jamous im Forum JavaAntworten: 5Letzter Beitrag: 21.04.08, 00:42 -
JAXB - Exception bei Unmarshall
Von y0dA im Forum JavaAntworten: 2Letzter Beitrag: 29.02.08, 09:53 -
JAXB: Fehler bei Unmarshaling
Von Rolando80 im Forum JavaAntworten: 0Letzter Beitrag: 17.09.07, 12:05





Zitieren
Login





