如果我使用Camel unmarshal和使用默认名称空间将JSON转换为XML XmlJsonDataFormat
,则所有开头的xml元素的末尾都有一个空格。我没有发现可能影响这种行为。
这是转换后的XML:
<Message xmlns="http://example.com/Message/1">
<Header >
<Header1 >header 1</Header1>
<Header2 >header 2</Header2>
</Header>
</Message>
这是我的路线定义:
public void configure() throws Exception {
// create JSON to XML data format
XmlJsonDataFormat json2XmlFormat = new XmlJsonDataFormat();
json2XmlFormat.setEncoding("UTF-8");
json2XmlFormat.setRootName("Message");
List<XmlJsonDataFormat.NamespacesPerElementMapping> namespaces = new ArrayList<XmlJsonDataFormat.NamespacesPerElementMapping>();
namespaces.add(new XmlJsonDataFormat.NamespacesPerElementMapping("Message", "||http://example.com/Message/1|"));
json2XmlFormat.setNamespaceMappings(namespaces);
from("direct:input")
.unmarshal(json2XmlFormat)
.log("${body}");
}
转换后的XML对空格仍然有效。但是我想对他们提出解决方案。
setSkipWhitespace()
并且setTrimSpaces()
仅适用于XML到JSON
任何的想法?
如果定义的命名空间不包含|
字符,则结果将在标签中不包含空格(这等效于不命名空间):
namespaces.add(new XmlJsonDataFormat.NamespacesPerElementMapping("Message", "http://example.com/Message/1"));
或者,如果为名称空间定义了适当的别名,例如ns
:
namespaces.add(new XmlJsonDataFormat.NamespacesPerElementMapping("Message", "|ns|http://example.com/Message/1|"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句