public class DomUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
ENCODING |
static String |
INPUT_ENCODING |
| Modifier and Type | Method and Description |
|---|---|
static void |
addEntity(Element element,
String entity)
Add an entity to a specified Element.
|
static void |
addText(Element element,
String text)
Add Text object to an Element.
|
static void |
copyDocument(Document source,
Document target)
Copy an XML document, adding it as a child of the target document root
|
static void |
copyDocumentNode(Node source,
Document target)
Copy a Node from one source document, adding it to the document
root of a different, target Document
|
static Element |
createElement(Document document,
String name)
Create a new element
|
static Element |
createElement(Element parent,
String name)
Add a new element to the given parent
|
static Document |
createXmlDocument()
Start a new XML Document (with root name = xml)
|
static Document |
createXmlDocument(String rootName)
Start a new XML Document.
|
static String |
getAllText(Node parent)
Get any text associated with this element and it's children.
|
static String |
getAllTextAtNode(Node parent)
Get the text associated with this element, at all suboordinate levels
|
static String |
getAttribute(Element element,
String name)
Get an Attribute from an Element.
|
static Node |
getChildSiblingByName(Node currentNode,
String tagName)
Search for a named child of a given node
|
static Element |
getElement(Element element,
String name)
Return the first named Element found.
|
static NodeList |
getElementList(Element element,
String name)
Return a list of named Elements.
|
static NodeList |
getElementListNS(String namespace,
Element element,
String name)
Return a list of specified namespace:Elements
|
static Element |
getElementNS(String namespace,
Element element,
String name)
Return the first named Element found - namespace aware
|
static String |
getFirstText(Node parent)
Get the first text node associated with this element
|
static Node |
getNextNodeByName(Node currentNode,
String tagName)
Deprecated.
|
static Node |
getNextSiblingByName(Node currentNode,
String tagName)
Search our next siblings for a given node
|
static Node |
getPreviousNodeByName(Node currentNode,
String tagName)
Search up the tree for a given node
|
static Node |
getPreviousSiblingByName(Node currentNode,
String tagName)
Search earlier siblings for a given node
|
static String |
getText(Node parent)
Get the text associated with this element, at this level only
|
static String |
getText(Node parent,
String elementName)
Get the text associated with a specified element, at this level only
|
static String |
getTextNodeByNumber(Node parent,
int number)
Get the specified text node associated with this element
|
static String |
getTextNS(String namespace,
Node parent,
String elementName)
Get the text associated with a specified element, at this level
only - namespace aware
|
static DocumentBuilder |
getXmlDocumentBuilder()
Get a DOM Document builder.
|
static DocumentBuilder |
getXmlDocumentBuilder(boolean namespaceAware)
Get a DOM Document builder.
|
static DocumentBuilder |
getXmlDocumentBuilderNS()
Get a DOM Document builder - namespace aware
|
static String |
normalizeText(String update)
"Normalize" XML text node content to create a simple string
|
static String |
normalizeText(String original,
String update)
"Normalize" XML text node content to create a simple string
|
static Document |
parseHtmlBytes(byte[] html)
Parse HTML text (from a raw byte array) into a Document.
|
static Document |
parseHtmlString(String html)
Parse HTML text (from a String) into a Document.
|
static Document |
parseXmlBytes(byte[] xml)
Parse XML text (from a raw byte array) into a Document.
|
static Document |
parseXmlBytesNS(byte[] xml)
Parse XML text (from a raw byte array) into a Document - namespace aware.
|
static Document |
parseXmlFile(String filename)
Parse an XML file into a Document.
|
static Document |
parseXmlReader(Reader xmlReader)
Parse XML text (from a Reader) into a Document.
|
static Document |
parseXmlStream(InputStream xmlStream)
Parse XML text (from an input stream) into a Document.
|
static Document |
parseXmlStreamNS(InputStream xmlStream)
Parse XML text (from an input stream) into a Document - namespace aware.
|
static Document |
parseXmlString(String xml)
Parse XML text (from a string) into a Document.
|
Node |
removeNode(Node node)
Remove this node from its parent.
|
static List |
selectElementsByAttributeValue(Element element,
String name,
String attribute,
String value)
Return a list of named Elements with a specific attribute value.
|
static List |
selectElementsByAttributeValue(Element element,
String name,
String attribute,
String value,
boolean returnFirst)
Return a list of named Elements with a specific attribute value.
|
static List |
selectElementsByAttributeValueNS(String namespace,
Element element,
String name,
String attribute,
String value)
Return a list of named Elements with a specific attribute
value (namespace aware)
|
static List |
selectElementsByAttributeValueNS(String namespace,
Element element,
String name,
String attribute,
String value,
boolean returnFirst)
Return a list of named Elements with a specific attribute
value (namespace aware)
|
static Element |
selectFirstElementByAttributeValue(Element element,
String name,
String attribute,
String value)
Return the first named Element with a specific attribute value.
|
static Element |
selectFirstElementByAttributeValueNS(String namespace,
Element element,
String name,
String attribute,
String value)
Return the first named Element with a specific attribute
value (namespace aware)
namespace The namespace URI
|
static String |
serialize(Object object)
Write formatted XML text to a String.
|
static void |
serializeXml(Node node,
OutputStream target)
Write formatted XML text to supplied OutputStream.
|
static void |
serializeXml(Node node,
Writer writer)
Write formatted XML text to supplied Writer.
|
static void |
setAttribute(Element element,
String name,
String value)
Set an Attribute in an Element
|
static String |
textSearch(Node parent,
boolean recursiveSearch)
Get the text associated with this element at this level only, or
recursivley, searching through all child elements
|
public static final String INPUT_ENCODING
public static final String ENCODING
public static Element createElement(Document document, String name)
document - Document to contain the new elementname - the element namepublic static Element createElement(Element parent, String name)
parent - the parent Elementname - the child namepublic static void addText(Element element, String text)
element - the containing elementtext - the text to addpublic static void addEntity(Element element, String entity)
DomUtils.addEntity(element, "nbsp");)element - the containing elemententity - the entity to addpublic static String normalizeText(String update)
update - Text to add to the original stringpublic static String normalizeText(String original, String update)
original - Original textupdate - Text to add to the original stringpublic static String getText(Node parent)
parent - the node containing textpublic static String getText(Node parent, String elementName)
parent - the node containing textelementName - Element with the text we want to fetchpublic static String getTextNS(String namespace, Node parent, String elementName)
namespace - Namespace URIparent - the node containing textelementName - Element with the text we want to fetchpublic static String getAllTextAtNode(Node parent)
parent - the node containing textpublic static String textSearch(Node parent, boolean recursiveSearch)
parent - the node containing textrecursiveSearch - Search all child elements?public static String getFirstText(Node parent)
parent - the node containing textpublic static String getTextNodeByNumber(Node parent, int number)
parent - the node containing textnumber - The text node to fetch (1st, 2nd, etc)public static String getAllText(Node parent)
parent - the node containing textpublic static String getAttribute(Element element, String name)
element - the containing Elementname - the attribute namepublic static void setAttribute(Element element, String name, String value)
element - the containing Elementname - the attribute namevalue - the attribute valuepublic static NodeList getElementList(Element element, String name)
element - the containing Elementname - the tag namepublic static NodeList getElementListNS(String namespace, Element element, String name)
namespace - Namespace URIelement - the containing Elementname - the tag namepublic static List selectElementsByAttributeValue(Element element, String name, String attribute, String value)
element - the containing Elementname - the tag nameattribute - Attribute namevalue - Attribute valuepublic static Element selectFirstElementByAttributeValue(Element element, String name, String attribute, String value)
element - the containing Elementname - the tag nameattribute - Attribute namevalue - Attribute valuepublic static List selectElementsByAttributeValue(Element element, String name, String attribute, String value, boolean returnFirst)
element - the containing Elementname - the tag nameattribute - Attribute namevalue - Attribute valuereturnFirst - Return only the first matching value?public static List selectElementsByAttributeValueNS(String namespace, Element element, String name, String attribute, String value)
namespace - Namespace URIelement - the containing Elementname - the tag nameattribute - Attribute namevalue - Attribute valuepublic static Element selectFirstElementByAttributeValueNS(String namespace, Element element, String name, String attribute, String value)
element - the containing Elementname - the tag nameattribute - Attribute namevalue - Attribute valuepublic static List selectElementsByAttributeValueNS(String namespace, Element element, String name, String attribute, String value, boolean returnFirst)
namespace - Namespace URIelement - the containing Elementname - the tag nameattribute - Attribute namevalue - Attribute valuereturnFirst - Return only the first matching value?public static Element getElement(Element element, String name)
element - the containing Elementname - the tag namepublic static Element getElementNS(String namespace, Element element, String name)
namespace - Namespace URIelement - the containing Elementname - the tag namepublic Node removeNode(Node node)
node - the node to removepublic static Node getPreviousNodeByName(Node currentNode, String tagName)
currentNode - Starting point for our searchtagName - Node name to look uppublic static Node getPreviousSiblingByName(Node currentNode, String tagName)
currentNode - Starting point for our searchtagName - Node name to look uppublic static Node getNextSiblingByName(Node currentNode, String tagName)
currentNode - Starting point for our searchtagName - Node name to look uppublic static Node getNextNodeByName(Node currentNode, String tagName)
getNextSiblingByName(Node currentNode, String tagName)currentNode - Starting point for our searchtagName - Node name to look uppublic static Node getChildSiblingByName(Node currentNode, String tagName)
currentNode - Starting point for our searchtagName - Node name to look uppublic static DocumentBuilder getXmlDocumentBuilder() throws DomException
DomExceptionpublic static DocumentBuilder getXmlDocumentBuilderNS() throws DomException
DomExceptionpublic static DocumentBuilder getXmlDocumentBuilder(boolean namespaceAware) throws DomException
namespaceAware - true if we're to handle namespace detailsDomExceptionpublic static Document createXmlDocument() throws DomException
DomExceptionpublic static Document createXmlDocument(String rootName) throws DomException
rootName - The name of the Document root Element (created here)DomExceptionpublic static void copyDocument(Document source, Document target)
source - Document to copytarget - Document to contain copypublic static void copyDocumentNode(Node source, Document target)
source - Document to copytarget - Document to contain copypublic static Document parseXmlStream(InputStream xmlStream) throws DomException
xmlStream - The XML text streamDomExceptionpublic static Document parseXmlStreamNS(InputStream xmlStream) throws DomException
xmlStream - The XML text streamDomExceptionpublic static Document parseXmlReader(Reader xmlReader) throws DomException
xmlReader - The XML ReaderDomExceptionpublic static Document parseXmlBytes(byte[] xml) throws DomException
xml - The XML textDomExceptionpublic static Document parseXmlBytesNS(byte[] xml) throws DomException
xml - The XML textDomExceptionpublic static Document parseXmlString(String xml) throws DomException
xml - The XML textDomExceptionpublic static Document parseXmlFile(String filename) throws DomException
filename - - The filename to parseDomExceptionpublic static Document parseHtmlBytes(byte[] html) throws DomException
html - The HTML textDomException -
The used to be:
parseHtmlStream(new ByteArrayInputStream(html));
public static Document parseHtmlString(String html) throws DomException
html - The HTML textDomException -
This used to be:
return parseHtmlReader(new StringReader(html));
public static void serializeXml(Node node, OutputStream target) throws DomException
node - Node to writetarget - stream to write toDomExceptionpublic static void serializeXml(Node node, Writer writer) throws DomException
node - the Node to writewriter - Writer the document is written toDomExceptionpublic static String serialize(Object object) throws DomException
object - The XML Document, HTML Document, or Element to writeDomExceptionCopyright © 2004–2018 Indiana University Library Information Technology. All rights reserved.