Groovy:XML结构转换

时间:2013-09-18 14:01:02

标签: xml groovy

输入XML

<ClientInformation>
<FirstName>Ram</FirstName>
<LastName>Shah</LastName>
<MiddleName></MiddleName>
<DateOfBirth>01/01/1972</DateOfBirth>
<TaxIdentification>12346</TaxIdentification>
<RelationshipToInsuredCT>Spouse</RelationshipToInsuredCT>
<RelationshipToEmployeeCT>Spouse</RelationshipToEmployeeCT>
<RoleTypeCT>Insured</RoleTypeCT>
</ClientInformation>

输出XML

<ContractClientVO>
 <ConstantElement1></ConstantElement1>
 <ConstantElement2></ConstantElement2>
 <RelationshipToInsuredCT>Spouse</RelationshipToInsuredCT>
 <RelationshipToEmployeeCT>Spouse</RelationshipToEmployeeCT>
 <ClientRoleVO>
  <ClientRoleConstantElement1></ClientRoleConstantElement1>
  <RoleTypeCT>Insured</RoleTypeCT>
  <ClientDetailVO>
   <FirstName>Ram</FirstName>
    <LastName>Shah</LastName>
    <MiddleName></MiddleName>
    <DateOfBirth>01/01/1972</DateOfBirth>
    <TaxIdentification>12346</TaxIdentification>
  </ClientDetailVO>
 <ClientRoleVO>
</ContractClientVO>

用于映射参考的XML

<ClientInformation>
    <ContractClientVO>
     <AssignmentReasonCT></AssignmentReasonCT>
     <Associated></Associated>
     <AuthorizedSignatureCT></AuthorizedSignatureCT>
     <ClassCT></ClassCT>
     <RelationshipToInsuredCT></RelationshipToInsuredCT>
     <RelationshipToEmployeeCT></RelationshipToEmployeeCT>
     <ClientRoleVO>
      <AgentFK></AgentFK>
      <Associated></Associated>
      <RoleTypeCT></RoleTypeCT>
      <ClientDetailVO>
        <address></address>
        <city></city>
        <state></state>
        <Amount></Amount>
        <CountryTypeCT></CountryTypeCT>
        <AmountOfInsurance></AmountOfInsurance>
        <FirstName></FirstName>
        <LastName></LastName>
        <TaxIdentification></TaxIdentification>
     </ClientDetailVO>
   </ClientRoleVO>
  </ContractClientVO>
 </ClientInformation>

所以第三个xml是供参考的。它有助于定义输出xml的结构 Groovy是否有针对此类问题的开箱即用解决方案?或者我按每个input xml元素查看并使用reference xml验证它。

1 个答案:

答案 0 :(得分:1)

所以,给定这个xml:

<Clients>
    <ClientInformation>
        <FirstName>Ram</FirstName>
        <LastName>Shah</LastName>
        <MiddleName></MiddleName>
        <DateOfBirth>01/01/1972</DateOfBirth>
        <TaxIdentification>12346</TaxIdentification>
        <RelationshipToInsuredCT>Spouse</RelationshipToInsuredCT>
        <RelationshipToEmployeeCT>Spouse</RelationshipToEmployeeCT>
        <RoleTypeCT>Insured</RoleTypeCT>
    </ClientInformation>
</Clients>

您可以使用此XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <Output>
            <xsl:for-each select="Clients/ClientInformation">
                <ContractClientVO>
                    <ConstantElement1></ConstantElement1>
                    <ConstantElement2></ConstantElement2>
                    <RelationshipToInsuredCT><xsl:value-of select="RelationshipToInsuredCT"/></RelationshipToInsuredCT>
                    <RelationshipToEmployeeCT><xsl:value-of select="RelationshipToEmployeeCT"/></RelationshipToEmployeeCT>
                    <ClientRoleVO>
                        <ClientRoleConstantElement1></ClientRoleConstantElement1>
                        <RoleTypeCT><xsl:value-of select="RoleTypeCT"/></RoleTypeCT>
                        <ClientDetailVO>
                            <FirstName><xsl:value-of select="FirstName"/></FirstName>
                            <LastName><xsl:value-of select="LastName"/></LastName>
                            <MiddleName><xsl:value-of select="MiddleName"/></MiddleName>
                            <DateOfBirth><xsl:value-of select="DateOfBirth"/></DateOfBirth>
                            <TaxIdentification><xsl:value-of select="TaxIdentification"/></TaxIdentification>
                        </ClientDetailVO>
                    </ClientRoleVO>
                </ContractClientVO>
            </xsl:for-each>
        </Output>
    </xsl:template>
</xsl:stylesheet>

生成:

<Output>
   <ContractClientVO>
      <ConstantElement1 />
      <ConstantElement2 />
      <RelationshipToInsuredCT>Spouse</RelationshipToInsuredCT>
      <RelationshipToEmployeeCT>Spouse</RelationshipToEmployeeCT>
      <ClientRoleVO>
         <ClientRoleConstantElement1 />
         <RoleTypeCT>Insured</RoleTypeCT>
         <ClientDetailVO>
            <FirstName>Ram</FirstName>
            <LastName>Shah</LastName>
            <MiddleName />
            <DateOfBirth>01/01/1972</DateOfBirth>
            <TaxIdentification>12346</TaxIdentification>
         </ClientDetailVO>
      </ClientRoleVO>
   </ContractClientVO>
</Output>

转换xml的groovy将是(假设xml位于名为xml的字符串中,而xsl位于名为xsl的字符串中):

import javax.xml.transform.TransformerFactory
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource

def w = new StringWriter()
TransformerFactory.newInstance()
                  .newTransformer( new StreamSource( new StringReader( xsl ) ) )
                  .transform( new StreamSource( new StringReader( xml ) ),
                  new StreamResult( w ) )
println groovy.xml.XmlUtil.serialize( w.toString() )