org.openbp.common.markup
Class XMLUtil

java.lang.Object
  extended by org.openbp.common.markup.XMLUtil

public final class XMLUtil
extends java.lang.Object

Provides methods for general use.


Method Summary
static boolean compareStringNode(org.w3c.dom.Node node, java.lang.String tag)
          Checks if a node resembles the given XML tag (including attributes).
static org.w3c.dom.Element getChildElement(org.w3c.dom.Node parent, java.lang.String childName)
          Gets a named child node.
static org.w3c.dom.Element[] getChildElements(org.w3c.dom.Node parent, java.lang.String childName)
          Gets a named child elements in flat level.
static org.w3c.dom.Element[] getChildElements(org.w3c.dom.Node parent, java.lang.String[] childNames)
          Gets a named child elements in flat level.
static org.w3c.dom.Node getChildNode(org.w3c.dom.Node parent, java.lang.String childName)
          Deprecated. getChildElement(org.w3c.dom.Node, java.lang.String)
static org.w3c.dom.Node getChildNode(org.w3c.dom.Node parent, java.lang.String childName, short nodeType)
          Gets a named child node by the node type.
static java.lang.String getChildNodeValue(org.w3c.dom.Node parent, java.lang.String childName)
          Gets the text value of a named child node.
static java.lang.String getChildNodeValue(org.w3c.dom.Node parent, java.lang.String childName, short nodeType)
          Gets the text value of a named child node.
static java.lang.String getNodeValue(org.w3c.dom.Node node)
          Gets the text value of a node.
static org.w3c.dom.Document parseDocument(org.xml.sax.InputSource is)
          Parses an input source and creates a document from it.
static org.w3c.dom.Document parseDocument(java.lang.String fileName)
          Parses an input file and creates a document from it.
static org.w3c.dom.Document parseDocument(java.lang.String fileName, java.util.Map substitute)
          Parses an input file and creates a document from it.
static java.lang.String serialize(org.w3c.dom.Document document)
          Serializes the XML node tree to a string.
static void serialize(org.w3c.dom.Document document, java.lang.String fileName)
          Serializes the XML document to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

parseDocument

public static org.w3c.dom.Document parseDocument(org.xml.sax.InputSource is)
                                          throws java.io.IOException,
                                                 org.xml.sax.SAXException
Parses an input source and creates a document from it.

Parameters:
is - XML input source
Returns:
The document read
Throws:
java.io.IOException - On error
org.xml.sax.SAXException

parseDocument

public static org.w3c.dom.Document parseDocument(java.lang.String fileName)
                                          throws java.io.IOException,
                                                 org.xml.sax.SAXException
Parses an input file and creates a document from it.

Parameters:
fileName - Name of file to read
Returns:
XML node
Throws:
java.io.IOException - On error
org.xml.sax.SAXException - On error

parseDocument

public static org.w3c.dom.Document parseDocument(java.lang.String fileName,
                                                 java.util.Map substitute)
                                          throws java.io.IOException,
                                                 org.xml.sax.SAXException
Parses an input file and creates a document from it. Before parsing, all characters that are not 7 bit ASCII characters will be automatically converted to their HTML representation, i\. e\. &"#207;".
Optionally, substitutes $(key) parameters in the input file by their values.

Parameters:
fileName - Name of file to read
substitute - Map mapping parameter keys to values or null
Returns:
XML node
Throws:
java.io.IOException - On error
org.xml.sax.SAXException - On error

serialize

public static void serialize(org.w3c.dom.Document document,
                             java.lang.String fileName)
                      throws java.io.IOException
Serializes the XML document to a file.

Parameters:
document - Document or null
fileName - Name of the file to serialize into
Throws:
java.io.IOException - Error on converting or writing file

serialize

public static java.lang.String serialize(org.w3c.dom.Document document)
                                  throws java.io.IOException
Serializes the XML node tree to a string.

Parameters:
document - XML node or null
Returns:
XML string or null on error or if document is null
Throws:
java.io.IOException - Error on converting

getChildNode

public static org.w3c.dom.Node getChildNode(org.w3c.dom.Node parent,
                                            java.lang.String childName)
Deprecated. getChildElement(org.w3c.dom.Node, java.lang.String)

Gets a named child node. In contrast to the Element.getElementsByTagName method, this method is fine if there is only a single element by that name.
Note: Only child \belements\b will be considered.

Parameters:
parent - Parent node
childName - Name of the child node or null for the first child
Returns:
The child node or null if no such node exists

getChildElement

public static org.w3c.dom.Element getChildElement(org.w3c.dom.Node parent,
                                                  java.lang.String childName)
Gets a named child node. In contrast to the Element.getElementsByTagName method, this method is fine if there is only a single element by that name.
Note: Only child \belements\b will be considered.

Parameters:
parent - Parent node
childName - Name of the child node or null for the first child
Returns:
The child node or null if no such node exists

getChildNode

public static org.w3c.dom.Node getChildNode(org.w3c.dom.Node parent,
                                            java.lang.String childName,
                                            short nodeType)
Gets a named child node by the node type. In contrast to the Element.getElementsByTagName method, this method is fine if there is only a single element by that name.\n Note: Only child node of the given node type will be considered.

Parameters:
parent - Parent node
childName - Name of the child node or null for the first child
nodeType - see types of interface Node
Returns:
The child node or null if no such node exists

getChildElements

public static org.w3c.dom.Element[] getChildElements(org.w3c.dom.Node parent,
                                                     java.lang.String childName)
Gets a named child elements in flat level. In contrast to the Element.getElementsByTagName method, this method is fine if you want a list of elements by the name but only that elements which of their owner nodes are the parent node.

Parameters:
parent - Parent node
childName - Name of the child node or null for the first child
Returns:
The child element array

getChildElements

public static org.w3c.dom.Element[] getChildElements(org.w3c.dom.Node parent,
                                                     java.lang.String[] childNames)
Gets a named child elements in flat level. In contrast to the Element.getElementsByTagName method, this method is fine if you want a list of elements by the name but only that elements which of their owner nodes are the parent node.

Parameters:
parent - Parent node
childNames - Names of the child node or null for the first child
Returns:
The child element array

getChildNodeValue

public static java.lang.String getChildNodeValue(org.w3c.dom.Node parent,
                                                 java.lang.String childName)
Gets the text value of a named child node. First the child node is retrieved by it's name, then the getNodeValue(org.w3c.dom.Node) method is applied to it.
Note: Only child \belements\b will be considered.

Parameters:
parent - Parent node
childName - Name of the child node or null for the first child
Returns:
The text value or null if the node has no text value, has no children or it's child has not text value or if the text value is empty.
Any spaces are trimmed from the value.

getChildNodeValue

public static java.lang.String getChildNodeValue(org.w3c.dom.Node parent,
                                                 java.lang.String childName,
                                                 short nodeType)
Gets the text value of a named child node. First the child node is retrieved by it's name, then the getNodeValue(org.w3c.dom.Node) method is applied to it.
Note: Only child \belements\b will be considered.

Parameters:
parent - Parent node
childName - Name of the child node or null for the first child
nodeType - see types of interface Node
Returns:
The text value or null if the node has no text value, has no children or it's child has not text value or if the text value is empty.
Any spaces are trimmed from the value.

getNodeValue

public static java.lang.String getNodeValue(org.w3c.dom.Node node)
Gets the text value of a node. First, the node is checked if has a text value itself. If not, the method tries to retrieve a value from the child node of the node.

Parameters:
node - Node to check
Returns:
The text value or null if the node has no text value, has no children or it's child has not text value or if the text value is empty.
Any spaces are trimmed from the value.

compareStringNode

public static boolean compareStringNode(org.w3c.dom.Node node,
                                        java.lang.String tag)
Checks if a node resembles the given XML tag (including attributes).

Parameters:
node - Node to compare
tag - XML tag to compare, e. g. ''
Returns:
true: Node is equal tag
false: Node is not equal tag


Copyright © 2011. All Rights Reserved.