[英]What is the model of using Smook and Freemarker to transform Java Objects to XML?
I am having trouble finding clear documentation on how to do the following transformation: 我很难找到有关如何进行以下转换的清晰文档:
Java Object -> Smooks/Freemarker Template -> XML Output Java对象-> Smooks / Freemarker模板-> XML输出
Here is the example I am trying: 这是我尝试的示例:
Java POJO (I have a separate DAO clas that populates this object): Java POJO(我有一个单独的DAO clas填充此对象):
package Transformer;
public class JavaObject {
String name;
}
Main transformer class: 主变压器等级:
package Transformer;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.transform.stream.StreamResult;
import org.milyn.Smooks;
import org.milyn.container.ExecutionContext;
import org.milyn.payload.JavaSource;
import org.xml.sax.SAXException;
public class Transformer {
protected static String runSmooksTransform(Object javaObject) throws IOException, SAXException {
Smooks smooks = new Smooks("smooks-config.xml");
try {
ExecutionContext executionContext = smooks.createExecutionContext();
StringWriter writer = new StringWriter();
smooks.filterSource(executionContext, new JavaSource("smooks-config.xml"), new StreamResult(writer));
return writer.toString();
} finally {
smooks.close();
}
}
public static void main(String args[]) {
try {
Transformer.runSmooksTransform(javaObject);
} catch(Throwable ex){
System.err.println("Uncaught exception - " + ex.getMessage());
ex.printStackTrace(System.err);
}
}
}
So here is the point where I am confused... I have seen a few different ways to "map" the template 所以这是我感到困惑的地方...我已经看到了几种“映射”模板的方法
here are some examples I have seen: 这是我看到的一些示例:
A .ftl template file with mapping like this: 映射如下的.ftl模板文件:
<Nm> ${Name} </Nm>
An XML mapping like this: 这样的XML映射:
<medi:segment minOccurs="0" maxOccurs="1" segcode="" xmltag="Group">
<medi:field xmltag="Name" />
</medi:segment>
Mapping in the smooks-config.xml itself: 在smooks-config.xml本身中进行映射:
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd"
xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
<resource-config selector="global-parameters">
<param name="stream.filter.type">SAX</param>
</resource-config>
<reader mappingModel="example.xml" />
<ftl:freemarker applyOnElement="order">
<ftl:template>
<Nm>${name}</Nm>
</ftl:template>
</ftl:freemarker>
</smooks-resource-list>
So can anyone please explain the correct way to use Smooks + a Freemarker template to convert a java object to a specified XML output? 因此,谁能解释使用Smooks + Freemarker模板将Java对象转换为指定XML输出的正确方法吗?
Or point me to documentation/example specific to this use case? 或点我具体到这个用例文档/例子吗?
Thank you 谢谢
I don't know anything about how it's done in Smooks, but it's very likely that you need to add a public String getName() { return name; }
我对Smooks中的操作一无所知,但是很有可能需要添加一个
public String getName() { return name; }
public String getName() { return name; }
to the JavaObject
class, or else it won't be visible form the FreeMarker template. public String getName() { return name; }
到JavaObject
类,否则它将在FreeMarker模板中不可见。 It actually depends on the FreeMarker configuration settings (and I don't know how Smooks configures it), so anything is possible in theory, but it's likely that you need a getter method, but if not then at least the field need to be public
. 它实际上取决于FreeMarker的配置设置(而且我不知道Smooks是如何配置的),因此理论上一切皆有可能,但是您可能需要使用getter方法,但如果没有,那么至少该字段需要
public
。
Also you don't pass javaObject
to Smooks in your example code, though I guess it's not a the real code. 同样,您也不要在示例代码中将
javaObject
传递给Smooks,尽管我认为这不是真正的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.