2010-11-01 19 views
0

Ceci est XMLXML numéro désérialisation java Xstream

<?xml version="1.0" encoding="UTF-8"?> 
<person> 
    <fullname>Guilherme</fullname> 
    <age>10</age> 
    <address>address,address,address,address,</address> 
</person> 

<person> 
    <fullname>Guilherme</fullname> 
    <age>10</age> 
    <address>address,address,address,address,</address> 
</person> 

Ceci est POJO,

public class Person { 

    private String name; 
    private int age; 
    private String address; 

    public Person(String name) { 
     this.name = name; 
    } 

    public Person(String name, int age, String address) { 
     this.name = name; 
     this.age = age; 
     this.address = address; 
    } 

    public Person() { 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 

Ceci est logique unmarshaller:

@Override 
    public Object unmarshal(HierarchicalStreamReader reader, 
      UnmarshallingContext context) { 
     List<Person> persons = new ArrayList<Person>(); 
     while (reader.hasMoreChildren()) { 

      Person person = new Person(); 
      reader.moveDown(); 
      System.out.println("+" + reader.getValue()); 
      person.setName(reader.getValue()); 
      reader.moveUp(); 
      reader.moveDown(); 
      System.out.println("+" + reader.getValue()); 
      person.setAge(Integer.parseInt(reader.getValue())); 
      reader.moveUp(); 
      reader.moveDown(); 
      System.out.println("+" + reader.getValue()); 
      person.setAddress(reader.getValue()); 
      reader.moveUp(); 
      persons.add(person); 
     } 
     return persons; 
    } 

Je reçois exception suivante:

[Fatal Error] :8:2: The markup in the document following the root element must be well-formed. 
Exception in thread "main" com.thoughtworks.xstream.io.StreamException: : The markup in the document following the root element must be well-formed. 
     at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:86) 
     at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:70) 
     at com.thoughtworks.xstream.XStream.fromXML(XStream.java:861) 
     at com.mycompany.xstreamconvertersample.App.main(App.java:34) 
Caused by: org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed. 
     at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239) 
     at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283) 
     at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:79) 
     ... 3 more 
------------------------------------------------------------------------ 
[ERROR]BUILD ERROR 

Répondre

1

Le fichier XML n'est pas bien formé. Il ne peut pas avoir deux éléments racine distincts, dans votre cas <person>. Il doit y avoir un seul élément de niveau supérieur, tel que <persons>, autour des deux éléments de personne.

EDIT: Pour être bien formé, il aurait besoin de ressembler à ceci

<persons> 
    <person> 
    . 
    . 
    </person> 
    <person> 
    . 
    . 
    </person> 
<persons> 
+0

ok comment puis-je valider contre mon xsd? et pouvez-vous mettre à jour votre réponse contenant le code XML requis –

+0

Comment le code XML a-t-il été généré? À quoi ressemble le xsd? –

+0

Je veux créer xsd pour ce format particulier de XML. J'ai généré ce XML manuellement –