简体   繁体   中英

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

My requirement is to match following tag in data.xml file and replace the content in display.xml file using 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

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.

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM