如何使用java smooks和freemarker从具有多个名称空间的xml中提取元素

时间:2018-03-22 21:28:39

标签: java freemarker smooks

我正在尝试使用Smooks进行XML-XML转换。输入XML具有多个名称空间时遇到问题。

这是我的示例输入文件

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
    <look:Trans xmlns:look="http://www.example.com">
     <Response>0</Response>
     <DGX>
       <SIGN>1</SIGN>
     </DGX> 
   </look:Trans>
  </soapenv:Body>
</soapenv:Envelope>

我的smooks配置文件

<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
 xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd"
 xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">

<params>
    <param name="stream.filter.type">SAX</param>
    <param name="default.serialization.on">false</param>
</params>

<resource-config selector="Trans">
    <resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>

<ftl:freemarker applyOnElement="Trans">
    <ftl:template>
<!--  <#ftl ns_prefixes={"look": "http://www.example.com"}> <#outputformat 'XML'> 
<Response> 
    <Code>${look\:Trans.Response} </Code>
    <Sign>${look\:Trans.DGX.SIGN} </Sign>
</Response> </#outputformat> -->
    </ftl:template>

我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

我不确定Smook选择哪个XML节点作为模板上下文根...如果它是soapenv:Body那么我就不会在模板中看到问题。无论如何,我在http://try.freemarker.org/上尝试了这一点,其中doc是文档节点(因此与Smooks不同),以证明多个名称空间应该像这样工作:

模板:

<#ftl ns_prefixes={"look": "http://www.example.com", "soapenv": "http://schemas.xmlsoap.org/soap/envelope/"}
      output_format="XML">
${doc.soapenv\:Envelope.soapenv\:Body.look\:Trans.DGX.SIGN}

数据模型:

doc=
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
    <look:Trans xmlns:look="http://www.example.com">
     <Response>0</Response>
     <DGX>
       <SIGN>123</SIGN>
     </DGX> 
   </look:Trans>
  </soapenv:Body>
</soapenv:Envelope>

结果:

123

BTW,请注意,您只需将<#outputformat 'XML'>...</#outputformat>添加到output_format='XML',而不是<#ftl ...>。它更短。

相关问题