English 中文(简体)
利用安东尼奥·马列里文件进行扼杀
原标题:Parsing a string within an XML document using Ant

在以下文件中:

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

我如何回馈价值:id17

我在安文档案中提出了以下目标:

  <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

是否有办法规定,我只想“id”资源?

最佳回答

我给你带来了好消息和坏消息。 一个坏消息是,没有箱外解决办法。 好消息是<条码>xmlproperty。 由于暴露了保护的<代码>Node()方法,这项任务相当可扩展。 在这方面,你可以做些什么:

1. 导言 • 制作和编辑ant.jar(在以下表格中可找到一个:,从Maven上找到):

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. 结 论 现在,你只需要把这项任务看上去。 简便方式:在名录中建立<条码>task的子中,有您的附本——和;拷贝汇编了MyXmlProp级,其目录结构为task。 页: 1

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. 第四条第4款 页: 1

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.

问题回答

暂无回答




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签