本文介绍了Ant 和 XML 配置文件解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下形式的 XML 文件 -
I have an XML file of the following form -
<map MAP_XML_VERSION="1.0">
<entry key="database.user" value="user1"/>
...
</map>
ant 是否具有读取此内容的本机能力并让我执行 xquery 以拉回键的值?通过 API 我没有看到这样的功能.
Does ant have a native ability to read this and let me perform an xquery to pull back values for keys? Going through the API I did not see such capabilities.
推荐答案
您可以使用 scriptdef 标记为您的类创建一个 JavaScript 包装器.在 JS 中,您几乎拥有 Java 的全部功能,并且可以执行您想要的任何类型的复杂 XML 解析.
You can use the scriptdef tag to create a JavaScript wrapper for your class. Inside JS, you pretty much have the full power of Java and can do any kind of complicated XML parsing you want.
例如:
<project default="build">
<target name="build">
<xpath-query query="//entry[@key='database.user']/@value"
xmlFile="test.xml" addproperty="value"/>
<echo message="Value is ${value}"/>
</target>
<scriptdef name="xpath-query" language="javascript">
<attribute name="query"/>
<attribute name="xmlfile"/>
<attribute name="addproperty"/>
<![CDATA[
importClass(java.io.FileInputStream);
importClass(javax.xml.xpath.XPath);
importClass(javax.xml.xpath.XPathConstants);
importClass(javax.xml.xpath.XPathFactory);
importClass(org.xml.sax.InputSource);
var exp = attributes.get("query");
var filename = attributes.get("xmlfile");
var input = new InputSource(new FileInputStream(filename));
var xpath = XPathFactory.newInstance().newXPath();
var value = xpath.evaluate(exp, input, XPathConstants.STRING);
self.project.setProperty( attributes.get("addproperty"), value );
]]>
</scriptdef>
</project>
这篇关于Ant 和 XML 配置文件解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!