使用Ant在XML文档中解析字符串 [英] Parsing a string within an XML document using Ant

查看:203
本文介绍了使用Ant在XML文档中解析字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下列文件:

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

我如何返回值:ID17

How do I return the value: id17

当我运行下面的目标在我的Ant文件:

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>

我得到 -

print:
        [echo] name = section,id17

有没有指定我只想资源身份证?

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

推荐答案

我有一个好消息和你坏消息。一个坏消息是,有没有外的即装即用的解决方案。好消息是, XMLProperty中任务是为保护暴露 processNode()方法相当扩展的感谢。这里是你可以做什么:

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。创建和使用的ant.jar(你可以找到一个在 LIB 在Ant分发或的从Maven的得到它)在类路径下code:

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。现在你只需要做这个任务可见蚂蚁。该simpliest方式:创建任务子目录,在你有你的ant脚本目录 - >用它复制编译MyXmlProp类的目录结构任务目录中,这样你应该是这样结束:任务/ PL / sobczyk /彼得/ MyXmlProp.class

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。导入任务Ant脚本,你应该是这样结束:

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。运行Ant,蚂蚁瞧,你应该看到: [回应] 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.

这篇关于使用Ant在XML文档中解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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