how can i get a matching string from a xml file using <getxmlproperties> tag using ant and replace the matched string in other xml file

huangapple go评论76阅读模式
英文:

how can i get a matching string from a xml file using <getxmlproperties> tag using ant and replace the matched string in other xml file

问题

我的要求是匹配data.xml文件中的以下标签,并使用ant中的替换display.xml文件中的内容。

data.xml

123456789

display.xml

abcdefg

我需要匹配data.xml文件中的内容,并将其替换为display.xml文件中的内容。

我的最终输出应该是:

data.xml

123456789

display.xml

123456789

如何解决这个问题?提前感谢。

英文:

My requirement is to match following <data> tag in data.xml file and replace the content in display.xml file using <getxmlproperties> in ant

data.xml
--------

&lt;data&gt;123456789&lt;/data&gt;

display.xml
----------- 

&lt;data&gt;abcdefg&lt;/data

I need to match the content in data.xml file and replace the it in display.xml file.

my final output should be like:

data.xml
--------
&lt;data&gt;123456789&lt;/data&gt;

display.xml
-----------
&lt;data&gt;123456789&lt;/data&gt;

How can i solve this Issue? Thanks in advance

答案1

得分: 1

我没有找到名为GetXmlProperties的任何Ant任务,但我认为你可能考虑的是XmlProperty任务,它从解析XML文档中的属性序列中组合而来。

有两种方法可以实现你想要的目标(可能还有更多):

  1. 最基本的方法是仅使用XmlProperty任务来检索所需的值,然后在目标文件上使用Replace任务,执行简单的字符串替换。在处理目标文件时,将其视为纯文本文件,而不必关心其中的XML逻辑。然而,对XML数据进行字符串匹配和替换既无趣又容易出错。

  2. 因此,我提出了第二种方法,即使用XmlTask,如下面的示例所示。将前缀xml添加到我们新解析的属性中,可以更容易地将它们与其余部分区分开。出于演示目的,我们还让构建过程将所有新属性在控制台上以xml前缀记录下来,使用EchoProperties任务。

&lt;project name=&quot;SO63816092&quot; default=&quot;default&quot;&gt;

  &lt;taskdef name=&quot;xmltask&quot; classname=&quot;com.oopsconsultancy.xmltask.ant.XmlTask&quot; /&gt;

  &lt;xmlproperty file=&quot;data.xml&quot; prefix=&quot;xml&quot; /&gt;

  &lt;echoproperties prefix=&quot;xml&quot;/&gt;

  &lt;target name=&quot;default&quot;&gt;
    &lt;xmltask source=&quot;display.xml&quot; dest=&quot;display.xml&quot; failWithoutMatch=&quot;true&quot;&gt;
      &lt;replace path=&quot;//data/text()&quot; withText=&quot;${xml.data}&quot; /&gt;
    &lt;/xmltask&gt;
  &lt;/target&gt;

&lt;/project&gt;

使用第二种方法,使用以下输入文件data.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;data&gt;123456789&lt;/data&gt;

和以下目标文件display.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;data&gt;abcdefg&lt;/data&gt;

我可以成功地实现你所要求的,并使display.xml变为:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;data&gt;123456789&lt;/data&gt;

只需确保将XmlTask的Java jar文件放置在当前Ant进程的类路径中,并注意,根据你的Ant文档结构的需要,可能需要更改我们使用的XPath表达式//data/text(),因为现在的方式会替换整个display.xml文档中找到的所有data元素的值。

英文:

I did not find any Ant task named GetXmlProperties, but I think you may have thought about this one, XmlProperty, which composes a sequence of properties from parsing an XML document.

Two ways, to achieve what you want, come to my mind (there may be more):

  1. The most primitive would be to just use the XmlProperty task to retreive the value in question and use a crude Replace task on the destination file, doing a simple string replace, by handling the destination as a plain text file, instead of caring for the XML logic in it. However, doing string match and replace with XML data is no fun and error prone.

  2. Thus I propose a second approach, which is to use the XmlTask, as per the following example. Adding a prefix xml to our newly parsed properties makes it easier to distinguish them from the rest. For demo purposes we also let the build process log all the new properties under the xml prefix to the console by using the EchoProperties task.

&lt;project name=&quot;SO63816092&quot; default=&quot;default&quot;&gt;

  &lt;taskdef name=&quot;xmltask&quot; classname=&quot;com.oopsconsultancy.xmltask.ant.XmlTask&quot; /&gt;

  &lt;xmlproperty file=&quot;data.xml&quot; prefix=&quot;xml&quot; /&gt;

  &lt;echoproperties prefix=&quot;xml&quot;/&gt;

  &lt;target name=&quot;default&quot;&gt;
    &lt;xmltask source=&quot;display.xml&quot; dest=&quot;display.xml&quot; failWithoutMatch=&quot;true&quot;&gt;
      &lt;replace path=&quot;//data/text()&quot; withText=&quot;${xml.data}&quot; /&gt;
    &lt;/xmltask&gt;
  &lt;/target&gt;

&lt;/project&gt;

Using the second approach, while using the following input file data.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;data&gt;123456789&lt;/data&gt;

and the following destination file display.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;data&gt;abcdefg&lt;/data&gt;

I can successfully accomplish what you ask for and get display.xml to become:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;data&gt;123456789&lt;/data&gt;

Just remember to place the XmlTask Java jar in the current classpath for your Ant process and note, that you may need to change the XPath expression, we use, //data/text(), to something more refined, should your document structure demand it, because the way, it is now, it would replace the value for all data elements it finds, throughout the whole display.xml document.

huangapple
  • 本文由 发表于 2020年9月10日 00:48:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63816092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定