英文:
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中的
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
--------
<data>123456789</data>
display.xml
-----------
<data>abcdefg</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
--------
<data>123456789</data>
display.xml
-----------
<data>123456789</data>
How can i solve this Issue? Thanks in advance
答案1
得分: 1
我没有找到名为GetXmlProperties
的任何Ant任务,但我认为你可能考虑的是XmlProperty
任务,它从解析XML文档中的属性序列中组合而来。
有两种方法可以实现你想要的目标(可能还有更多):
-
最基本的方法是仅使用
XmlProperty
任务来检索所需的值,然后在目标文件上使用Replace
任务,执行简单的字符串替换。在处理目标文件时,将其视为纯文本文件,而不必关心其中的XML逻辑。然而,对XML数据进行字符串匹配和替换既无趣又容易出错。 -
因此,我提出了第二种方法,即使用
XmlTask
,如下面的示例所示。将前缀xml
添加到我们新解析的属性中,可以更容易地将它们与其余部分区分开。出于演示目的,我们还让构建过程将所有新属性在控制台上以xml
前缀记录下来,使用EchoProperties
任务。
<project name="SO63816092" default="default">
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" />
<xmlproperty file="data.xml" prefix="xml" />
<echoproperties prefix="xml"/>
<target name="default">
<xmltask source="display.xml" dest="display.xml" failWithoutMatch="true">
<replace path="//data/text()" withText="${xml.data}" />
</xmltask>
</target>
</project>
使用第二种方法,使用以下输入文件data.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<data>123456789</data>
和以下目标文件display.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<data>abcdefg</data>
我可以成功地实现你所要求的,并使display.xml
变为:
<?xml version="1.0" encoding="UTF-8"?>
<data>123456789</data>
只需确保将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):
-
The most primitive would be to just use the
XmlProperty
task to retreive the value in question and use a crudeReplace
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. -
Thus I propose a second approach, which is to use the
XmlTask
, as per the following example. Adding a prefixxml
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 thexml
prefix to the console by using theEchoProperties
task.
<project name="SO63816092" default="default">
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" />
<xmlproperty file="data.xml" prefix="xml" />
<echoproperties prefix="xml"/>
<target name="default">
<xmltask source="display.xml" dest="display.xml" failWithoutMatch="true">
<replace path="//data/text()" withText="${xml.data}" />
</xmltask>
</target>
</project>
Using the second approach, while using the following input file data.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<data>123456789</data>
and the following destination file display.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<data>abcdefg</data>
I can successfully accomplish what you ask for and get display.xml
to become:
<?xml version="1.0" encoding="UTF-8"?>
<data>123456789</data>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论