[英]How to generate custom tag names and namespaces in xml using apache camel
[英]How to add another xml tag based on the content size using apache camel
问题陈述:
我有平面文件作为输入源,其中包含长度从(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
转换器路由
@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 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 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 类中使用两个对象引用来引用同一个对象。 根据标题长度,我想创建生成 book xml 标签的 book 对象。 这是我与预期和当前输出一起使用的代码。
作者.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>
您能否重新格式化预期的输出以将<APPT_NOTE_SEG>
包装在<APPT_NOTE_SEGS>
标记中? 然后,您可以在用于执行编组的对象中创建<APPT_NOTE_SEG>
对象的列表。
否则,您可能在用于执行编组的类中有两个<APPT_NOTE_SEG>
对象,并使用@XmlAttribute(required=true)
配置第一个对象,使用@XmlAttribute(required=true)
配置第二个@XmlAttribute
,如果它为空,编组器将忽略该对象。 然后根据您已经完成的逻辑根据需要设置值。
也不确定您是否只是命名与此示例的 XML 匹配的内容,但以防万一您实际上可以创建一个 pojo 并使用您需要的 XML 命名注释元素:
@XmlRootElement(name = "test")
public class TestBean {
@XmlAttribute(name = "desired_id_format")
private String id;
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.