gpt4 book ai didi

java - 如何处理xml标签中的下划线

转载 作者:行者123 更新时间:2023-12-01 23:50:08 26 4
gpt4 key购买 nike

如何处理 xml 标签中的下划线。这是我正在工作的代码。它为我生成带有双下划线(“__”)而不是单下划线(“_”)的 xml 标记。谁能帮我解决这个问题。

ConverterRoute.Java

public class ConverterRoute implements RoutesBuilder {

private static final String SOURCE_INPUT_PATH = "file://inbox?fileName=Source.txt";

private static final String SOURCE_OUTPUT_PATH = "file://outbox?fileName=file_$simple{date:now:yyyyMMddHHmmssSSS}.xml";

public void addRoutesToCamelContext(CamelContext context) throws Exception {

context.addRoutes(new RouteBuilder() {
public void configure() {
try {
DataFormat bindyFixed = new BindyCsvDataFormat(Test.class);

XStreamDataFormat xStreamDataFormat = new XStreamDataFormat();
xStreamDataFormat.setAliases(Collections.singletonMap("TEST_INB",Test.class.getCanonicalName()));

from(SOURCE_INPUT_PATH).
split().tokenize(System.lineSeparator()).
unmarshal(bindyFixed).
marshal(xStreamDataFormat).
to(SOURCE_OUTPUT_PATH).log("Finished Transformation").end();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

CamelConfig.java

@Component
public class CamelConfig extends RouteBuilder {
@Override
public void configure() throws Exception {
try {
CamelContext context = new DefaultCamelContext();
ConverterRoute route = new ConverterRoute();
route.addRoutesToCamelContext(context);
context.start();
Thread.sleep(5000);
context.stop();

} catch (Exception exe) {
exe.printStackTrace();
}
}
}

Test.java

@CsvRecord(separator = "\\|",skipField = true,name = "TEST_INB")
public class Test {

@DataField(pos = 1,name = "ALT_NUM")
private BigDecimal ALT_NUM;

@DataField(pos = 2,name = "PRTNUM")
private BigDecimal PRTNUM;

@DataField(pos = 3,name = "UOMCOD")
private Integer UOMCOD;

}

Source.txt

55158|11901|2346
55158|11101|3454

输出

File.xml

<?xml version='1.0' encoding='UTF-8'?>    
<TEST__INB>
<ALT__NUM>55158</ALT__NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>2346</UOMCOD>
</TEST__INB>

预期输出

File.xml

<?xml version='1.0' encoding='UTF-8'?>    
<TEST_INB>
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>2346</UOMCOD>
</TEST_INB>

最佳答案

XStream 在生成 XML 时在内部使用下划线来转义字符。

查看常见问题解答: http://x-stream.github.io/faq.html#XML_double_underscores

您可以尝试使用自定义驱动程序(而不是默认驱动程序):

XmlFriendlyNameCoder nameCoder = new XmlFriendlyNameCoder("_-", "_");  
Dom4JDriver myCustomDriver = new Dom4JDriver(nameCoder);
...
XStreamDataFormat xStreamDataFormat = new XStreamDataFormat();
xStreamDataFormat.setDriver(myCustomDriver);

或者,如果不起作用,可以尝试使用其他备用驱动程序:

DomDriver myCustomDriver = new DomDriver("UTF_8", new NoNameCoder());

希望这有效!

关于java - 如何处理xml标签中的下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58222331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com