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.
Modifier and Type | Method and Description |
---|---|
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
|
public static final DocumentBuilderFactory createDocumentBuilderFactory()
public static String getAttribute(Node node, String attr, String def)
node
- target nodeattr
- attribute namedef
- default valuepublic static String getAttribute(Node node, String attr)
node
- target nodeattr
- attribute namepublic static boolean getBoolAttribute(Node node, String attr, boolean def)
node
- target nodeattr
- attribute namedef
- default valuepublic static boolean getBoolAttribute(Node node, String attr)
node
- target nodeattr
- attribute namepublic static int getIntAttribute(Node node, String attr, int def)
node
- target nodeattr
- attribute namedef
- default valuepublic static int getIntAttribute(Node node, String attr)
node
- target nodeattr
- attribute namepublic static Element parse(Reader xml)
xml
- xml stream readerpublic static Element parse(String xml)
xml
- xml stringpublic static NodeList search(String xpath, Node context) throws XPathExpressionException
xpath
- XPath expressioncontext
- evaluation contextXPathExpressionException
public static List<Node> getNodes(String xpath, Node context) throws XPathExpressionException
xpath
- XPath expressioncontext
- evaluation contextXPathExpressionException
public static List<Element> getElements(String xpath, Node context) throws XPathExpressionException
xpath
- XPath expressioncontext
- evaluation contextXPathExpressionException
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)
n
- target nodepublic static String nodeToString(Node node)
node
- XML nodepublic static boolean isXmlMimeType(String mimeType)
mimeType
- mime typetrue
if this mime type is an XML formatCopyright © 2002–2021 The Apache Software Foundation. All rights reserved.