Package org.apache.velocity.tools
Class XmlUtils
- java.lang.Object
-
- org.apache.velocity.tools.XmlUtils
-
public final class XmlUtils extends java.lang.ObjectUtility class for simplifying parsing of xml documents. Documents are not validated, and loading of external files (xinclude, external entities, DTDs, etc.) are disabled.
- Since:
- 3.0
- Version:
- $$
- Author:
- Claude Brisson
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static javax.xml.parsers.DocumentBuilderFactorycreateDocumentBuilderFactory()static java.lang.StringgetAttribute(org.w3c.dom.Node node, java.lang.String attr)static java.lang.StringgetAttribute(org.w3c.dom.Node node, java.lang.String attr, java.lang.String def)Extracts an attribute from a node.static booleangetBoolAttribute(org.w3c.dom.Node node, java.lang.String attr)static booleangetBoolAttribute(org.w3c.dom.Node node, java.lang.String attr, boolean def)Retrieves an attribute as a boolean.static java.util.List<org.w3c.dom.Element>getElements(java.lang.String xpath, org.w3c.dom.Node context)Search for elements using an XPath expressionstatic intgetIntAttribute(org.w3c.dom.Node node, java.lang.String attr)static intgetIntAttribute(org.w3c.dom.Node node, java.lang.String attr, int def)static java.util.List<org.w3c.dom.Node>getNodes(java.lang.String xpath, org.w3c.dom.Node context)Search for nodes using an XPath expressionstatic booleanisXmlMimeType(java.lang.String mimeType)Checkes whether the given mime type is an XML formatstatic java.lang.StringnodePath(org.w3c.dom.Node n)Builds the xpath expression for a node (tries to use id/name nodes when possible to get a unique path)static java.lang.StringnodeToString(org.w3c.dom.Node node)XML Node to stringstatic org.w3c.dom.Elementparse(java.io.Reader xml)Attempts to parse the input xml into a single element.static org.w3c.dom.Elementparse(java.lang.String xml)Attempts to parse the input xml into a single element.static org.w3c.dom.NodeListsearch(java.lang.String xpath, org.w3c.dom.Node context)Search for nodes using an XPath expression
-
-
-
Method Detail
-
createDocumentBuilderFactory
public static final javax.xml.parsers.DocumentBuilderFactory createDocumentBuilderFactory()
-
getAttribute
public static java.lang.String getAttribute(org.w3c.dom.Node node, java.lang.String attr, java.lang.String def)Extracts an attribute from a node.- Parameters:
node- target nodeattr- attribute namedef- default value- Returns:
- The value of the attribute, or def
-
getAttribute
public static java.lang.String getAttribute(org.w3c.dom.Node node, java.lang.String attr)- Parameters:
node- target nodeattr- attribute name- Returns:
- The value of the given attribute, or null if not present.
-
getBoolAttribute
public static boolean getBoolAttribute(org.w3c.dom.Node node, java.lang.String attr, boolean def)Retrieves an attribute as a boolean.- Parameters:
node- target nodeattr- attribute namedef- default value- Returns:
- True if the attribute exists and is not equal to "false" false if equal to "false", and def if not present.
-
getBoolAttribute
public static boolean getBoolAttribute(org.w3c.dom.Node node, java.lang.String attr)- Parameters:
node- target nodeattr- attribute name- Returns:
- True if the attribute exists and is not equal to "false" false otherwise.
-
getIntAttribute
public static int getIntAttribute(org.w3c.dom.Node node, java.lang.String attr, int def)- Parameters:
node- target nodeattr- attribute namedef- default value- Returns:
- An attribute coerced to an integer.
-
getIntAttribute
public static int getIntAttribute(org.w3c.dom.Node node, java.lang.String attr)- Parameters:
node- target nodeattr- attribute name- Returns:
- An attribute coerced to an integer.
-
parse
public static org.w3c.dom.Element parse(java.io.Reader xml)
Attempts to parse the input xml into a single element.- Parameters:
xml- xml stream reader- Returns:
- The document object
-
parse
public static org.w3c.dom.Element parse(java.lang.String xml)
Attempts to parse the input xml into a single element.- Parameters:
xml- xml string- Returns:
- The document object
-
search
public static org.w3c.dom.NodeList search(java.lang.String xpath, org.w3c.dom.Node context) throws javax.xml.xpath.XPathExpressionExceptionSearch for nodes using an XPath expression- Parameters:
xpath- XPath expressioncontext- evaluation context- Returns:
- org.w3c.NodeList of found nodes
- Throws:
javax.xml.xpath.XPathExpressionException
-
getNodes
public static java.util.List<org.w3c.dom.Node> getNodes(java.lang.String xpath, org.w3c.dom.Node context) throws javax.xml.xpath.XPathExpressionExceptionSearch for nodes using an XPath expression- Parameters:
xpath- XPath expressioncontext- evaluation context- Returns:
- List of found nodes
- Throws:
javax.xml.xpath.XPathExpressionException
-
getElements
public static java.util.List<org.w3c.dom.Element> getElements(java.lang.String xpath, org.w3c.dom.Node context) throws javax.xml.xpath.XPathExpressionExceptionSearch for elements using an XPath expression- Parameters:
xpath- XPath expressioncontext- evaluation context- Returns:
- List of found elements
- Throws:
javax.xml.xpath.XPathExpressionException
-
nodePath
public static java.lang.String nodePath(org.w3c.dom.Node n)
Builds the xpath expression for a node (tries to use id/name nodes when possible to get a unique path)
- Parameters:
n- target node- Returns:
- node xpath
-
nodeToString
public static java.lang.String nodeToString(org.w3c.dom.Node node)
XML Node to string- Parameters:
node- XML node- Returns:
- XML node string representation
-
isXmlMimeType
public static boolean isXmlMimeType(java.lang.String mimeType)
Checkes whether the given mime type is an XML format- Parameters:
mimeType- mime type- Returns:
trueif this mime type is an XML format
-
-