Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Ant替换XML文件中的值_Ant - Fatal编程技术网

使用Ant替换XML文件中的值

使用Ant替换XML文件中的值,ant,Ant,我正在尝试自动化我的android构建过程。为此,我想更改XML文件中的应用程序名称。代码是这样的 <resources> <string name="app_name">Realta Connections</string> </resources> Realta连接 现在我想在构建时用其他名称替换“Realta Connections”,这是我在构建时给出的名称。名称可以是“Realta Connections”或任何其他名称,因此

我正在尝试自动化我的android构建过程。为此,我想更改XML文件中的应用程序名称。代码是这样的

<resources>
   <string name="app_name">Realta Connections</string>
</resources>

Realta连接

现在我想在构建时用其他名称替换“Realta Connections”,这是我在构建时给出的名称。名称可以是“Realta Connections”或任何其他名称,因此我需要检测name=“app\u name”并替换其中的内容。我试着寻找如何做到这一点,但找不到确切的方法。我该怎么做?请帮助。

可能最容易有一个固定值,它将被替换。这将允许使用替换任务: 您需要
replacetoken
/
replacevalue
中的字符串![CDATA[]
因为xml字符

  <replace casesensitive="false" 
    file="../KS.build/ivy.properties">
    <replacetoken><![CDATA[<string name="app_name">Realta Connections</string>]]></replacetoken>  
    <replacevalue><![CDATA[<string name="app_name">Something else</string>]]></replacevalue>  
  </replace>

Realta连接]]>
其他内容]]>
否则就没有正常的ant解决方案(不允许嵌套的CDATA替换)

链接:


在ANT中处理XML时,我始终建议使用。有关您的要求,请参阅

一点Xpath知识不会有什么坏处,请参见:



我知道这个问题很老了,但这里有一个想法。这似乎不是一个纯粹的ant解决方案,但您可以使用
标记将脚本嵌入ant中

以下是一个函数,可用于搜索XML文件中的内容并将结果存储在属性中:

<scriptdef name="xpath-query" language="javascript">
        <attribute name="query"/>
    <attribute name="xmlfile"/>
    <attribute name="property"/>
     <![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);
        importClass(java.lang.System);


        var exp = attributes.get("query");          
        var filename = attributes.get("xmlfile");
        var xpath = XPathFactory.newInstance().newXPath();          
        var input = new InputSource(new FileInputStream(filename));
        var value = xpath.evaluate(exp, input, XPathConstants.STRING);          
        self.project.setProperty( attributes.get("property"), value );          
        System.out.println("Copied this value:" + value + " to this property:" + attributes.get("property"));

    ]]>
</scriptdef>
这种方法的潜在缺点是,如果XML文件中的其他地方有相同的应用程序名称字符串,则regex replace可能会替换您不想替换的内容


您可能可以定义
来进行替换,而不仅仅是存储在属性中找到的内容,但我已经准备好了这部分代码。

试试这段代码,它适合我

<target name="change-app-name">
    <replaceregexp file="res/values/strings.xml" match='name="app_name"(.*)'
        replace='name="app_name"&gt;Something Else&lt;\/string&gt;'/>
</target>


我要求它可以是任何东西,而不是Realta连接。所以我不知道有什么价值。因此,我需要访问name=“app_name”,然后替换其中的任何内容。这方面没有标准的ant任务,您可能需要使用固定名称或编写自己的任务。您找到解决方案了吗?
<replaceregexp file="app.xml" match="${appName}" replace="${newAppName}" />
<target name="change-app-name">
    <replaceregexp file="res/values/strings.xml" match='name="app_name"(.*)'
        replace='name="app_name"&gt;Something Else&lt;\/string&gt;'/>
</target>