public class DOMUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static void |
addDescendants(org.w3c.dom.Node node,
java.util.List<org.w3c.dom.Node> descendants)
Adds descendant elements of a node to a list.
|
static java.util.Map<java.lang.String,java.lang.String> |
attributesToSortedMap(org.xml.sax.Attributes attributes)
Convert attributes list to sorted map.
|
static org.w3c.dom.Element |
findChild(org.w3c.dom.Node parent,
java.lang.String namePat)
Find child node name matching regular expression.
|
static java.util.List<org.w3c.dom.Node> |
findChildren(org.w3c.dom.Node node,
java.lang.String name)
Gets the child elements of a node by name pattern.
|
static java.lang.String |
getAllText(org.w3c.dom.Node node)
Gets all the text for a node.
|
static org.w3c.dom.Element |
getChild(org.w3c.dom.Node node,
java.lang.String name)
Gets a child element of a node by name.
|
static org.w3c.dom.Element |
getChild(org.w3c.dom.Node node,
java.lang.String name,
java.lang.String attrName,
java.lang.String attrValue)
Gets a child element of a node by name and attribute value.
|
static java.util.List<org.w3c.dom.Node> |
getChildren(org.w3c.dom.Node node,
java.lang.String name)
Gets the child elements of a node by name.
|
static java.util.List<org.w3c.dom.Element> |
getChildren(org.w3c.dom.Node node,
java.lang.String name,
java.lang.String attrName,
java.lang.String attrValue)
Gets the child elements of a node by name and attribute value.
|
static org.w3c.dom.Element |
getDescendant(org.w3c.dom.Node node,
java.lang.String path)
Gets a descendant element of a node.
|
static java.util.List<org.w3c.dom.Node> |
getDescendants(org.w3c.dom.Node node)
Gets descendant elements of a node.
|
static java.util.List<org.w3c.dom.Node> |
getDescendants(org.w3c.dom.Node node,
java.lang.String path)
Gets descendant elements of a node.
|
static org.w3c.dom.Element |
getLastChild(org.w3c.dom.Node node,
java.lang.String name)
Gets the last child element of a node by name.
|
static org.w3c.dom.NodeList |
getNodesByTagName(org.w3c.dom.Document document,
java.lang.String[] tagNames)
Get list of nodes by one or more tag names.
|
static java.lang.String |
getText(org.w3c.dom.Node node)
Gets text for a node.
|
static org.w3c.dom.Document |
newDocument()
Creates a new empty DOM document.
|
static boolean |
nodeHasDescendant(org.w3c.dom.Node node,
java.lang.String[] names1,
java.lang.String[] names2)
Checks to see if a node has a descendant node of some name, other
than those in children of some other name.
|
static org.w3c.dom.Document |
parse(java.io.File file)
Parses an XML file.
|
static org.w3c.dom.Document |
parse(java.lang.String path)
Parses an XML file.
|
static org.w3c.dom.Document |
parse(java.net.URL url)
Parses XML document from URL.
|
static org.w3c.dom.Document |
parseText(java.lang.String text)
Parses XML document from a string.
|
static org.w3c.dom.DocumentFragment |
parseXML(org.w3c.dom.Document doc,
java.lang.String fragment)
Parse a string containing XML and return a DocumentFragment.
|
static void |
save(org.w3c.dom.Document document,
java.lang.String path)
Saves a DOM document to an XML file in utf-8.
|
static void |
save(org.w3c.dom.Document document,
java.lang.String dtdName,
java.lang.String path)
Saves a DOM document to an XML file in utf-8.
|
static java.lang.String |
saveToString(org.w3c.dom.Document document)
Saves a DOM document to a String.
|
static java.lang.String |
saveToString(org.w3c.dom.Document document,
java.lang.String dtdName)
Saves a DOM document to a String with a specified DTD name.
|
static void |
setText(org.w3c.dom.Node node,
java.lang.String text)
Sets text for a node.
|
static void |
validateDocument(org.w3c.dom.Document document,
javax.xml.validation.Schema schema)
Validate a DOM document against a schema.
|
public static org.w3c.dom.Document parse(java.io.File file)
throws java.io.IOException,
javax.xml.parsers.ParserConfigurationException,
org.xml.sax.SAXException
file - File.java.lang.Exceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXExceptionpublic static org.w3c.dom.Document parse(java.lang.String path)
throws java.io.IOException,
javax.xml.parsers.ParserConfigurationException,
org.xml.sax.SAXException
path - File path.java.lang.Exceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXExceptionpublic static org.w3c.dom.Document parse(java.net.URL url)
throws java.io.IOException,
javax.xml.parsers.ParserConfigurationException,
org.xml.sax.SAXException
url - URL.java.lang.Exceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXExceptionpublic static org.w3c.dom.Document parseText(java.lang.String text)
throws java.io.IOException,
javax.xml.parsers.ParserConfigurationException,
org.xml.sax.SAXException
text - Document text string.java.lang.Exceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXExceptionpublic static org.w3c.dom.Element getChild(org.w3c.dom.Node node,
java.lang.String name)
node - Node.name - Name.public static org.w3c.dom.Element findChild(org.w3c.dom.Node parent,
java.lang.String namePat)
parent - Node whose child we want.namePat - Regular expression for child name.public static org.w3c.dom.Element getLastChild(org.w3c.dom.Node node,
java.lang.String name)
node - Node.name - Name.public static org.w3c.dom.Element getChild(org.w3c.dom.Node node,
java.lang.String name,
java.lang.String attrName,
java.lang.String attrValue)
node - Node.name - Name.attrName - Attribute name.attrValue - Attribute value.public static java.lang.String getText(org.w3c.dom.Node node)
node - Node.public static void setText(org.w3c.dom.Node node,
java.lang.String text)
Sets the value of the first child text node, if any. Creates new child text node if none found.
node - Node.text - New text for the node.public static java.lang.String getAllText(org.w3c.dom.Node node)
node - Node.public static java.util.List<org.w3c.dom.Node> getChildren(org.w3c.dom.Node node,
java.lang.String name)
node - Node.name - Name.public static java.util.List<org.w3c.dom.Node> findChildren(org.w3c.dom.Node node,
java.lang.String name)
node - Node.name - Name as a regular expression.public static java.util.List<org.w3c.dom.Element> getChildren(org.w3c.dom.Node node,
java.lang.String name,
java.lang.String attrName,
java.lang.String attrValue)
node - Node.name - Name.attrName - Attribute name.attrValue - Attribute value.public static org.w3c.dom.Element getDescendant(org.w3c.dom.Node node,
java.lang.String path)
node - Node.path - Path to descendant, using tag names of child
elements separated by "/".public static java.util.List<org.w3c.dom.Node> getDescendants(org.w3c.dom.Node node,
java.lang.String path)
node - Node.path - Path to descendants, using tag names of child
elements separated by "/".public static void addDescendants(org.w3c.dom.Node node,
java.util.List<org.w3c.dom.Node> descendants)
node - Node.descendants - List of descendants.public static java.util.List<org.w3c.dom.Node> getDescendants(org.w3c.dom.Node node)
node - Node.public static org.w3c.dom.Document newDocument()
throws javax.xml.parsers.ParserConfigurationException
javax.xml.parsers.ParserConfigurationExceptionpublic static org.w3c.dom.DocumentFragment parseXML(org.w3c.dom.Document doc,
java.lang.String fragment)
doc - The document,fragment - The XML text fragment.public static void save(org.w3c.dom.Document document,
java.lang.String path)
throws javax.xml.transform.TransformerException,
java.io.IOException
document - DOM document.path - Output file path.TransformerException, - IOExceptionjavax.xml.transform.TransformerExceptionjava.io.IOExceptionpublic static void save(org.w3c.dom.Document document,
java.lang.String dtdName,
java.lang.String path)
throws javax.xml.transform.TransformerException,
java.io.IOException
document - DOM document.dtdName - The DTD name.path - Output file path.TransformerException, - IOExceptionjavax.xml.transform.TransformerExceptionjava.io.IOExceptionpublic static java.lang.String saveToString(org.w3c.dom.Document document)
throws javax.xml.transform.TransformerException,
java.io.IOException
document - DOM document.TransformerException, - IOExceptionjavax.xml.transform.TransformerExceptionjava.io.IOExceptionpublic static java.lang.String saveToString(org.w3c.dom.Document document,
java.lang.String dtdName)
throws javax.xml.transform.TransformerException,
java.io.IOException
document - DOM document.dtdName - The DTD name.TransformerException, - IOExceptionjavax.xml.transform.TransformerExceptionjava.io.IOExceptionpublic static boolean nodeHasDescendant(org.w3c.dom.Node node,
java.lang.String[] names1,
java.lang.String[] names2)
node - Nodenames1 - Array of names to include.names2 - Array of names to exclude.public static void validateDocument(org.w3c.dom.Document document,
javax.xml.validation.Schema schema)
throws org.xml.sax.SAXException,
java.io.IOException
document - The parsed DOM document to validate.schema - A parsed schema.java.io.IOException - if the validation fails.org.xml.sax.SAXException - if the validation fails.
Simply returns without error if validation succeeds.
public static java.util.Map<java.lang.String,java.lang.String> attributesToSortedMap(org.xml.sax.Attributes attributes)
attributes - Attributes.public static org.w3c.dom.NodeList getNodesByTagName(org.w3c.dom.Document document,
java.lang.String[] tagNames)
document - Document in which to extract nodes.tagNames - String array of tag names for which to
extract matching nodes.