org.apache.velocity.tools

## Class XmlUtils

- java.lang.Object
- - org.apache.velocity.tools.XmlUtils

    public final class XmlUtils
    extends Object

Utility 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
`static DocumentBuilderFactory` `createDocumentBuilderFactory()`
`static String` `getAttribute(Node node, String attr)`
`static String` `getAttribute(Node node, String attr, String def)` Extracts an attribute from a node.
`static boolean` `getBoolAttribute(Node node, String attr)`
`static boolean` `getBoolAttribute(Node node, String attr, boolean def)` Retrieves an attribute as a boolean.
`static List<Element>` `getElements(String xpath, Node context)` Search for elements using an XPath expression
`static int` `getIntAttribute(Node node, String attr)`
`static int` `getIntAttribute(Node node, String attr, int def)`
`static List<Node>` `getNodes(String xpath, Node context)` Search for nodes using an XPath expression
`static boolean` `isXmlMimeType(String mimeType)` Checkes whether the given mime type is an XML format
`static String` `nodePath(Node n)` Builds the xpath expression for a node (tries to use id/name nodes when possible to get a unique path)
`static String` `nodeToString(Node node)` XML Node to string
`static Element` `parse(Reader xml)` Attempts to parse the input xml into a single element.
`static Element` `parse(String xml)` Attempts to parse the input xml into a single element.
`static NodeList` `search(String xpath, Node context)` Search for nodes using an XPath expression

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### createDocumentBuilderFactory

    public static final DocumentBuilderFactory createDocumentBuilderFactory()

#### getAttribute

    public static String getAttribute(Node node,
                                      String attr,
                                      String def)

Extracts an attribute from a node.

Parameters:
  `node` - target node
  `attr` - attribute name
  `def` - default value
Returns:
  The value of the attribute, or def

#### getAttribute

    public static String getAttribute(Node node,
                                      String attr)

Parameters:
  `node` - target node
  `attr` - attribute name
Returns:
  The value of the given attribute, or null if not present.

#### getBoolAttribute

    public static boolean getBoolAttribute(Node node,
                                           String attr,
                                           boolean def)

Retrieves an attribute as a boolean.

Parameters:
  `node` - target node
  `attr` - attribute name
  `def` - 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(Node node,
                                           String attr)

Parameters:
  `node` - target node
  `attr` - attribute name
Returns:
  True if the attribute exists and is not equal to "false" false otherwise.

#### getIntAttribute

    public static int getIntAttribute(Node node,
                                      String attr,
                                      int def)

Parameters:
  `node` - target node
  `attr` - attribute name
  `def` - default value
Returns:
  An attribute coerced to an integer.

#### getIntAttribute

    public static int getIntAttribute(Node node,
                                      String attr)

Parameters:
  `node` - target node
  `attr` - attribute name
Returns:
  An attribute coerced to an integer.

#### parse

    public static Element parse(Reader xml)

Attempts to parse the input xml into a single element.

Parameters:
  `xml` - xml stream reader
Returns:
  The document object

#### parse

    public static Element parse(String xml)

Attempts to parse the input xml into a single element.

Parameters:
  `xml` - xml string
Returns:
  The document object

#### search

    public static NodeList search(String xpath,
                                  Node context)
                           throws XPathExpressionException

Search for nodes using an XPath expression

Parameters:
  `xpath` - XPath expression
  `context` - evaluation context
Returns:
  org.w3c.NodeList of found nodes
Throws:
  `XPathExpressionException`

#### getNodes

    public static List<Node> getNodes(String xpath,
                                      Node context)
                               throws XPathExpressionException

Search for nodes using an XPath expression

Parameters:
  `xpath` - XPath expression
  `context` - evaluation context
Returns:
  List of found nodes
Throws:
  `XPathExpressionException`

#### getElements

    public static List<Element> getElements(String xpath,
                                            Node context)
                                     throws XPathExpressionException

Search for elements using an XPath expression

Parameters:
  `xpath` - XPath expression
  `context` - evaluation context
Returns:
  List of found elements
Throws:
  `XPathExpressionException`

#### nodePath

    public static String nodePath(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 String nodeToString(Node node)

XML Node to string

Parameters:
  `node` - XML node
Returns:
  XML node string representation

#### isXmlMimeType

    public static boolean isXmlMimeType(String mimeType)

Checkes whether the given mime type is an XML format

Parameters:
  `mimeType` - mime type
Returns:
  `true` if this mime type is an XML format