Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a string within an XML document using Ant

Within the following document:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="toc">section</string>
<string name="id">id17</string>
</resources>

How do I return the value: id17

When I run the following target in my Ant file:

  <target name="print"
    description="print the contents of the config.xml file in various ways" >

  <xmlproperty file="$config.xml" prefix="build"/>
  <echo message="name = ${build.resources.string}"/>
  </target>

I get -

print:
        [echo] name = section,id17

Is there a way to specify that I only want the resource "id"?

like image 917
VARoadstter Avatar asked May 14 '26 00:05

VARoadstter


1 Answers

I had a good news and a bad news for you. A bad news is that there is not out-of-the-box solution. The good news is that xmlproperty task is quite extendable thanks for exposing processNode() method as protected. Here's what you can do:

1. Create and compile with ant.jar (you can find one in lib subdirectory in your ant distribution or get it from Maven) on classpath the following code:

package pl.sobczyk.piotr;

import org.apache.tools.ant.taskdefs.XmlProperty;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class MyXmlProp extends XmlProperty{

@Override
public Object processNode(Node node, String prefix, Object container) {
    if(node.hasAttributes()){
        NamedNodeMap nodeAttributes = node.getAttributes();
        Node nameNode = nodeAttributes.getNamedItem("name");
        if(nameNode != null){
           String name = nameNode.getNodeValue();

           String value = node.getTextContent();
           if(!value.trim().isEmpty()){
               String propName = prefix + "[" + name + "]";
               getProject().setProperty(propName, value);
           }
       }
    }

    return super.processNode(node, prefix, container);
}

}

2. Now you only need to make this task visible to ant. The simpliest way: create task subdirectory in directory where you have your ant script -> copy compiled MyXmlProp class with it's directory structure to task directory so you should end up with something like: task/pl/sobczyk/peter/MyXmlProp.class.

3. Import task to your ant script, you should end up with something like:

<target name="print">
  <taskdef name="myxmlproperty" classname="pl.sobczyk.piotr.MyXmlProp">
    <classpath>
      <pathelement location="task"/>
    </classpath>
  </taskdef>
  <myxmlproperty file="config.xml" prefix="build"/>
  <echo message="name = ${build.resources.string[id]}"/>
</target>

4. Run ant, ant voila, you should see: [echo] name = id17

What we did here is defining a special fancy square brackets syntax for your specific case :-). For some more general solution task extension may be a little more complex, but everything is possible :). Good luck.

like image 108
Piotr Sobczyk Avatar answered May 15 '26 15:05

Piotr Sobczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!