无法使用 Camel JacksonXML 解组 XML

Unable to unmarshal XML using Camel JacksonXML

我正在尝试简单地解组一个 XML 文件,如下所示:

<?xml version="1.0" encoding = "UTF-8" ?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <products>
        <product>
            <facet type="string" elementType="string" name="Weight (g)"><![CDATA[210]]></facet>
        </product>
    </products>
</feed>

我知道了 类:

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = "feed")
@XmlAccessorType(XmlAccessType.FIELD)
public class Feed {
    private Products products;
}

子类 Products:

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Products {
    private List<Product> products;
}

子类 Product:

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Product {

    @XmlElement(name = "facet")
    private List<Facet> facet;
}

最后 Facet:

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Facet {
    @XmlElement(name = "name")
    private String name;

    @XmlElement(name = "type")
    private String type;

    @XmlElement(name = "elementType")
    private String elementType;

    private String content;
}

我写的unmarshall的骆驼路线如下:

@Component
public class XMLSplitterRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("file:src/main/resources/files/xml").routeId("xmlUmarshaller")
            .log("body: ${body}")
            .unmarshal().jacksonXml(Products.class)
                .log("The unmarshalled object is ${body}")
            .marshal().json()
            .to("activemq:json-marshal-queue");
    }
}

但我不断收到以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Unexpected non-whitespace text ('210' in Array context: should not occur (or should be handled)
 at [Source: (BufferedInputStream); line: 29, column: 96] (through reference chain: com.sammy.model.Products["products"]->java.util.ArrayList[0]->com.sammy.model.Product["facet"])

Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected non-whitespace text ('210' in Array context: should not occur (or should be handled)
 at [Source: (BufferedInputStream); line: 29, column: 96]

这意味着,XML文件的cdata中的值似乎不知道如何处理!我到处都看过,但没有看到任何可能的解决方案。请帮忙!!!

根据 Nicolas Filotto 的好建议,我通过首先将我的 XML 转换为 XSD 然后使用 xjc 生成 POJO 来修复我的映射。 对于 Camel 解组过程,我将其从 jacksonXML 更改为使用 Jaxb 转换器。

@Component
public class XMLSplitterRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        DataFormat jaxb = new JaxbDataFormat("com.sammy.model");

        from("file:src/main/resources/files/xml").routeId("xmlSplitter")
            .log("body: ${body}")
            .unmarshal(jaxb)
                .log("The unmarshalled object is ${body}")
    }
}

这现在很有魅力!!!