XML 反序列化 java XSTream 问题

发布于 2024-09-30 07:23:31 字数 3218 浏览 4 评论 0原文

这是 XML

<?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>  

这是 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;
    }
}

这是解组器逻辑:

@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;
    }  

我收到以下异常:

[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

This is XML

<?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>  

This is 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;
    }
}

This is unmarshaller logic:

@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;
    }  

I am getting following exception :

[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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻梦旅人 2024-10-07 07:23:31

XML 文件的格式不正确。它不能有两个单独的根元素,在您的情况下是 。两个 person 元素周围必须有一个顶级元素,例如

编辑:
为了格式良好,它需要看起来像这样

<persons>
  <person>
    .
    .
  </person>
  <person>
    .
    .
  </person>
<persons>

The XML file is not well-formed. It cannot have two separate root elements, in your case <person>. There must be a single top-level element, such as <persons>, around both person elements.

EDIT:
In order to be well-formed, it would need to look like this

<persons>
  <person>
    .
    .
  </person>
  <person>
    .
    .
  </person>
<persons>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文