问题陈述:
我有平面文件作为输入源,其中包含长度从(0 - 80)的第一个位置的文本。如果文本长度小于 40,我必须创建一个 xml 标签。如果它大于 40,我必须将其拆分并将其附加到另一个 xml 标记。我能够转换为 xml,但我需要实现将进行长度检查并生成标签的逻辑。
INPUT - 带有管道分隔字符串的平面文件
Hey how are u and hows ur life. long time no see. how u been|LIVE|002|6315115097|IN|US||POS REPLEN|N|QUEST NUTRITION LLC
转换器路由.java
@Data
public class ConverterRoute implements RoutesBuilder {
String data;
List<String> output = new ArrayList<>();
JSONArray jsonArray = new JSONArray();
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", TEST.class.getCanonicalName()));
xStreamDataFormat.setXstreamDriver(myCustomDriver);
from("direct:sendData").
split().tokenize(System.lineSeparator()).log("Line separator ${body}").
unmarshal(bindyFixed).log("Unmarshaling ${body}").
process(new AppendAttributesProcessor()).
marshal(xStreamDataFormat).
log("Finished Transformation ${body}").process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
data = exchange.getIn().getBody(String.class);
data = data.replaceAll("[\n\r]", "");
output.add(data);
jsonArray.put(data);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
AppendAttributesProcessor.java
public class AppendAttributesProcessor implements Processor {
public void process(Exchange exchange) {
TEST appt_inb_ifd = exchange.getIn().getBody(TEST.class);
APPT_NOTE_SEG appt_note_seg = appt.getAPPT_NOTE_SEG();
appt.setTRLR_NUM(appt.getAPPT_ID());
String noteText = appt_note_seg.getNOTTXT();
if (noteText.length() > 40) {
System.out.println("Greater");
} else {
System.out.println("Lesser");
}
appt_note_seg.setNOTLIN("0001");
appt_note_seg.setNOTTXT(noteText.substring(0, 40));
appt.setAPPT_NOTE_SEG(appt_note_seg);
exchange.getIn().setBody(appt_inb_ifd);
}
}
输出我得到:
测试.xml
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<TEST>
<APPT_NOTE_SEG>
<SEGNAM>APPT_NOTE</SEGNAM>
<NOTLIN>0001</NOTLIN>
<NOTTXT>Hey how are u and hows ur life. long tim</NOTTXT>
</APPT_NOTE_SEG>
</TEST>
预期输出:
测试.xml
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<TEST>
<APPT_NOTE_SEG>
<SEGNAM>APPT_NOTE</SEGNAM>
<NOTLIN>0001</NOTLIN>
<NOTTXT>Hey how are u and hows ur life. long tim</NOTTXT>
</APPT_NOTE_SEG>
<APPT_NOTE_SEG>
<SEGNAM>APPT_NOTE</SEGNAM>
<NOTLIN>0002</NOTLIN>
<NOTTXT>e no see. how u been</NOTTXT>
</APPT_NOTE_SEG>
</TEST>
编辑:
我试图在 Author 类中使用两个对象引用,它们引用同一个对象。根据标题长度,我想创建生成图书 xml 标签的图书对象。这是我正在使用的代码,以及我得到的预期和当前输出。
作者.java
@Data
@CsvRecord(separator="," , skipField = true)
public class Author {
@DataField(pos = 1)
private String firstName;
@DataField(pos = 2)
private String lastName;
@Link
private Book book;
@Link
private Book bookOne;
@DataField(pos = 5)
private String Age;
}
图书.java
@Data
@Link
@CsvRecord(separator = ",")
public class Book {
@DataField(pos = 3)
private String title;
@DataField(pos = 4)
private String year;
}
输出返回:
<?xml version="1.0" encoding="UTF-8"?>
<APPT_INB_IFD>
<firstName>Claus</firstName>
<lastName>Ibsen</lastName>
<book>
<title>Camel in Action 2</title>
<year>2012</year>
</book>
<bookOne reference="../book"/>
<Age>35</Age>
</APPT_INB_IFD>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<APPT_INB_IFD>
<firstName>Claus</firstName>
<lastName>Ibsen</lastName>
<book>
<title>Camel in Action 1</title>
<year>2010</year>
</book>
<book>
<title>Camel in Action 1</title>
<year>2010</year>
</book>
<Age>35</Age>
</APPT_INB_IFD>