[英]Unmarshal xml with UTF-16 content how to remove special character from bottom of xml file
[英]Ignore ESC Special Character on XML Unmarshal Java Spring
我有一個無法更改的第三方 xml 文件。 XML 中的字段之一在字段值的中間有 ESC 字符。 我想添加一個 unmarshal 屬性,它只是忽略它找到的所有 ESC 字符字段。
是否有可能做到這一點? 如果是,如何?
這是我閱讀的 stream ,用於解組我的自定義 class TransactionType
。 而且,后者是不能改變的。
@Bean
public MultiResourceItemReader<TransactionType> customItemReader() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setCheckForXmlRootElement(false);
marshaller.setClassesToBeBound(TransactionType.class);
marshaller.supports(TransactionType.class);
StaxEventItemReader<TransactionType> posLogStaxEventItemReader = new StaxEventItemReader<>();
posLogStaxEventItemReader.setUnmarshaller(marshaller);
posLogStaxEventItemReader.setFragmentRootElementName("{http://www.nrf-arts.org/IXRetail/namespace/}Transaction");
MultiResourceItemReader<TransactionType> transactionReader = new MultiResourceItemReader<>();
transactionReader.setDelegate(posLogStaxEventItemReader);
return transactionReader;
}
我有很多不能忽略的開發限制,所以如果可以忽略這個字符,我只能在創建解組器時這樣做。
現在解組中斷,因為<ID><![CDATA[888711173110]]></ID>
包含無效字符。 我想忽略它,好像 CDATA 塊包含888711173110
我設法通過將文件讀取為字節,將其轉換為字符串,使用.replace()
刪除不需要的字符,將其轉換回字節,將其轉換為輸入 stream 來實現我想要的。
不是優雅的解決方案,但它有效。
解組器已經期望構造良好的 XML,因此我無法在文件讀取期間實現我想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.