无法使用 Camel JacksonXML 解组 XML

问题描述 投票:0回答:2

我正在尝试简单地解组 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的camel路线如下:

@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 中的值!我到处寻找但没有看到任何潜在的解决方案。请帮忙!!!

spring-boot apache-camel unmarshalling jackson-dataformat-xml
2个回答
0
投票

根据 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}")
    }
}

现在这就像一个魅力!!!


0
投票

我也遇到过同样的问题。 我通过将 @JsonProperty@JacksonXmlElementWrapper(useWrapping = false) 添加到我的列表中解决了这个问题。 这是类的示例:

@Getter
@JacksonXmlRootElement(localName = "users")
private static class Users {
    @JacksonXmlElementWrapper(useWrapping = false)
    @JsonProperty("user")
    List<User> users;

    @Getter
    @JacksonXmlRootElement(localName = "user")
    public static class User {
        @JacksonXmlProperty(isAttribute = true)
        private String key;

        @JacksonXmlText
        private String value;
    }
}

这对于 xml 非常有用,例如:

<users>
   <user key="a">111</user>
   <user key="b">222</user>
   <user key="c">333</user>
</users>
© www.soinside.com 2019 - 2024. All rights reserved.