如何使用 apache camel 而不是 xml 记录列表将平面文件中的单个记录转换为一个 xml
How to transform individual record in flat file to one xml using apache camel instead of list of xml records
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
@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;
}
输出
* File.xml*
<?xml version='1.0' encoding='UTF-8'?>
<list>
<com.john.Test>
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>2346</UOMCOD>
</com.john.Test>
<com.john.Test>
<TRNNAM>55158</TRNNAM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>3454</UOMCOD>
</com.john.Test>
</list>
预期输出
fileOne.xml
<?xml version='1.0' encoding='UTF-8'?>
<Test>
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>2346</UOMCOD>
</Test>
fileTwo.xml
<?xml version='1.0' encoding='UTF-8'?>
<Test>
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>3454</UOMCOD>
</Test>
我能够生成 xml 文件,其中所有字段都在单个 file.I 中,想要单个 xml 文件中的单个记录。任何人都可以帮助 me.Also 输出文件根元素中的标签名称它正在生成 class.
的包名称
您或许可以使用 split()
分别处理 csv 记录中的每一行。
from(SOURCE_INPUT_PATH).
.split().tokenize(System.lineSeparator())
unmarshal(bindyFixed).
marshal().
xstream().
to(SOURCE_OUTPUT_PATH).log("Finished Transformation").end();
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
@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;
}
输出
* File.xml*
<?xml version='1.0' encoding='UTF-8'?>
<list>
<com.john.Test>
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>2346</UOMCOD>
</com.john.Test>
<com.john.Test>
<TRNNAM>55158</TRNNAM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>3454</UOMCOD>
</com.john.Test>
</list>
预期输出
fileOne.xml
<?xml version='1.0' encoding='UTF-8'?>
<Test>
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>2346</UOMCOD>
</Test>
fileTwo.xml
<?xml version='1.0' encoding='UTF-8'?>
<Test>
<ALT_NUM>55158</ALT_NUM>
<PRTNUM>11901</PRTNUM>
<UOMCOD>3454</UOMCOD>
</Test>
我能够生成 xml 文件,其中所有字段都在单个 file.I 中,想要单个 xml 文件中的单个记录。任何人都可以帮助 me.Also 输出文件根元素中的标签名称它正在生成 class.
的包名称您或许可以使用 split()
分别处理 csv 记录中的每一行。
from(SOURCE_INPUT_PATH).
.split().tokenize(System.lineSeparator())
unmarshal(bindyFixed).
marshal().
xstream().
to(SOURCE_OUTPUT_PATH).log("Finished Transformation").end();