通过ant读取键值配对XML文件?

时间:2015-06-19 10:45:23

标签: xml ant xml-parsing xmltask

我想阅读下面的XML文件

var config = {
    transformResponse: function (data, headers) {
        if(headers("content-type") === "application/json" && angular.isString(data)) {

            try {
                data = JSON.parse(data);
            } catch (e) {
                // if parsing error, try another parser
                // or just fix commas, if you know for sure that the problem is in commas
                data = JSON2.parse(data);
            }

            return data;

        } else {
            return data;
        }
    }
};

$http.get("rules.json", config).success(function (data) {
    $scope.rules = data;
});

我想阅读其中<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="con" path="org.eclipse.jst.server.core.container/com.ibm.ws.st.core.runtimeClasspathProvider/com.ibm.worklight"/> <classpathentry kind="con" path="com.worklight.studio.plugin.classpath.SERVER_CONTAINER"/> <classpathentry kind="src" path="server/java"/> <classpathentry kind="src" path="common"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="src" output="adapters/adp1/bin" path="adapters/agent/src"/> <classpathentry kind="src" output="adapters/adp2/bin" path="adapters/alerts/src"/> <classpathentry kind="src" output="adapters/adp3/bin" path="adapters/billing/src"/> <classpathentry kind="src" output="adapters/adp4/bin" path="adapters/client/src"/> <classpathentry kind="src" output="adapters/adp5/bin" path="adapters/category/src"/> </classpath> path的{​​{1}}的值。我能够获得所有路径值但不能暗示它的条件。我使用以下代码。

kind

它适用于所有"src"值,但我应该怎样做才能获得<target name="xml"> <echo>Test For Each</echo> <for list="${classpath.classpathentry.path}" param="letter" delimiter=","> <sequential> <echo message="path ::: @{letter}"/> </sequential> </for> </target> path path的价值?

1 个答案:

答案 0 :(得分:2)

正如我在评论中所述,以下XSLT将解析kind = src的所有类路径条目并生成单行路径语句。

getclasspath.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/classpath">
        <xsl:text>path=</xsl:text>
        <xsl:for-each select="classpathentry[@kind='src']">
            <xsl:value-of select="@path"/>
            <xsl:text>;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

然后是以下蚂蚁任务:

<xslt style='getclasspath.xslt' in='classpath.xml' out='classpath.properties' />