如何避免XML头,而在Ant脚本串联XML文件 [英] How to avoid xml header while concatenating xml files in ant script

查看:126
本文介绍了如何避免XML头,而在Ant脚本串联XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我串联一个文件夹中的所有XML文件到Ant脚本一个XML文件。虽然串联的XML文件,标题

 <?XML版本=1.0编码=UTF-8&GT?;

在所有XML文件越来越附加输出XMLFILE。

有没有办法来避免这个头?

 < CONCAT destfile =$ {} docbook.dir /all-sections.xml
      力=无>
   <文件集DIR =$ {} docbook.dir
     包括=节/ * XML。/>
   < / CONCAT>


解决方案

您可以将一个正则表达式丢弃头:

 < CONCAT destfile =$ {} docbook.dir /all-sections.xml力=无>
    <文件集DIR =$ {} docbook.dir包括= /&GT节/ * XML。
    < filterchain>
        < linecontainsregexp否定=真正的>
            <正则表达式模式=&放大器; LT; \\ XML版本/>
        < / linecontainsregexp>
    < / filterchain>
 < / CONCAT>

https://ant.apache.org/manual/Types/filterchain.html

编辑:如果你想保持头部的第一次出现,那么这是一个选项:

 <属性名=第一值=真/>< CONCAT destfile =$ {} docbook.dir /all-sections.xml>
    <文件集DIR =$ {} docbook.dir包括= /&GT节/ * XML。
    < filterchain>
        < scriptfilter语言=JavaScript的>
        <![CDATA [
            首先= project.getProperty(第一);
            如果(self.getToken()的indexOf(< \\ XML版本)!= -1){
                如果(第一==真){
                    project.setProperty(第一,假);
                }其他{
                    self.setToken(NULL);
                }
            }
        ]]>
        < / scriptfilter>
    < / filterchain>
< / CONCAT>

I am concatenating all the xml files in a folder into a single xml file in ant script. While concatenating the xml files, the header

 <?xml version="1.0" encoding="UTF-8" ?> 

in all xml files are getting appended in the output xmlfile.

Is there any way to avoid this header ?

  <concat destfile="${docbook.dir}/all-sections.xml"
      force="no">       
   <fileset dir="${docbook.dir}"
     includes="sections/*.xml"/>
   </concat>

解决方案

You can apply a regex to discard the header:

<concat destfile="${docbook.dir}/all-sections.xml" force="no">       
    <fileset dir="${docbook.dir}" includes="sections/*.xml"/>
    <filterchain>
        <linecontainsregexp negate="true">
            <regexp pattern="&lt;\?xml version"/>
        </linecontainsregexp>
    </filterchain>  
 </concat>

https://ant.apache.org/manual/Types/filterchain.html

EDIT: If you want to keep the first occurrence of the header then this is an option:

<property name="first" value="true"/>

<concat destfile="${docbook.dir}/all-sections.xml">       
    <fileset dir="${docbook.dir}" includes="sections/*.xml"/>
    <filterchain>
        <scriptfilter language="javascript">
        <![CDATA[
            first = project.getProperty("first");
            if(self.getToken().indexOf("<\?xml version") != -1) {
                if(first == "true") {
                    project.setProperty("first", "false");
                } else {
                    self.setToken(null);
                }
            }
        ]]> 
        </scriptfilter>             
    </filterchain>
</concat>

这篇关于如何避免XML头,而在Ant脚本串联XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆