Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jackson从XSD生成的选项反序列化XML时出错_Java_Xml_Serialization_Xsd_Jackson - Fatal编程技术网

Java 使用Jackson从XSD生成的选项反序列化XML时出错

Java 使用Jackson从XSD生成的选项反序列化XML时出错,java,xml,serialization,xsd,jackson,Java,Xml,Serialization,Xsd,Jackson,我目前正在尝试将传入的XML反序列化为从XSD生成的对象。遗憾的是,在尝试反序列化到生成的choice元素时似乎出现了问题。我已经尝试了很多东西,我甚至刚刚实现了最基本的示例。每次我都会遇到同样的异常。实施情况如下: public static void main(final String[] args) throws IOException { final String xml = "<Foo> \n" + " &

我目前正在尝试将传入的XML反序列化为从XSD生成的对象。遗憾的是,在尝试反序列化到生成的choice元素时似乎出现了问题。我已经尝试了很多东西,我甚至刚刚实现了最基本的示例。每次我都会遇到同样的异常。实施情况如下:

public static void main(final String[] args) throws IOException {
    final String xml =
            "<Foo>    \n" +
            "    <A> 1 </A>\n" +
            "    <B> 2.5 </B>\n" +
            "</Foo>";

    final XmlMapper xmlMapper = new XmlMapper();
    final Foo foo = xmlMapper.readValue(xml, Foo.class);
}

public static class Foo {

    @XmlElementRefs({
            @XmlElementRef(name = "A", type = Integer.class),
            @XmlElementRef(name = "B", type = Float.class)
    })
    public List items;
}
看起来他只是在寻找一个具有给定名称的字段,试图将值设置为这个,而这个值当然不存在

我在这里看到过类似的问题,答案是您应该添加一个配置来忽略未知,这不是我想要的。我希望列表最终包含两个元素,即两个数字


我还看到了一个名为Simplify的扩展,它为每个choice元素生成一个列表字段。在我的用例中,我实际上更希望将all添加到单个列表中。

您链接到的
@xmlements
的Javadoc基于JAXB。例如,直接使用JAXB解析(解组)和序列化(编组)该XML:

public static void main(final String[] args)
        throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Foo.class);

    String xml = 
            "<Foo>    \n" + 
            "    <A> 1 </A>\n" +
            "    <B> 2.5 </B>\n" +
            "</Foo>";

    StringReader sr = new StringReader(xml);
    Foo foo = (Foo) jaxbContext.createUnmarshaller().unmarshal(sr);
    System.out.println(foo.items);

    StringWriter sw = new StringWriter();
    jaxbContext.createMarshaller().marshal(foo, sw);
    System.out.println(sw);
}

@XmlRootElement(name = "Foo")
public static class Foo {

    @XmlElements({ 
        @XmlElement(name = "A", type = Integer.class),
        @XmlElement(name = "B", type = Float.class) 
    })
    public List items;
}
。。。退回这个

<Foo><items>1</items><items><B>2.5</B></items></Foo>

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "A" (class so.xmllist.XmlElementsTestBasic$Foo), not marked as ignorable (one known property: "items"])
 at [Source: (StringReader); line: 2, column: 15] (through reference chain: so.xmllist.XmlElementsTestBasic$Foo["A"])
12.5
线程“main”com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException中的异常:未识别的字段“A”(class so.xmllist.xmlementstestbasic$Foo),未标记为可忽略(一个已知属性:“items”])
在[Source:(StringReader);第2行,第15列](通过引用链:so.xmllist.xmlementstestbasic$Foo[“A”])

您链接到的
@xmlements
的Javadoc基于JAXB。例如,直接使用JAXB解析(解组)和序列化(编组)该XML:

public static void main(final String[] args)
        throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Foo.class);

    String xml = 
            "<Foo>    \n" + 
            "    <A> 1 </A>\n" +
            "    <B> 2.5 </B>\n" +
            "</Foo>";

    StringReader sr = new StringReader(xml);
    Foo foo = (Foo) jaxbContext.createUnmarshaller().unmarshal(sr);
    System.out.println(foo.items);

    StringWriter sw = new StringWriter();
    jaxbContext.createMarshaller().marshal(foo, sw);
    System.out.println(sw);
}

@XmlRootElement(name = "Foo")
public static class Foo {

    @XmlElements({ 
        @XmlElement(name = "A", type = Integer.class),
        @XmlElement(name = "B", type = Float.class) 
    })
    public List items;
}
。。。退回这个

<Foo><items>1</items><items><B>2.5</B></items></Foo>

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "A" (class so.xmllist.XmlElementsTestBasic$Foo), not marked as ignorable (one known property: "items"])
 at [Source: (StringReader); line: 2, column: 15] (through reference chain: so.xmllist.XmlElementsTestBasic$Foo["A"])
12.5
线程“main”com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException中的异常:未识别的字段“A”(class so.xmllist.xmlementstestbasic$Foo),未标记为可忽略(一个已知属性:“items”])
在[Source:(StringReader);第2行,第15列](通过引用链:so.xmllist.xmlementstestbasic$Foo[“A”])

对于反序列化,您可以使用setter方法指示XmlMapper:

public class Foo {

    public List items;

    public Foo() {
        items = new ArrayList();
    }

    @JacksonXmlProperty(localName = "A")
    public void setA(Integer a) {
        items.add(a);
    }

    @JacksonXmlProperty(localName = "B")
    public void setB(Double b) {
        items.add(b);
    }
}
召唤

final XmlMapper xmlMapper = new XmlMapper();
final Foo foo = xmlMapper.readValue(xml, Foo.class);
将提供:


对于反序列化,您可以使用setter方法指示XmlMapper:

public class Foo {

    public List items;

    public Foo() {
        items = new ArrayList();
    }

    @JacksonXmlProperty(localName = "A")
    public void setA(Integer a) {
        items.add(a);
    }

    @JacksonXmlProperty(localName = "B")
    public void setB(Double b) {
        items.add(b);
    }
}
召唤

final XmlMapper xmlMapper = new XmlMapper();
final Foo foo = xmlMapper.readValue(xml, Foo.class);
将提供:


非常感谢您的回答!直接使用JAXB还提供了我在项目中需要的结果。你能猜到为什么即使在注册模块中使用Jackson也不起作用吗?非常感谢你的回答!直接使用JAXB还提供了我在项目中需要的结果。你能猜到为什么即使在注册模块中使用Jackson也不起作用吗?是的,这是可行的,尽管我正在使用xjc自动生成类,并且我不想手动修补这些类。所以,除非很容易配置,否则我不想这样做。但是谢谢你的回答!是的,这是可行的,尽管我正在使用xjc自动生成类,并且我不想手动修补这些类。所以,除非很容易配置,否则我不想这样做。但是谢谢你的回答!