类 XmlTool
java.lang.Object
org.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.generic.XmlTool
- 所有已实现的接口:
Serializable
Tool for reading/navigating XML files, with XPath expressions support.
Configuration parameters:
resource=file or classpath resourcesource=URL
Here's a short example:
XML file:
<foo><bar>woogie</bar><a name="test"/></foo>
Template:
$foo.bar.text
$foo.find('a')
$foo.a.name
Output:
woogie
<a name="test"/>
test
Configuration:
<tools>
<toolbox scope="application">
<tool class="org.apache.velocity.tools.generic.XmlTool"
key="foo" source="doc.xml"/>
</toolbox>
</tools>
Note that this tool is included in the default GenericTools configuration
under the key "xml". You can read but unless you set safeMode="false" for it, you will
only be able to parse XML strings. Safe mode is on by default and blocks
access to the read(String) method.
- 从以下版本开始:
- VelocityTools 2.0
- 版本:
- $Revision: 1769055 $ $Date: 2006-11-27 10:49:37 -0800 (Mon, 27 Nov 2006) $
- 作者:
- Nathan Bubna, Claude Brisson
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明static classIterator implementation that wraps a Node list iterator to return new XmlTool instances for each item in the wrapped iterator.s -
字段概要
字段从类继承的字段 org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明children()protected voidconfigure(ValueParser values) Configuration.Reads and parses a remote or local URLConverts the specified object to a String and callsfind(String)with that.This will first attempt to find an attribute with the specified name and return its value.getFirst()getLast()getName()Asksget(Object)for a "name" result.Returns the name of the root node.getPath()Returns the XPath that identifies the first/soleNoderepresented by this instance.getText()Returns the concatenated text content of all the internally held nodes.protected voidinitializeImportSupport(ValueParser config) ImportSupport initializationbooleanisEmpty()Returnstrueif there are noNodes internally held by this instance.iterator()node()Returns the first/soleNodefrom this instance's internal Node list, if any.parents()Reads and parses a local resource fileprotected voidSets a singular rootNodefor this instance.intsize()Returns the number ofNodes internally held by this instance.toString()If this instance has no XMLNodes, then this returns the result ofsuper.toString().从类继承的方法 org.apache.velocity.tools.generic.SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
字段详细资料
-
importSupport
ImportSupport utility which provides underlying i/o
-
-
构造器详细资料
-
XmlTool
public XmlTool()Default constructor. -
XmlTool
Builds an XmlTool around a node.- 参数:
node- target node
-
XmlTool
Builds an XmlTool around a nodes list.- 参数:
nodes- nodes list
-
-
方法详细资料
-
initializeImportSupport
ImportSupport initialization- 参数:
config- configuration values
-
configure
Configuration.- 覆盖:
configure在类中SafeConfig- 参数:
values- configuration values
-
setRoot
Sets a singular rootNodefor this instance.- 参数:
node- new root node
-
parse
- 参数:
xml- XML string- 返回:
- new XmlTool
-
read
Reads and parses a local resource file- 参数:
resource- resource path- 返回:
- new XmlTool
-
fetch
Reads and parses a remote or local URL- 参数:
url- resource URL- 返回:
- new XmlTool
-
get
This will first attempt to find an attribute with the specified name and return its value. If no such attribute exists or its value isnull, this will attempt to convert the given value to aNumberand get the result ofget(Number). If the number conversion fails, then this will convert the object to a string. If that string does not contain a '/', it appends the result ofgetPath()and a '/' to the front of it. Finally, it delegates the string to thefind(String)method and returns the result of that.- 参数:
o- attribute name, number, xpath relative expression- 返回:
- found value or null
-
getName
Asksget(Object)for a "name" result. If none, this will return the result ofgetNodeName().- 返回:
- found value or null
-
getNodeName
Returns the name of the root node. If the internalNodelist has more than oneNode, it will only return the name of the first node in the list.- 返回:
- node name
-
getPath
Returns the XPath that identifies the first/soleNoderepresented by this instance.- 返回:
- xpath node path
-
attr
Returns the value of the specified attribute for the first/soleNodein the internal Node list for this instance, if that Node is anElement. If it is a non-Element node type or there is no value for that attribute in this element, then this will returnnull.- 参数:
o- attribute name- 返回:
- attribute value or null
-
attributes
Returns aMapof all attributes for the first/soleNodeheld internally by this instance. If that Node is not anElement, this will return null.- 返回:
- attributes map
-
isEmpty
public boolean isEmpty()Returnstrueif there are noNodes internally held by this instance.- 返回:
- whether the set of current nodes is empty
-
size
public int size()Returns the number ofNodes internally held by this instance.- 返回:
- the size of the set of current nodes
-
iterator
Returns anIteratorthat returns newXmlToolinstances for eachNodeheld internally by this instance.- 返回:
- an iterator on wrappers of current nodes or null
-
getFirst
- 返回:
- a wrapper on the first node or null
-
getLast
- 返回:
- a wrapper on the last node or null
-
get
- 参数:
n- node index in current nodes list- 返回:
- a wrapper on the nth node or null
-
node
Returns the first/soleNodefrom this instance's internal Node list, if any.- 返回:
- the unwrapped first node or null
-
find
Converts the specified object to a String and callsfind(String)with that.- 参数:
o- xpath to search- 返回:
- a wrapper on found nodes or null
-
find
Performs an XPath selection on the current set ofNodes held by this instance and returns a newXmlToolinstance that wraps those results. If the specified value is null or this instance does not currently hold any nodes, then this will returnnull. If the specified value, when converted to a string, does not contain a '/' character, then it has "//" prepended to it. This means that a call to$xml.find("a")is equivalent to calling$xml.find("//a"). The full range of XPath selectors is supported here.- 参数:
xpath- xpath to search- 返回:
- a wrapper on found nodes or null
-
getParent
Returns a newXmlToolinstance that wraps the parentElementof the first/soleNodebeing wrapped by this instance.- 返回:
- wrapper on the parent of the first node or null
-
parents
Returns a newXmlToolinstance that wraps the parentElements of each of theNodes being wrapped by this instance. This does not return all ancestors, just the immediate parents.- 返回:
- a wrapper on the set of parents nodes or null
-
children
Returns a newXmlToolinstance that wraps all the childElements of all the current internally held nodes that areElements themselves.- 返回:
- a wrapper on the children of current nodes
-
getText
Returns the concatenated text content of all the internally held nodes. Obviously, this is most useful when only one node is held.- 返回:
- text of current nodes or null
-
toString
-