我正在努力解决如何将XML字符串转换为Java对象的问题。

我有以下代码。

public class User {
    @JacksonXmlProperty(namespace = "http://schemas.helloworld/_user/", localName = "AccountName")
    private String accountName;
    @JacksonXmlProperty(namespace = "http://schemas.helloworld/_user/", localName = "AccountStatus")
    private String accountStatus;
// getters/setters
}


和:

String xml = "<a:Users xmlns:b=\"http://schemas.helloworld/_account/\">\n" +
        "    <b:User>\n" +
        "        <b:AccountName>John Doe</b:AccountName>\n" +
        "        <b:AccountStatus>Active</b:AccountStatus>\n" +
        "\t</b:User>\n" +
        "<b:Users>";

XmlMapper xmlMapper = new XmlMapper();
User[] users = xmlMapper.readValue(xml, User[].class);


这些是我运行代码时遇到的错误:

Exception in thread "main" java.io.IOException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,56]
Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#ElementPrefixUnbound?a&a:Users
    at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.throwXmlAsIOException(StaxUtil.java:24)
    at com.fasterxml.jackson.dataformat.xml.XmlFactory._createJsonParser(XmlFactory.java:318)
    at com.fasterxml.jackson.dataformat.xml.XmlFactory._createJsonParser(XmlFactory.java:27)
    at com.fasterxml.jackson.core.JsonFactory.createJsonParser(JsonFactory.java:640)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1805)
    at test.jacksonxml.Test1.main(Test1.java:19)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,56]
Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#ElementPrefixUnbound?a&a:Users
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)
    at com.fasterxml.jackson.dataformat.xml.XmlFactory._initializeXmlReader(XmlFactory.java:378)
    at com.fasterxml.jackson.dataformat.xml.XmlFactory._createJsonParser(XmlFactory.java:316)
    ... 4 more


谢谢。

最佳答案

唯一的例外是语法错误,XML格式错误。
用户的名称空间前缀中有错别字:应为b。
而且缺少结束标记。

import java.io.IOException;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public class User {

    @JacksonXmlProperty(namespace = "http://schemas.helloworld/_user/", localName = "AccountName")
    private String accountName;
    @JacksonXmlProperty(namespace = "http://schemas.helloworld/_user/", localName = "AccountStatus")
    private String accountStatus;
    public String getAccountName() {
        return accountName;
    }
    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }
    public String getAccountStatus() {
        return accountStatus;
    }
    public void setAccountStatus(String accountStatus) {
        this.accountStatus = accountStatus;
    }

    public static void main(String[] args) {
        String xml = "<b:Users xmlns:b=\"http://schemas.helloworld/_account/\">\n" +
                "    <b:User>\n" +
                "        <b:AccountName>John Doe</b:AccountName>\n" +
                "        <b:AccountStatus>Active</b:AccountStatus>\n" +
                "\t</b:User>\n" +
                "</b:Users>";

        XmlMapper xmlMapper = new XmlMapper();
        try {
            User[] users = xmlMapper.readValue(xml, User[].class);
            System.out.println(users.length); // 1
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

08-27 23:52
查看更多