如何使用 camel-xstream 定义自定义命名空间和标记别名
How to define custom namespace and tag alias using camel-xstream
我正在尝试将竖线分隔的字符串转换为 xml,效果很好。但是,我没有得到下面显示的实际输出。在我的输出中,父标记与包名称一起生成,它也没有生成我想要的命名空间。谁能帮助我如何将平面文件转换为我期望的确切输出。
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();
}
}
}
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.xml";
public void addRoutesToCamelContext(CamelContext context) throws Exception {
context.addRoutes(new RouteBuilder() {
public void configure() {
try {
DataFormat bindyFixed = new BindyCsvDataFormat(Test.class);
from(SOURCE_INPUT_PATH).
unmarshal(bindyFixed).
marshal().
xstream().
to(SOURCE_OUTPUT_PATH).log("Finished Transformation").end();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Source.txt
55158|11901|2346
55158|11101|3454
Test.java
package com.john;
@CsvRecord(separator = "\|",skipField = true,name = "Test")
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;
}
输出
test.xml
<?xml version='1.0' encoding='UTF-8'?>
<com.john.Test>
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>2346</UOMCOD>
</com.john.Test>
预期输出
test.xml
<?xml version='1.0' encoding='UTF-8'?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>2346</UOMCOD>
</Test>
您可以使用 aliases
数据格式 属性:
定义类型别名
XStreamDataFormat xstream = new XStreamDataFormat();
xstream.setAliases(Collections.singletonMap("Test", Test.class.getCanonicalName()));
//...
.marshal(xstream)
为了定义自定义命名空间,请在您的实体中使用 @XStreamAsAttribute
and @XStreamAlias
:
public class Test {
@XStreamAsAttribute
@XStreamAlias("xmlns:xsi")
private final String xmlns = "http://www.w3.org/2001/XMLSchema-instance";
//...
}
我正在尝试将竖线分隔的字符串转换为 xml,效果很好。但是,我没有得到下面显示的实际输出。在我的输出中,父标记与包名称一起生成,它也没有生成我想要的命名空间。谁能帮助我如何将平面文件转换为我期望的确切输出。
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();
}
}
}
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.xml";
public void addRoutesToCamelContext(CamelContext context) throws Exception {
context.addRoutes(new RouteBuilder() {
public void configure() {
try {
DataFormat bindyFixed = new BindyCsvDataFormat(Test.class);
from(SOURCE_INPUT_PATH).
unmarshal(bindyFixed).
marshal().
xstream().
to(SOURCE_OUTPUT_PATH).log("Finished Transformation").end();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Source.txt
55158|11901|2346
55158|11101|3454
Test.java
package com.john;
@CsvRecord(separator = "\|",skipField = true,name = "Test")
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;
}
输出
test.xml
<?xml version='1.0' encoding='UTF-8'?>
<com.john.Test>
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>2346</UOMCOD>
</com.john.Test>
预期输出
test.xml
<?xml version='1.0' encoding='UTF-8'?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>2346</UOMCOD>
</Test>
您可以使用 aliases
数据格式 属性:
XStreamDataFormat xstream = new XStreamDataFormat();
xstream.setAliases(Collections.singletonMap("Test", Test.class.getCanonicalName()));
//...
.marshal(xstream)
为了定义自定义命名空间,请在您的实体中使用 @XStreamAsAttribute
and @XStreamAlias
:
public class Test {
@XStreamAsAttribute
@XStreamAlias("xmlns:xsi")
private final String xmlns = "http://www.w3.org/2001/XMLSchema-instance";
//...
}