Ant和XML配置文件解析

时间:2022-12-01 13:01:43

I have an XML file of the following form -

我有一个以下形式的XML文件 -

<map MAP_XML_VERSION="1.0">
    <entry key="database.user" value="user1"/>
    ...
</map>

Does ant have a native ability to read this and let me perform an xquery to pull back values for keys? Going through the API I did not see such capabilities.

蚂蚁是否具有读取此功能的原生能力,让我执行xquery来拉回键的值?通过API我没有看到这样的功能。

3 个解决方案

#1


8  

You can use the scriptdef tag to create a JavaScript wrapper for your class. Inside JS, you pretty much have the full power of Java and can do any kind of complicated XML parsing you want.

您可以使用scriptdef标记为您的类创建JavaScript包装器。在JS中,你几乎拥有Java的全部功能,可以进行任何你想要的复杂XML解析。

For example:

例如:

<project default="build">        
    <target name="build">            
        <xpath-query query="//entry[@key='database.user']/@value"
                     xmlFile="test.xml" addproperty="value"/>
        <echo message="Value is ${value}"/>    
    </target>

    <scriptdef name="xpath-query" language="javascript">
        <attribute name="query"/>
        <attribute name="xmlfile"/>
        <attribute name="addproperty"/>

        <![CDATA[
            importClass(java.io.FileInputStream);
            importClass(javax.xml.xpath.XPath);
            importClass(javax.xml.xpath.XPathConstants);
            importClass(javax.xml.xpath.XPathFactory);
            importClass(org.xml.sax.InputSource);

            var exp = attributes.get("query");
            var filename = attributes.get("xmlfile");
            var input = new InputSource(new FileInputStream(filename));
            var xpath = XPathFactory.newInstance().newXPath();
            var value = xpath.evaluate(exp, input, XPathConstants.STRING);

            self.project.setProperty( attributes.get("addproperty"), value );

        ]]>

    </scriptdef>
</project>

#2


10  

The optional Ant task XMLTask is designed to do this. Give it an XPath expression and you can select the above into (say) a property. Here's an article on how to use it, with examples. It'll do tons of other XML-related manipulations/querying/creation as well.

可选的Ant任务XMLTask旨在实现此目的。给它一个XPath表达式,你可以选择上面的(比如说)一个属性。这是一篇关于如何使用它的文章,带有示例。它还将进行大量其他与XML相关的操作/查询/创建。

e.g.

例如

<xmltask source="map.xml">
   <!-- copies to a property 'user' -->
   <copy path="/map/entry[@key='database.user']/@value" attrValue="true" property="user"/>
</xmltask>

Disclaimer: I'm the author.

免责声明:我是作者。

#3


0  

Sounds like you want something like ant-xpath-task. I'm not aware of a built-in way to do this with Ant.

听起来你想要像ant-xpath-task这样的东西。我不知道使用Ant的内置方法。

#1


8  

You can use the scriptdef tag to create a JavaScript wrapper for your class. Inside JS, you pretty much have the full power of Java and can do any kind of complicated XML parsing you want.

您可以使用scriptdef标记为您的类创建JavaScript包装器。在JS中,你几乎拥有Java的全部功能,可以进行任何你想要的复杂XML解析。

For example:

例如:

<project default="build">        
    <target name="build">            
        <xpath-query query="//entry[@key='database.user']/@value"
                     xmlFile="test.xml" addproperty="value"/>
        <echo message="Value is ${value}"/>    
    </target>

    <scriptdef name="xpath-query" language="javascript">
        <attribute name="query"/>
        <attribute name="xmlfile"/>
        <attribute name="addproperty"/>

        <![CDATA[
            importClass(java.io.FileInputStream);
            importClass(javax.xml.xpath.XPath);
            importClass(javax.xml.xpath.XPathConstants);
            importClass(javax.xml.xpath.XPathFactory);
            importClass(org.xml.sax.InputSource);

            var exp = attributes.get("query");
            var filename = attributes.get("xmlfile");
            var input = new InputSource(new FileInputStream(filename));
            var xpath = XPathFactory.newInstance().newXPath();
            var value = xpath.evaluate(exp, input, XPathConstants.STRING);

            self.project.setProperty( attributes.get("addproperty"), value );

        ]]>

    </scriptdef>
</project>

#2


10  

The optional Ant task XMLTask is designed to do this. Give it an XPath expression and you can select the above into (say) a property. Here's an article on how to use it, with examples. It'll do tons of other XML-related manipulations/querying/creation as well.

可选的Ant任务XMLTask旨在实现此目的。给它一个XPath表达式,你可以选择上面的(比如说)一个属性。这是一篇关于如何使用它的文章,带有示例。它还将进行大量其他与XML相关的操作/查询/创建。

e.g.

例如

<xmltask source="map.xml">
   <!-- copies to a property 'user' -->
   <copy path="/map/entry[@key='database.user']/@value" attrValue="true" property="user"/>
</xmltask>

Disclaimer: I'm the author.

免责声明:我是作者。

#3


0  

Sounds like you want something like ant-xpath-task. I'm not aware of a built-in way to do this with Ant.

听起来你想要像ant-xpath-task这样的东西。我不知道使用Ant的内置方法。