info.aduna.xml
Class NodeUtil

java.lang.Object
  extended by info.aduna.xml.NodeUtil

public class NodeUtil
extends Object

Defines some util methods on DOM nodes.


Constructor Summary
NodeUtil()
           
 
Method Summary
static Element getChildElement(Node node, int index)
          Returns the n-th child element.
static Element getChildElement(Node node, String name)
          Returns the first child element with the given name.
static List<Element> getChildElements(Node node)
          Get all child elements of the given node.
static List<Element> getChildElements(Node node, String name)
          Get all childELEMENTS with name 'name' from the given node.
static List<Node> getChildNodes(Node node, String name)
           
static List<Element> getDescendingElements(Node node, String name)
          Get all descending elements from the given node with the given name.
static List<Node> getDescendingNodes(Node node, String name)
          Get all descending nodes from the given node with the given name.
static Element getLastChildElement(Node node)
          Returns the last child element of the specified node.
static String getText(Node node)
          Get all text-content of a node.
static String getText(Node node, boolean spaces)
           
static void getText(Node node, StringBuilder buffer, boolean spaces)
          Add all text-content of a node to a stringBuilder.
static String toLegalXMLString(String s)
           
static void writeAttribute(Attr attribute, PrintStream output)
           
static void writeCDATA(CDATASection cdata, PrintStream output)
           
static void writeChildren(Node node, PrintStream output)
           
static void writeComment(Comment comment, PrintStream output)
           
static void writeDocument(Document document, PrintStream output)
           
static void writeDocumentFragment(DocumentFragment documentFragment, PrintStream output)
           
static void writeDocumentType(DocumentType documentType, PrintStream output)
           
static void writeElement(Element element, PrintStream output)
           
static void writeEntity(Entity entity, PrintStream output)
           
static void writeEntityReference(EntityReference entityReference, PrintStream output)
           
static void writeNotation(Notation notation, PrintStream output)
           
static void writeProcessingInstruction(ProcessingInstruction instr, PrintStream output)
           
static void writeText(Text text, PrintStream output)
           
static void writeXML(Node node, OutputStream output)
          Write the contents of a DOM (sub)tree to a stream.
static void writeXMLNode(Node node, PrintStream output)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NodeUtil

public NodeUtil()
Method Detail

getChildElements

public static List<Element> getChildElements(Node node)
Get all child elements of the given node.

Parameters:
node - the node to get the childelements from.
Returns:
all childnodes that are element nodes.

getDescendingElements

public static List<Element> getDescendingElements(Node node,
                                                  String name)
Get all descending elements from the given node with the given name.

Parameters:
node - the parent node
name - the tagname of the elements that are returned
Returns:
A List of Element's

getDescendingNodes

public static List<Node> getDescendingNodes(Node node,
                                            String name)
Get all descending nodes from the given node with the given name.

Parameters:
node - the node to get the descending nodes from.
name - the name of the nodes that are returned
Returns:
all descending nodes with the correct name.

getChildNodes

public static List<Node> getChildNodes(Node node,
                                       String name)

getChildElements

public static List<Element> getChildElements(Node node,
                                             String name)
Get all childELEMENTS with name 'name' from the given node.

Parameters:
node - the node to get the childelements from.
Returns:
all childnodes that are an elementnode.

getChildElement

public static Element getChildElement(Node node,
                                      String name)
Returns the first child element with the given name. Returns 'null' when there is no such element.


getChildElement

public static Element getChildElement(Node node,
                                      int index)
Returns the n-th child element. Returns 'null' when there is no such element.

Throws:
IllegalArgumentException - when index < 0

getLastChildElement

public static Element getLastChildElement(Node node)
Returns the last child element of the specified node. Returns 'null' when the node has no child elements.


getText

public static String getText(Node node)
Get all text-content of a node. All child-elements are recursively searched for TextNodes. The data of the textnodes is added to the result string.

Parameters:
node - the node to get the text from.
Returns:
result a string containing all text of the node with the markup removed.

getText

public static String getText(Node node,
                             boolean spaces)

getText

public static void getText(Node node,
                           StringBuilder buffer,
                           boolean spaces)
Add all text-content of a node to a stringBuilder. All childelements are recursively searched for TextNodes.

Parameters:
node - the node to get the text from.
buffer - a stringbuilder to add the text to.
spaces - indicates whether to put spaces between the TEXT nodes, before concatenating their content. This is useful if you want to know where words begin and end in your original text.

toLegalXMLString

public static String toLegalXMLString(String s)

writeXML

public static void writeXML(Node node,
                            OutputStream output)
Write the contents of a DOM (sub)tree to a stream. This method puts no extra whitespace in the document, desired whitespace should instead be present in the given node (perhaps this might become an option in the future).


writeXMLNode

public static void writeXMLNode(Node node,
                                PrintStream output)

writeDocument

public static void writeDocument(Document document,
                                 PrintStream output)

writeDocumentType

public static void writeDocumentType(DocumentType documentType,
                                     PrintStream output)

writeDocumentFragment

public static void writeDocumentFragment(DocumentFragment documentFragment,
                                         PrintStream output)

writeElement

public static void writeElement(Element element,
                                PrintStream output)

writeAttribute

public static void writeAttribute(Attr attribute,
                                  PrintStream output)

writeText

public static void writeText(Text text,
                             PrintStream output)

writeCDATA

public static void writeCDATA(CDATASection cdata,
                              PrintStream output)

writeEntity

public static void writeEntity(Entity entity,
                               PrintStream output)

writeEntityReference

public static void writeEntityReference(EntityReference entityReference,
                                        PrintStream output)

writeProcessingInstruction

public static void writeProcessingInstruction(ProcessingInstruction instr,
                                              PrintStream output)

writeComment

public static void writeComment(Comment comment,
                                PrintStream output)

writeNotation

public static void writeNotation(Notation notation,
                                 PrintStream output)

writeChildren

public static void writeChildren(Node node,
                                 PrintStream output)


Copyright © 2010 Aduna. All Rights Reserved.