xml - 使用 ANT 替换整个 XML 标签

标签 xml ant

PhoneGap Build提供一项服务(目前测试版免费),他们可以获取 HTML+JS+CSS 源代码以及用于配置构建选项 (config.xml) 的 XML 文件,并为所有支持的平台(iOS、Android、 Blackberry、Symbian、WebOS),这可以节省大量时间,并为我节省大量头痛药物费用。

不幸的是,there is inconsistent behavior with the way links to external websites work :

Currently in order to have links open in an external browser, it requires uploading different code to PhoneGap Build. For iOS the domain must be whitelisted, and for Android the domain must be unlisted.

You could make it consistent by adding an attribute to the access tag, like includeForAndroid="false", which would NOT include the access tag contents on Android, so that links would open in an external browser, just like on iOS.

Config.xml docs ;这是a relevant blog post .

虽然员工提出了修复建议,但尚未在产品中制作和发布:

We're hoping to get this straightened out in Cordova open source project: in the meantime, for PhoneGap Build, I'm thinking something like this:

<access origin="*" onlyInBrowser="true" />

That will not include the tag on Android, but will on iOS.

在进行此更改并在产品中可用之前,解决方法是制作两个我将上传的 zip 文件:一个用于 Android(不带访问标签),一个用于 iOS(带访问标签)。虽然并不理想,但为了保持应用程序行为一致,必须这样做。

我已经使用 ANT 来自动执行此项目中的许多任务,因此如果 ANT 也能为我执行 config.xml 更新,那就太理想了。

作为引用,这是我的 ANT 代码,它将构建两个 zip:

<target name="BUILD-ZIP" depends="verify-using-minified-js,prepare-for-build">

    <tstamp>
        <format property="build.tstamp" pattern="yyyy-MM-dd__HH-mm-ss" />
    </tstamp>
    
    <antcall target="zip-ios">
        <param name="tstamp" value="${build.tstamp}" />
    </antcall>
    
    <!-- build android second so that we can just remove the access tags -->
    <antcall target="zip-android">
        <param name="tstamp" value="${build.tstamp}" />
    </antcall>
        
</target>

<target name="zip-ios">
    <zip destfile="${dir.pkg.phonegap}${tstamp}-apple.zip">
        <zipfileset dir="${dir.pkg.tmp}">
            <exclude name="build.xml" />
            <exclude name="build.properties" />
            <exclude name="settings.xml" />
            <exclude name=".project" />
            <exclude name="**/*.psd" />
            <exclude name="assets/js/app.js" />
            <exclude name="assets/js/cordova-1.5.0.js" />
        </zipfileset>
    </zip>
</target>

<target name="zip-android">
    
    <!-- before building android zip, get rid of the <access> tags -->
    
    <zip destfile="${dir.pkg.phonegap}${tstamp}-android.zip">
        <zipfileset dir="${dir.pkg.tmp}">
            <exclude name="build.xml" />
            <exclude name="build.properties" />
            <exclude name="settings.xml" />
            <exclude name=".project" />
            <exclude name="**/*.psd" />
            <exclude name="assets/js/app.js" />
            <exclude name="assets/js/cordova-1.5.0.js" />
        </zipfileset>
    </zip>
</target>
    
<target name="prepare-for-build">
    
    <!-- increment build number -->
    <propertyfile file="build.properties">
        <entry key="version.build.number" type="int" operation="+" default="1"/>
    </propertyfile>
    <property file="build.properties"/>
    <echo message="BUILD NUMBER: ${version.build.number}"/>
    
    <delete includeemptydirs="true" verbose="false">
        <fileset dir="${dir.pkg.tmp}" includes="**/*"/>
    </delete>
    <filter token="BUILD_NUMBER" value="${version.build.number}" />
    <filter token="VERSION_MAJOR" value="${version.major}" />
    <filter token="VERSION_MINOR" value="${version.minor}" />
    <copy todir="${dir.pkg.tmp}" verbose="true" overwrite="true" filtering="true">
        <fileset dir="${dir.dev}" includes="**/*" />
    </copy>
    
</target>

那里有一条评论,在 zip-android 中目标:

<!-- before building android zip, get rid of the <access> tags -->

这是我想要进行替换的地方。我尝试过使用<filter>任务以及<replace><replaceRegExp> ,但我似乎找不到办法做到这一点,这一切都归结为我需要替换字符串:<access ... />这些替换方法似乎都不允许 <>在 token /匹配/等属性中。我尝试过使用 CDATA,但也无法正常工作。

我最接近的,因为 ANT 不会抛出任何错误,是这样的:

<replace file="${dir.pkg.tmp}config.xml" failOnNoReplacements="true">
    <replacetoken>
        <![CDATA[<access origin="http://example.com" subdomains="true" />]]>
    </replacetoken>
</replace>

理论上,这将取代指定的 <access ... />带有空字符串的标记,because I've omitted the value attribute .

不幸的是,由于failOnNoReplacements,它失败了。属性以及无论出于何种原因,它都没有进行任何替换的事实。

这可能吗?如果是这样,我做错了什么?!

最佳答案

如果您在标记和值中对 XML 字符串的元素进行编码,则 Ant 替换任务将起作用...例如:

在config.xml中

    <option name="some.unwanted.configuration" />

在 build.xml 中

    <replace file="${build.dir}/config.xml"
        token="&lt;option name=&quot;some.unwanted.configuration&quot; /&gt;" 
        value="&lt;!-- unwanted configuration removed --&gt;"
    />

结果

    <!-- unwanted configuration removed -->

关于xml - 使用 ANT 替换整个 XML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096332/

相关文章:

xml - 我可以在 *.xml 文件上使用 tr ("") 和 lupdate 吗?

c# - 将 ListView 列的宽度保存到 xml 文件

java - 如何将方法的输出传输到对话框?

ant - 批处理文件中的 ant 命令后命令被忽略

maven - 如何在 mvn assembly :single? 期间关联 Ant 任务

java - 在 NetBeans 和 Ant 中编译前过滤源

c# - Z 索引号对 RDLC 文件有什么影响?

Android:方向改变时缩放动画不起作用

xml - 仅在 IE10 中出现“XML5619 : Incorrect document syntax.”错误

java - 显示ant版本显示unable to locate tools.jar