从JSON进行Groovy输出XML采购

时间:2019-02-26 16:07:34

标签: groovy xmlserializer jsonslurper

我正在努力获得一个时髦的脚本来输出我需要遵守供应商规范的内容。最终,我将使用文件而非静态行填充JsonSlurper,但为简单起见,我提供了示例JSON。在GroovyConsole中运行以下代码时,我似乎无法查明为什么UserReference和Bal节点包含我指定的名称空间。任何帮助表示赞赏!

    import groovy.xml.* 
    import groovy.json.JsonSlurper  
    def queryResultsResponseString =  
    ''' 
    { 
    "rows": [ 
    { 

                     "account_num": 123, 
                     "name": "Mr Bigbucks", 
                     "balance": 83.23 
                   }, 
                   { 
                     "account_num": 8675309, 
                     "name": "Johnny", 
                     "balance": 45.75 
                   } 
                  ] 
       } 
     ''' 
     def jsonResp = (new JsonSlurper()).parseText(queryResultsResponseString); 
     def xml = new groovy.xml.StreamingMarkupBuilder()
     xml.encoding = "UTF-8"
     def prints = xml.bind{ 
        mkp.xmlDeclaration()  
      mkp.declareNamespace(p1 :"com/my/namespace/woot" + "\"" + " " + "xsi:schemaLocation=\"http://www.w3.org/2001/XMLSchema-instance") 

      'p1:BatchEidvPersonSearch' { 
       jsonResp.rows.each{row -> 
         EidvPersonSearchRequest { 
           InternalId('Internal Id')
           UserReference('CLEAR ID Confirm Person Search')
           criteria {
           acctNum row.account_num 
           custName row.name 
           bal row.balance 
           }
         } 
       } 
     } 
     }
    println XmlUtil.serialize(prints).replaceAll('"','"')
// ---Results Here ---
    <?xml version="1.0" encoding="UTF-8"?><p1:BatchEidvPersonSearch xmlns:p1="com/my/namespace/woot" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
      <EidvPersonSearchRequest>
        <InternalId>Internal Id</InternalId>
        <UserReference xmlns:p1="com/my/namespace/woot" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">CLEAR ID Confirm Person Search</UserReference>
        <criteria>
          <acctNum>123</acctNum>
          <custName>Mr Bigbucks</custName>
          <bal xmlns:p1="com/my/namespace/woot" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">83.23</bal>
        </criteria>
      </EidvPersonSearchRequest>
      <EidvPersonSearchRequest>
        <InternalId>Internal Id</InternalId>
        <UserReference xmlns:p1="com/my/namespace/woot" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">CLEAR ID Confirm Person Search</UserReference>
        <criteria>
          <acctNum>8675309</acctNum>
          <custName>Johnny</custName>
          <bal xmlns:p1="com/my/namespace/woot" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">45.75</bal>
        </criteria>
      </EidvPersonSearchRequest>
    </p1:BatchEidvPersonSearch>

---更新-----

我试图通过本地加载2.2 Groovy Jar的ETL工具调用此Groovy脚本,并注意到通过ETL工具调用脚本时,它会产生预期的输出。所以...我从档案库中下载了2.2 groovy,并使用groovy控制台和BAM调用了我的脚本。我不确定版本之间有什么变化,但是输出肯定有变化。我怀疑当我们升级ETL工具时,JAR将被更新为最新版本。有什么想法在这两个版本之间可能有显着不同吗?

0 个答案:

没有答案