用于读取和转换xml

时间:2017-02-20 19:14:18

标签: xml groovy transformation

需要有关Groovy脚本的帮助。我有以下输入xml,其中将动态填充此xml,我们没有任何关于RecordDetails节点下将填充多少节点的线索。

输入:

<?xml version="1.0" encoding="UTF-8"?>
<Record>
   <XYZ>
      <Header>
         <Code>12345</Code>
      </Header>
      <Details>
         <RecID>1</RecID>
         <RecordDetail>
            <Name>ABC</Name>
            <Email>abc@in.com</Email>
            <Address>123,acdf</Address>
         </RecordDetail>
      </Details>
      <Details>
         <RecID>2</RecID>
         <RecordDetail>
            <Name>ABC</Name>
            <Email>abc@in.com</Email>
         </RecordDetail>
      </Details>
   </XYZ>
</Record>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<Record>
   <Header>
      <Code>12345</Code>
   </Header>
   <Details>
      <RecID>1</RecID>
      <RecordDetail>
         <FieldName>NAME</FieldName>
         <FieldValue>ABC</FieldValue>
      </RecordDetail>
      <RecordDetail>
         <FieldName>Email</FieldName>
         <FieldValue>ABC@a.com</FieldValue>
      </RecordDetail>
   </Details>
</Record>

1 个答案:

答案 0 :(得分:2)

您只需要转换输入xml。

这可以通过以下方式实现:

  • 编写一个xslt并使用runner来执行
  • 转换为groovy本身。

看起来你正在寻找后一个。

这是groovy脚本:

mkp.xmlDeclaration()

可以在线快速尝试 Demo

<强>输出:

enter image description here

编辑:根据OP的问题。

<?xml version="1.0"?> - 添加details.each { detail ->

detail - 详情列表。我们想通过每个细节循环。每个值都进入for(detail : details)

fld类似。

using System; using System.Security.Cryptography; public class DataProtectionSample { // Create byte array for additional entropy when using Protect method. static byte[] s_aditionalEntropy = { 9, 8, 7, 6, 5 }; public static byte[] Protect(byte[] data) { try { // Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted // only by the same current user. return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.CurrentUser); } catch (CryptographicException e) { Console.WriteLine("Data was not encrypted. An error occurred."); Console.WriteLine(e.ToString()); return null; } } public static byte[] Unprotect(byte[] data) { try { //Decrypt the data using DataProtectionScope.CurrentUser. return ProtectedData.Unprotect(data, s_aditionalEntropy, DataProtectionScope.CurrentUser); } catch (CryptographicException e) { Console.WriteLine("Data was not decrypted. An error occurred."); Console.WriteLine(e.ToString()); return null; } } public static void PrintValues(Byte[] myArr) { foreach (Byte i in myArr) { Console.Write("\t{0}", i); } Console.WriteLine(); } } 也与上述相同。