有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring批量写入XML文件转义字符串

我不熟悉Spring和Spring批处理,并决定使用它将一组xml文件导出到需要从数据库中获取xml文件的第三方。我的问题是,我正在使用StatxeventitemWriter将对象转换为xml,但我得到如下输出:

<categories>&lt;category&gt;First Aid / Safety / Medical&lt;/category&gt;</categories>

当我想

<categories><category>First Aid / Safety / Medical</category></categories>

我的作者被定义为:

@Bean(name="OG-ProductWriter")
ItemWriter<ProductXml> productWriter() {
    StaxEventItemWriter<ProductXml> writer = new StaxEventItemWriter<>();
    writer.setResource(new FileSystemResource(exportDir + "\\product-feed.xml"));
    writer.setRootTagName("products");
    // they require ascii
    writer.setEncoding("ASCII");
    writer.setMarshaller(getMarshaller());

    return writer;
}

马歇尔先生:

@Bean(name="OG-Unmarshaller")
public Marshaller getMarshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    Map<String, Object> props = new HashMap<>();
    //these do nothing
    props.put("jaxb.formatted.output", true);
    props.put("com.sun.xml.bind.marshaller.CharacterEscapeHandler", new XmlCharacterHandler());
    marshaller.setClassesToBeBound(ProductXml.class);
    //this causes exception
    //marshaller.setJaxbContextProperties(props);

    marshaller.setMarshallerProperties(props);
    return marshaller;
}

我被要求提供更多关于我的配置的信息。我没有使用XML配置,而是通过java配置来完成。读取器只是一个JPA存储库读取器,处理器获取JPA对象并将其复制到productXml产品对象。我可以包括更多,但我不确定还有什么与我所问的问题相关。 该步骤定义为

@Bean(name="OG-ProductExportStep")
Step ogProductExport() {
    return stepBuilderFactory.get("ogProductFeed")
            .<Product,ProductXml>chunk(500)
            .reader(productReader())
            .processor(productProcessor())
            .writer(productWriter())
            .build();
}

我尝试过使用CastorMarshaller、XStreamMarshaller,但可能只是没有传递正确的参数。我一直在努力工作,到处寻找,只是没有找到任何解决办法。如有任何建议,将不胜感激

这是一个我尝试添加为属性的类,但它没有做任何事情:

public class XmlCharacterHandler implements CharacterEscapeHandler {
    public void escape(char[] buf, int start, int len, boolean isAttValue,
                   Writer out) throws IOException {
    StringWriter buffer = new StringWriter();

    for (int i = start; i < start + len; i++) {
        buffer.write(buf[i]);
    }

    String st = buffer.toString();

    if (!st.contains("CDATA")) {
        st = buffer.toString().replace("&", "&amp;").replace("<", "&lt;")
                .replace(">", "&gt;").replace("'", "&apos;")
                .replace("\"", "&quot;");

    }
    out.write(st);
    System.out.println(st);
}

}`

我的目标如果有帮助:

@XmlRootElement(name = "product")
@XmlAccessorType(XmlAccessType.FIELD)
public @Data class ProductXml {
    @XmlElement(name = "product_id")
    private int product_id;

    //this gives the same escaping problem
    @XmlElement(name = "name")
    private String name;
    @XmlElement(name = "sku")
    private String sku;
    // cheated and just made string categories.add("<category>string</category>"). 
    // The < and > get translated to &lt; and &gt; 
    @XmlElement(name = "categories")
    private ArrayList<String> categories
    ....(the rest defined the same)

}


共 (0) 个答案