XML到制表符分隔的文件

时间:2012-02-01 17:12:41

标签: xml coldfusion

我一直在尝试使用Coldfusion将XML作为制表符分隔文件存入文本文件。我想知道是否有人可以帮助我解决这个问题。

我的XML可以是具有多个子节点的多个节点。我可以计算出维护每个子节点的选项卡数量然后将其恢复到起点的逻辑。

是否有任何我不知道的特定递归算法?

xml是这样的 -

<?xml version="1.0" encoding="UTF-8"?>
<I BMSMETA_AUTOMETASET_4_OPTIONS="Personnel"
id="b02bfa5f-f2d0-45ac-99e0-9e4e12416db1">
<I BMSMETA_AUTOMETASET_4_OPTIONS="Ranks"
    id="c25d4732-862b-4dc4-835e-eb167f63a114">
    <I BMSMETA_AUTOMETASET_4_OPTIONS="Army"
        id="7a54176d-38f9-4753-bb93-60649e70ddb1">
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Field Marshall (Fd Marshall) (FM)"
            id="5d311b9d-8d6e-4fa8-8933-a643bf55143d" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="General (Gen)"
            id="56d3aae4-7da9-4940-9692-e1ad90584127" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Lieutenant General (Lt Gen)"
            id="0f1bb73b-39ae-4920-9d23-2c92d543ef37" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Major General (Maj Gen)"
            id="9f269157-fde9-4ea4-ae1e-65344d56388e" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Brigadier (Brig)"
            id="33e15bd2-aa5e-4a99-8096-821568bf7156" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Colonel (Col )"
            id="fa38285d-2319-4e43-9207-3c4080497118" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Lieutenant Colonel (Lt Col) (CO)"
            id="9fab6b6a-c4b1-4b5d-a051-8c22260f3225" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Major (Maj) (OC)"
            id="9c232a00-1e54-427f-9fdf-73aa50f4c49e" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Captain (Capt) (SO3)"
            id="18043ea4-d9cd-41cd-9a5c-851de350b397" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Lieutenant (Lt)"
            id="9dfc1631-e49d-48e7-ad47-7ce41da36cdf" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Second Lieutenant (2nd Lieutenant) (2Lt)"
            id="4f423559-b2a6-4164-8718-b7fce8180331" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Subaltern"
            id="38f33ead-17d3-4358-8fc4-1e03ee46008a" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Cornet (Cnt)"
            id="59ed6193-4e6a-413f-92da-cb37508bc637" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Officer Cadet (Ocdt)"
            id="bfb6ab3a-13e5-46c0-82e7-0d63b794b4f6" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Junior Under Officer (JUO)"
            id="dfe840e8-421a-4017-9066-429e7b3778ed" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Senior Under officer (SUO)"
            id="52f083f3-0826-4b25-be68-3cefd8561e15" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Conductor (Cdtr)"
            id="3ed346d3-0248-40f7-bcfb-ba9b8dabe02f" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Non Commissioned Officer (NCO)"
            id="c0b0af29-f6c6-47f0-8a49-27514d78a55d" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Senior Non Commissioned Officer (SNCO)"
            id="aeaa3bcf-b104-4261-8a5b-652bdd4b8531" />
    </I>
</I>

和tsv文件我想要像这样shud b -

Personnel
Ranks               
    Army            
        Field Marshall (Fd Marshall) (FM)       
        General (Gen)       
        Lieutenant General (Lt Gen)     
        Major General (Maj Gen)     
        Brigadier (Brig)        
        Colonel (Col )      
        Lieutenant Colonel (Lt Col) (CO)        
        Major (Maj) (OC)        
        Captain (Capt) (SO3)        
        Lieutenant (Lt)         
        Second Lieutenant (2nd Lieutenant) (2Lt)        
        Subaltern           
        Cornet (Cnt)        
        Officer Cadet (Ocdt)        
        Junior Under Officer (JUO)      
        Senior Under officer (SUO)      
        Conductor (Cdtr)        
        Non Commissioned Officer (NCO)      
            Senior Non Commissioned Officer (SNCO)  

1 个答案:

答案 0 :(得分:6)

最好的办法是使用XSLT:

<cfxml variable="myXML">
<?xml version="1.0" encoding="UTF-8"?>
<I BMSMETA_AUTOMETASET_4_OPTIONS="Personnel"
id="b02bfa5f-f2d0-45ac-99e0-9e4e12416db1">
<I BMSMETA_AUTOMETASET_4_OPTIONS="Ranks"
    id="c25d4732-862b-4dc4-835e-eb167f63a114">
    <I BMSMETA_AUTOMETASET_4_OPTIONS="Army"
        id="7a54176d-38f9-4753-bb93-60649e70ddb1">
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Field Marshall (Fd Marshall) (FM)"
            id="5d311b9d-8d6e-4fa8-8933-a643bf55143d" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="General (Gen)"
            id="56d3aae4-7da9-4940-9692-e1ad90584127" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Lieutenant General (Lt Gen)"
            id="0f1bb73b-39ae-4920-9d23-2c92d543ef37" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Major General (Maj Gen)"
            id="9f269157-fde9-4ea4-ae1e-65344d56388e" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Brigadier (Brig)"
            id="33e15bd2-aa5e-4a99-8096-821568bf7156" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Colonel (Col )"
            id="fa38285d-2319-4e43-9207-3c4080497118" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Lieutenant Colonel (Lt Col) (CO)"
            id="9fab6b6a-c4b1-4b5d-a051-8c22260f3225" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Major (Maj) (OC)"
            id="9c232a00-1e54-427f-9fdf-73aa50f4c49e" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Captain (Capt) (SO3)"
            id="18043ea4-d9cd-41cd-9a5c-851de350b397" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Lieutenant (Lt)"
            id="9dfc1631-e49d-48e7-ad47-7ce41da36cdf" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Second Lieutenant (2nd Lieutenant) (2Lt)"
            id="4f423559-b2a6-4164-8718-b7fce8180331" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Subaltern"
            id="38f33ead-17d3-4358-8fc4-1e03ee46008a" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Cornet (Cnt)"
            id="59ed6193-4e6a-413f-92da-cb37508bc637" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Officer Cadet (Ocdt)"
            id="bfb6ab3a-13e5-46c0-82e7-0d63b794b4f6" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Junior Under Officer (JUO)"
            id="dfe840e8-421a-4017-9066-429e7b3778ed" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Senior Under officer (SUO)"
            id="52f083f3-0826-4b25-be68-3cefd8561e15" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Conductor (Cdtr)"
            id="3ed346d3-0248-40f7-bcfb-ba9b8dabe02f" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Non Commissioned Officer (NCO)"
            id="c0b0af29-f6c6-47f0-8a49-27514d78a55d" />
        <I BMSMETA_AUTOMETASET_4_OPTIONS="Senior Non Commissioned Officer (SNCO)"
            id="aeaa3bcf-b104-4261-8a5b-652bdd4b8531" />
    </I>
</I>
</I>
</cfxml>

以上将您的XML存储在CF XML对象中。 接下来,XSLT对象:

<cfxml variable="myXSLT">
<?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="/">
    <xsl:call-template name="treeWalk">
      <xsl:with-param name="depth" select="0"/>
      <xsl:with-param name="node" select="I"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="treeWalk">
    <xsl:param name="depth"/>
    <xsl:param name="node" select="0"/>


    <xsl:call-template name="tabs">
      <xsl:with-param name="count" select="$depth"/>
    </xsl:call-template>

    <xsl:value-of select="$node/@BMSMETA_AUTOMETASET_4_OPTIONS" />  
<xsl:text>
</xsl:text>

    <xsl:if test="$node/I">
        <xsl:for-each select="$node/I">

            <xsl:call-template name="treeWalk">
              <xsl:with-param name="depth" select="$depth+1"/>
              <xsl:with-param name="node" select="."/>
            </xsl:call-template>

        </xsl:for-each>
    </xsl:if>   

</xsl:template>


<xsl:template name="tabs">
    <xsl:param name="count"/>

        <xsl:if test="$count > 0">
            <xsl:text>  </xsl:text>
            <xsl:call-template name="tabs">
              <xsl:with-param name="count" select="$count - 1"/>
            </xsl:call-template>
        </xsl:if>   
</xsl:template>


</xsl:stylesheet>
</cfxml>

最后,将XLST应用于XML:

<cfset output = XMLTransform(myXML, myXSLT)>

<cfoutput>
<pre>
#output#
</pre>
</cfoutput>