2017-01-26 4 views
0

J'ai beaucoup cherché sur ce site et j'ai essayé plusieurs choses mais je n'ai pas encore corrigé cette erreur. L'erreur que j'obtiens est:javax.xml.bind.UnmarshalException: élément inattendu (URI: "", local: "NewDataSet"). Les éléments attendus sont <{http://www.webserviceX.NET} string>

javax.xml.bind.UnmarshalException: unexpected element (URI:"", local:"NewDataSet"). Expected elements are <{http://www.webserviceX.NET}string> 

En essayant de démashaler je reçois l'erreur précédente. J'appelle un webservice et je reçois le résultat suivant, qui est une chaîne avec la structure XML suivante:

<NewDataSet> 
    <Table> 
    <Book>1</Book> 
    <BookTitle>Genesis</BookTitle> 
    </Table> 
    <Table> 
    <Book>2</Book> 
    <BookTitle>Exodus</BookTitle> 
    </Table> 
    <Table> 
    <Book>3</Book> 
    <BookTitle>Leviticus</BookTitle> 
    </Table> 
    <Table> 
    <Book>4</Book> 
    <BookTitle>Numbers</BookTitle> 
    </Table> 
    <Table> 
    <Book>5</Book> 
    <BookTitle>Deuteronomy</BookTitle> 
    </Table> 
    <Table> 
    <Book>6</Book> 
    <BookTitle>Joshua</BookTitle> 
    </Table> 
    <Table> 
    <Book>7</Book> 
    <BookTitle>Judges</BookTitle> 
    </Table> 
    <Table> 
    <Book>8</Book> 
    <BookTitle>Ruth</BookTitle> 
    </Table> 
    <Table> 
    <Book>9</Book> 
    <BookTitle>1 Samuel</BookTitle> 
    </Table> 
    <Table> 
    <Book>10</Book> 
    <BookTitle>2 Samuel</BookTitle> 
    </Table> 
    <Table> 
    <Book>11</Book> 
    <BookTitle>1 Kings</BookTitle> 
    </Table> 
    <Table> 
    <Book>12</Book> 
    <BookTitle>2 Kings</BookTitle> 
    </Table> 
    <Table> 
    <Book>13</Book> 
    <BookTitle>1 Chronicles</BookTitle> 
    </Table> 
    <Table> 
    <Book>14</Book> 
    <BookTitle>2 Chronicles</BookTitle> 
    </Table> 
    <Table> 
    <Book>15</Book> 
    <BookTitle>Ezra</BookTitle> 
    </Table> 
    <Table> 
    <Book>16</Book> 
    <BookTitle>Nehemiah</BookTitle> 
    </Table> 
    <Table> 
    <Book>17</Book> 
    <BookTitle>Esther</BookTitle> 
    </Table> 
    <Table> 
    <Book>18</Book> 
    <BookTitle>Job</BookTitle> 
    </Table> 
    <Table> 
    <Book>19</Book> 
    <BookTitle>Psalms</BookTitle> 
    </Table> 
    <Table> 
    <Book>20</Book> 
    <BookTitle>Proverbs</BookTitle> 
    </Table> 
    <Table> 
    <Book>21</Book> 
    <BookTitle>Ecclesiastes</BookTitle> 
    </Table> 
    <Table> 
    <Book>22</Book> 
    <BookTitle>Song of Solomon</BookTitle> 
    </Table> 
    <Table> 
    <Book>23</Book> 
    <BookTitle>Isaiah</BookTitle> 
    </Table> 
    <Table> 
    <Book>24</Book> 
    <BookTitle>Jeremiah</BookTitle> 
    </Table> 
    <Table> 
    <Book>25</Book> 
    <BookTitle>Lamentations</BookTitle> 
    </Table> 
    <Table> 
    <Book>26</Book> 
    <BookTitle>Ezekiel</BookTitle> 
    </Table> 
    <Table> 
    <Book>27</Book> 
    <BookTitle>Daniel</BookTitle> 
    </Table> 
    <Table> 
    <Book>28</Book> 
    <BookTitle>Hosea</BookTitle> 
    </Table> 
    <Table> 
    <Book>29</Book> 
    <BookTitle>Joel</BookTitle> 
    </Table> 
    <Table> 
    <Book>30</Book> 
    <BookTitle>Amos</BookTitle> 
    </Table> 
    <Table> 
    <Book>31</Book> 
    <BookTitle>Obadiah</BookTitle> 
    </Table> 
    <Table> 
    <Book>32</Book> 
    <BookTitle>Jonah</BookTitle> 
    </Table> 
    <Table> 
    <Book>33</Book> 
    <BookTitle>Micah</BookTitle> 
    </Table> 
    <Table> 
    <Book>34</Book> 
    <BookTitle>Nahum</BookTitle> 
    </Table> 
    <Table> 
    <Book>35</Book> 
    <BookTitle>Habakkuk</BookTitle> 
    </Table> 
    <Table> 
    <Book>36</Book> 
    <BookTitle>Zephaniah</BookTitle> 
    </Table> 
    <Table> 
    <Book>37</Book> 
    <BookTitle>Haggai</BookTitle> 
    </Table> 
    <Table> 
    <Book>38</Book> 
    <BookTitle>Zechariah</BookTitle> 
    </Table> 
    <Table> 
    <Book>39</Book> 
    <BookTitle>Malachi</BookTitle> 
    </Table> 
    <Table> 
    <Book>40</Book> 
    <BookTitle>Matthew</BookTitle> 
    </Table> 
    <Table> 
    <Book>41</Book> 
    <BookTitle>Mark</BookTitle> 
    </Table> 
    <Table> 
    <Book>42</Book> 
    <BookTitle>Luke</BookTitle> 
    </Table> 
    <Table> 
    <Book>43</Book> 
    <BookTitle>John</BookTitle> 
    </Table> 
    <Table> 
    <Book>44</Book> 
    <BookTitle>Acts</BookTitle> 
    </Table> 
    <Table> 
    <Book>45</Book> 
    <BookTitle>Romans</BookTitle> 
    </Table> 
    <Table> 
    <Book>46</Book> 
    <BookTitle>1 Corinthians</BookTitle> 
    </Table> 
    <Table> 
    <Book>47</Book> 
    <BookTitle>2 Corinthians</BookTitle> 
    </Table> 
    <Table> 
    <Book>48</Book> 
    <BookTitle>Galatians</BookTitle> 
    </Table> 
    <Table> 
    <Book>49</Book> 
    <BookTitle>Ephesians</BookTitle> 
    </Table> 
    <Table> 
    <Book>50</Book> 
    <BookTitle>Philippians</BookTitle> 
    </Table> 
    <Table> 
    <Book>51</Book> 
    <BookTitle>Colossians</BookTitle> 
    </Table> 
    <Table> 
    <Book>52</Book> 
    <BookTitle>1 Thessalonians</BookTitle> 
    </Table> 
    <Table> 
    <Book>53</Book> 
    <BookTitle>2 Thessalonians</BookTitle> 
    </Table> 
    <Table> 
    <Book>54</Book> 
    <BookTitle>1 Timothy</BookTitle> 
    </Table> 
    <Table> 
    <Book>55</Book> 
    <BookTitle>2 Timothy</BookTitle> 
    </Table> 
    <Table> 
    <Book>56</Book> 
    <BookTitle>Titus</BookTitle> 
    </Table> 
    <Table> 
    <Book>57</Book> 
    <BookTitle>Philemon</BookTitle> 
    </Table> 
    <Table> 
    <Book>58</Book> 
    <BookTitle>Hebrews</BookTitle> 
    </Table> 
    <Table> 
    <Book>59</Book> 
    <BookTitle>James</BookTitle> 
    </Table> 
    <Table> 
    <Book>60</Book> 
    <BookTitle>1 Peter</BookTitle> 
    </Table> 
    <Table> 
    <Book>61</Book> 
    <BookTitle>2 Peter</BookTitle> 
    </Table> 
    <Table> 
    <Book>62</Book> 
    <BookTitle>1 John</BookTitle> 
    </Table> 
    <Table> 
    <Book>63</Book> 
    <BookTitle>2 John</BookTitle> 
    </Table> 
    <Table> 
    <Book>64</Book> 
    <BookTitle>3 John</BookTitle> 
    </Table> 
    <Table> 
    <Book>65</Book> 
    <BookTitle>Jude</BookTitle> 
    </Table> 
    <Table> 
    <Book>66</Book> 
    <BookTitle>Revelation</BookTitle> 
    </Table> 
</NewDataSet> 

Le code J'utilise est la suivante:

public class Main { 
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException { 
    BibleWebservice service = new BibleWebservice(); 
    BibleWebserviceSoap soap = service.getBibleWebserviceSoap(); 
    String bookTitles = soap.getBookTitles(); 
    try { 
     StringReader sr = new StringReader(bookTitles); 
     JAXBContext jaxbContext = JAXBContext.newInstance(Book.class); 
     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
     Book book = (Book)jaxbUnmarshaller.unmarshal(sr); 
     //Book book = ((JAXBElement<Book>) jaxbContext.createUnmarshaller().unmarshal(sr)).getValue(); 
    } catch (JAXBException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    //System.out.println(bookTitles); 
} 

} 

Ma classe du livre est :

// 
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// 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: 2017.01.25 at 03:45:20 PM GMT 
// 


package filesgeneratedfromxml; 

import java.util.ArrayList; 
import java.util.List; 
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> 
* &lt;complexType> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="NewDataSet"> 
*   &lt;complexType> 
*    &lt;complexContent> 
*    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*     &lt;choice maxOccurs="unbounded" minOccurs="0"> 
*     &lt;element name="Table"> 
*      &lt;complexType> 
*      &lt;complexContent> 
*       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*       &lt;sequence> 
*        &lt;element name="Book" type="{http://www.w3.org/2001/XMLSchema}byte"/> 
*        &lt;element name="BookTitle" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*       &lt;/sequence> 
*       &lt;/restriction> 
*      &lt;/complexContent> 
*      &lt;/complexType> 
*     &lt;/element> 
*     &lt;/choice> 
*    &lt;/restriction> 
*    &lt;/complexContent> 
*   &lt;/complexType> 
*   &lt;/element> 
*  &lt;/sequence> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "newDataSet" 
}) 
@XmlRootElement(name = "string") 
public class Book { 

    @XmlElement(name = "NewDataSet", required = true) 
    protected Book.NewDataSet newDataSet; 

    /** 
    * Gets the value of the newDataSet property. 
    * 
    * @return 
    *  possible object is 
    *  {@link Book.NewDataSet } 
    *  
    */ 
    public Book.NewDataSet getNewDataSet() { 
     return newDataSet; 
    } 

    /** 
    * Sets the value of the newDataSet property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link Book.NewDataSet } 
    *  
    */ 
    public void setNewDataSet(Book.NewDataSet value) { 
     this.newDataSet = value; 
    } 


    /** 
    * <p>Java class for anonymous complex type. 
    * 
    * <p>The following schema fragment specifies the expected content contained within this class. 
    * 
    * <pre> 
    * &lt;complexType> 
    * &lt;complexContent> 
    *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *  &lt;choice maxOccurs="unbounded" minOccurs="0"> 
    *   &lt;element name="Table"> 
    *   &lt;complexType> 
    *    &lt;complexContent> 
    *    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *     &lt;sequence> 
    *     &lt;element name="Book" type="{http://www.w3.org/2001/XMLSchema}byte"/> 
    *     &lt;element name="BookTitle" type="{http://www.w3.org/2001/XMLSchema}string"/> 
    *     &lt;/sequence> 
    *    &lt;/restriction> 
    *    &lt;/complexContent> 
    *   &lt;/complexType> 
    *   &lt;/element> 
    *  &lt;/choice> 
    *  &lt;/restriction> 
    * &lt;/complexContent> 
    * &lt;/complexType> 
    * </pre> 
    * 
    * 
    */ 
    @XmlAccessorType(XmlAccessType.FIELD) 
    @XmlType(name = "", propOrder = { 
     "table" 
    }) 
    public static class NewDataSet { 

     @XmlElement(name = "Table") 
     protected List<Book.NewDataSet.Table> table; 

     /** 
     * Gets the value of the table property. 
     * 
     * <p> 
     * This accessor method returns a reference to the live list, 
     * not a snapshot. Therefore any modification you make to the 
     * returned list will be present inside the JAXB object. 
     * This is why there is not a <CODE>set</CODE> method for the table property. 
     * 
     * <p> 
     * For example, to add a new item, do as follows: 
     * <pre> 
     * getTable().add(newItem); 
     * </pre> 
     * 
     * 
     * <p> 
     * Objects of the following type(s) are allowed in the list 
     * {@link Book.NewDataSet.Table } 
     * 
     * 
     */ 
     public List<Book.NewDataSet.Table> getTable() { 
      if (table == null) { 
       table = new ArrayList<Book.NewDataSet.Table>(); 
      } 
      return this.table; 
     } 


     /** 
     * <p>Java class for anonymous complex type. 
     * 
     * <p>The following schema fragment specifies the expected content contained within this class. 
     * 
     * <pre> 
     * &lt;complexType> 
     * &lt;complexContent> 
     *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
     *  &lt;sequence> 
     *   &lt;element name="Book" type="{http://www.w3.org/2001/XMLSchema}byte"/> 
     *   &lt;element name="BookTitle" type="{http://www.w3.org/2001/XMLSchema}string"/> 
     *  &lt;/sequence> 
     *  &lt;/restriction> 
     * &lt;/complexContent> 
     * &lt;/complexType> 
     * </pre> 
     * 
     * 
     */ 
     @XmlAccessorType(XmlAccessType.FIELD) 
     @XmlType(name = "", propOrder = { 
      "book", 
      "bookTitle" 
     }) 
     public static class Table { 

      @XmlElement(name = "Book") 
      protected byte book; 
      @XmlElement(name = "BookTitle", required = true) 
      protected java.lang.String bookTitle; 

      /** 
      * Gets the value of the book property. 
      * 
      */ 
      public byte getBook() { 
       return book; 
      } 

      /** 
      * Sets the value of the book property. 
      * 
      */ 
      public void setBook(byte value) { 
       this.book = value; 
      } 

      /** 
      * Gets the value of the bookTitle property. 
      * 
      * @return 
      *  possible object is 
      *  {@link java.lang.String } 
      *  
      */ 
      public java.lang.String getBookTitle() { 
       return bookTitle; 
      } 

      /** 
      * Sets the value of the bookTitle property. 
      * 
      * @param value 
      *  allowed object is 
      *  {@link java.lang.String } 
      *  
      */ 
      public void setBookTitle(java.lang.String value) { 
       this.bookTitle = value; 
      } 

     } 

    } 

} 

Ma classe package-info est:

// 
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// 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: 2017.01.25 at 03:45:20 PM GMT 
// 

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.webserviceX.NET", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 
package filesgeneratedfromxml; 

Et ma classe ObjectFactory est:

// 
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// 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: 2017.01.25 at 03:45:20 PM GMT 
// 


package filesgeneratedfromxml; 

import javax.xml.bind.annotation.XmlRegistry; 


/** 
* This object contains factory methods for each 
* Java content interface and Java element interface 
* generated in the filesgeneratedfromxml package. 
* <p>An ObjectFactory allows you to programatically 
* construct new instances of the Java representation 
* for XML content. The Java representation of XML 
* content can consist of schema derived interfaces 
* and classes representing the binding of schema 
* type definitions, element declarations and model 
* groups. Factory methods for each of these are 
* provided in this class. 
* 
*/ 
@XmlRegistry 
public class ObjectFactory { 


    /** 
    * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: filesgeneratedfromxml 
    * 
    */ 
    public ObjectFactory() { 
    } 

    /** 
    * Create an instance of {@link Book.NewDataSet.Table } 
    * 
    */ 
    public Book.NewDataSet.Table createStringNewDataSetTable() { 
     return new Book.NewDataSet.Table(); 
    } 

    /** 
    * Create an instance of {@link Book } 
    * 
    */ 
    public Book createString() { 
     return new Book(); 
    } 

    /** 
    * Create an instance of {@link Book.NewDataSet } 
    * 
    */ 
    public Book.NewDataSet createStringNewDataSet() { 
     return new Book.NewDataSet(); 
    } 

} 

Toute idée pourquoi je reçois l'erreur?

+0

Pourquoi est-ce que vous vous démasquez dans Book? Je m'attendais à ce que NewDataSet. Votre annotation XmlRootElement est une chaîne, mais elle doit être NewDataset. Je suis très confus au sujet de la hiérarchie dans votre classe-livre ... – IQV

+0

Salut. C'est déjà réparé. Il suffit d'ajouter l'en-tête "" et le pied de page "" à la chaîne renvoyée par le service web "bookTitles". Merci de votre aide! – bentakayze

+0

@IQV lorsque j'appelle le webservice via webbrowser, le xml retourné contient l'élément avec l'espace de noms, mais lors de l'appel du client, il suffit de retourner sans élément . Aucune idée pourquoi. – bentakayze

Répondre

1

Il est déjà fixé. Juste dû ajouter en-tête

<string xmlns=\"webserviceX.NET\"> 

et pied de page

</string>

à la chaîne renvoyée par le WebService "bookTitles". Merci de votre aide!

0

Essayez d'utiliser JAXBElement lorsque vous obtenez élément inattendu:

public static Car unmarshal(File file) throws JAXBException { 
    JAXBContext jaxbContext = JAXBContext.newInstance(Book.class); 
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 

    JAXBElement<Book> root = jaxbUnmarshaller.unmarshal(new StreamSource(
      file), Book.class); 
    Book book= root.getValue(); 

    return book; 
} 
+1

Salut. C'est déjà réparé. Il suffit d'ajouter l'en-tête "" et le pied de page "" à la chaîne retournée par le webservice "bookTitles". Merci de votre aide! – bentakayze