java - 如何在java spring-boot上将xml转换为对象

标签 java xml object spring-boot

你好,我正在尝试用对象数量转换 xml,我得到一个 错误消息:文档中跟在根元素之后的标记必须格式正确。

XML:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="test.example.com">
  <Item>
    <ItemKey>1111</ItemKey>
    <Start>2/10/2017</Start>
    <customNumber>12</customNumber>
    <End>2/10/2018</End>
    <Account>2221111</Account>
    <Name>John</Name>
    <Note>GOOD</Note>
    <CodeNo>4444-1</CodeNo>
    <Source>www.cnn.com</Source>
  </Item>
  <Item>
    <ItemKey>2222</ItemKey>
    <Start>2/10/2017</Start>
    <customNumber>75</customNumber>
    <End>2/10/2018</End>
    <Account>3333111</Account>
    <Name>Smith</Name>
    <Note>NOT GOOD</Note>
    <CodeNo>4444-2</CodeNo>
    <Source>www.fox.com</Source>
  </Item>
</string>

模型类:

package example.models;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Item")
public class Model {

private String CodeNo;

private String ItemKey;

private String Start;

private String End;

private String Account;

private String Name;

private String Note;

...(gets and sets)

主要代码:

StringReader reader = new StringReader(response);
String response = restTemplate.getForObject(url, String.class);
...

 JAXBContext jaxbContext = JAXBContext.newInstance(Model.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Model recordes = (Model) unmarshaller.unmarshal(reader);

解码异常: 文档中跟在根元素之后的标记必须格式正确。

xml 只有一项代码有效。

我缺少什么并且需要做什么才能无误地获取元素(项目)对象列表?

最佳答案

在 XML 文件中,您的根元素是 <string xmlns="test.example.com">标记,以便更正 XML 或更正 Model 类以使其正常工作。

要了解有关错误的更多信息,请检查此 How to fix error: The markup in the document following the root element must be well-formed

编辑

您可以使用此工具生成 POJO:http://pojo.sodhanalibrary.com

下面是上述 XML 的 POJO 类:

public class MyXML
{
    private String string; // Change the class as String is Wrapper class

    public String getString ()
    {
        return string;
    }

    public void setString (String string)
    {
        this.string = string;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [string = "+string+"]";
    }
}

字符串类:

public class String // Change this className as String is Wrapper class in java
{
    private Item[] Item;

    private String xmlns;

    public Item[] getItem ()
    {
        return Item;
    }

    public void setItem (Item[] Item)
    {
        this.Item = Item;
    }

    public String getXmlns ()
    {
        return xmlns;
    }

    public void setXmlns (String xmlns)
    {
        this.xmlns = xmlns;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [Item = "+Item+", xmlns = "+xmlns+"]";
    }
}

元素类

public class Item
{
    private String Name;

    private String Source;

    private String End;

    private String CodeNo;

    private String Start;

    private String Account;

    private String ItemKey;

    private String Note;

    private String customNumber;

    public String getName ()
    {
        return Name;
    }

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

    public String getSource ()
    {
        return Source;
    }

    public void setSource (String Source)
    {
        this.Source = Source;
    }

    public String getEnd ()
    {
        return End;
    }

    public void setEnd (String End)
    {
        this.End = End;
    }

    public String getCodeNo ()
    {
        return CodeNo;
    }

    public void setCodeNo (String CodeNo)
    {
        this.CodeNo = CodeNo;
    }

    public String getStart ()
    {
        return Start;
    }

    public void setStart (String Start)
    {
        this.Start = Start;
    }

    public String getAccount ()
    {
        return Account;
    }

    public void setAccount (String Account)
    {
        this.Account = Account;
    }

    public String getItemKey ()
    {
        return ItemKey;
    }

    public void setItemKey (String ItemKey)
    {
        this.ItemKey = ItemKey;
    }

    public String getNote ()
    {
        return Note;
    }

    public void setNote (String Note)
    {
        this.Note = Note;
    }

    public String getCustomNumber ()
    {
        return customNumber;
    }

    public void setCustomNumber (String customNumber)
    {
        this.customNumber = customNumber;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [Name = "+Name+", Source = "+Source+", End = "+End+", CodeNo = "+CodeNo+", Start = "+Start+", Account = "+Account+", ItemKey = "+ItemKey+", Note = "+Note+", customNumber = "+customNumber+"]";
    }
}

关于java - 如何在java spring-boot上将xml转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50461827/

相关文章:

java - 在 Guice 中配置返回泛型类型的 Provider

java - (错误)GlassFish : publishModule kind= 3 deltaKind=2 1 WebApp

java - 如何引用在不同类的 main 方法中声明的对象的属性

java - Spring MVC : Complex object as GET @RequestParam

java - 使用 JDBC 从 ORACLE 获取 CLOB 数据时出现 NotSerializedException

android - 如何从项目资源android中解析一个xml

python - 使用 lxml 和 iterparse() 解析一个大的 (+- 1Gb) XML 文件

javascript - 将 XML 解析移至服务器端

javascript - `throw new Error` 和 `throw someObject` 和有什么区别?

javascript - 使用 JS 对象和函数