|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--uk.org.ogsadai.common.xml.XMLUtilities
A collection of helper methods for parsing to and from XML.
Field Summary | |
private static java.lang.String |
COPYRIGHT_NOTICE
Copyright statement |
Constructor Summary | |
XMLUtilities()
|
Method Summary | |
static org.w3c.dom.Document |
createEmptyDocument()
Create a new empty org.w3c.dom.Document . |
static java.lang.String |
elementToString(org.w3c.dom.Element element)
Converts an Element to its String
representation. |
static java.lang.String |
getAttributeValue(org.w3c.dom.Document doc,
java.lang.String eltName,
java.lang.String attributeName)
Extracts the value of a named attribute of the first element with a given name in a given doc. |
static org.w3c.dom.Element |
getChildElement(org.w3c.dom.Element parent,
java.lang.String namespaceURI,
java.lang.String childName)
Gets the named child element of the specified parent. |
static java.lang.String |
getChildElementAttribute(org.w3c.dom.Element parent,
java.lang.String namespaceURI,
java.lang.String childName,
java.lang.String attributeName)
Gets the named attribute of the named child element of the specified parent. |
static java.util.Iterator |
getChildElementIterator(org.w3c.dom.Node parent)
Returns an iterator over the child elements of a DOM node. |
static java.util.List |
getChildElements(org.w3c.dom.Element parent)
Gets a list of all the child elements of the specified parent. |
static java.lang.String |
getChildText(org.w3c.dom.Element parent,
java.lang.String namespaceURI,
java.lang.String childName)
Gets the concatenated text children, trimmed of surrounding whitespace, of the named child element of the specified parent. |
static java.lang.String |
getChildText(org.w3c.dom.Node parent)
Extract the XML sub-tree beneath the given node and return as an XML string. |
static org.w3c.dom.Element |
getElement(org.w3c.dom.Document doc,
java.lang.String eltName)
Extracts the first element with a given name from a given document. |
static java.lang.String |
getElementValue(org.w3c.dom.Document doc,
java.lang.String eltName)
Extracts the value of the first element with a given name from a given document. |
static java.lang.String |
getTextContent(org.w3c.dom.Node node)
Gets the text content below of the specified node. |
static java.lang.String |
parseAttribute(org.w3c.dom.Element element,
java.lang.String attributeName,
boolean nonEmpty)
Parses the value of an attribute of an element. |
static java.lang.String |
parseChildElementAttribute(org.w3c.dom.Element parent,
java.lang.String namespaceURI,
java.lang.String childName,
java.lang.String attributeName,
boolean required,
boolean nonEmpty)
Parses the value of an attribute of a child element. |
static java.lang.String |
parseChildElementValue(org.w3c.dom.Element parent,
java.lang.String namespaceURI,
java.lang.String childName,
boolean required,
boolean nonEmpty)
Parses the value of a child element of the specified parent. |
static java.lang.String |
parseElementValue(org.w3c.dom.Element element,
boolean nonEmpty)
Parses the specified element and returns its value. |
static long |
parseLong(org.w3c.dom.Element parent,
java.lang.String childName,
java.lang.String attributeName)
Parses the values of a named attribute of an element and converts the value to a long . |
static java.lang.String |
replaceSpecialCharacters(java.lang.String input)
Forms a new string by replacing any XML reserved characters in the given input string with their encoded equivalents. |
static void |
xmlDOMToFile(org.w3c.dom.Document doc,
java.lang.String fileName)
Puts a DOM Document into a file. |
static void |
xmlDOMToStream(org.w3c.dom.Node node,
java.io.OutputStream stream)
Serializes a DOM object to a java.io.OutputStream . |
private static void |
xmlDOMToStream(org.w3c.dom.Node node,
java.io.OutputStream stream,
java.io.Writer writer)
Serializes a DOM object to a java.io.OutputStream
or a java.io.Writer . |
static void |
xmlDOMToStream(org.w3c.dom.Node node,
java.io.Writer writer)
Serializes a DOM object to a java.io.Writer . |
static java.lang.String |
xmlDOMToString(org.w3c.dom.Node node)
Serializes a DOM object to a String . |
static org.w3c.dom.Document |
xmlFileToDOM(java.lang.String fileName)
Parses an XML file into a DOM Document and
validates it against its XML Schema. |
static org.w3c.dom.Document |
xmlFileToDOM(java.lang.String fileName,
boolean validate)
Parses an XML file into a DOM Document . |
static org.w3c.dom.Document |
xmlReaderToDOM(java.io.Reader reader,
boolean validate)
Converts XML from a java.io.Reader into a DOM
Document |
static org.w3c.dom.Document |
xmlStreamToDOM(java.io.InputStream stream)
Converts XML from a java.io.InputStream into a DOM
Document and validates it against its XML Schema. |
static org.w3c.dom.Document |
xmlStreamToDOM(java.io.InputStream stream,
boolean validate)
Converts XML from a java.io.InputStream into a DOM
Document . |
static org.w3c.dom.Document |
xmlStringToDOM(java.lang.String str)
Converts XML from a String into a DOM
Document and validates it against its XML Schema. |
static org.w3c.dom.Document |
xmlStringToDOM(java.lang.String str,
boolean validate)
Converts XML from a String into a DOM
Document . |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final java.lang.String COPYRIGHT_NOTICE
Constructor Detail |
public XMLUtilities()
Method Detail |
public static org.w3c.dom.Document xmlFileToDOM(java.lang.String fileName) throws XMLFileException, XMLDocumentException, XMLParseException
Document
and
validates it against its XML Schema.
fileName
-
XMLFileException
XMLDocumentException
XMLParseException
xmlFileToDOM(java.lang.String, boolean)
public static org.w3c.dom.Document xmlFileToDOM(java.lang.String fileName, boolean validate) throws XMLFileException, XMLDocumentException, XMLParseException
Document
.
fileName
- Source file.validate
- A boolean flag to indicate if schema validation is required.
Document
.
XMLFileException
- if there is a problem accessing the file.
XMLDocumentException
- if the document does not validate against the XML Schema.
XMLParseException
- if there is a problem with the parser setup or execution.
java.lang.IllegalArgumentException
- if file
is null
.xmlStreamToDOM(java.io.InputStream, boolean)
public static org.w3c.dom.Document xmlStringToDOM(java.lang.String str) throws XMLDocumentException, XMLParseException
String
into a DOM
Document
and validates it against its XML Schema.
str
- XML string
XMLDocumentException
XMLParseException
xmlStreamToDOM(java.io.InputStream, boolean)
public static org.w3c.dom.Document xmlStringToDOM(java.lang.String str, boolean validate) throws XMLDocumentException, XMLParseException
String
into a DOM
Document
.
str
- String
providing the XML.validate
- A boolean flag to indicate if schema validation is required.
Document
.
XMLDocumentException
- if the document does not validate
against the XML Schema.
XMLParseException
- if there is a problem with
the parser setup or execution.
java.lang.IllegalArgumentException
- if string
is
null
.xmlStreamToDOM(java.io.InputStream, boolean)
public static org.w3c.dom.Document xmlStreamToDOM(java.io.InputStream stream) throws XMLDocumentException, XMLParseException
java.io.InputStream
into a DOM
Document
and validates it against its XML Schema.
stream
- XML input stream
XMLDocumentException
XMLParseException
xmlStreamToDOM(java.io.InputStream, boolean)
public static org.w3c.dom.Document xmlStreamToDOM(java.io.InputStream stream, boolean validate) throws XMLDocumentException, XMLParseException
java.io.InputStream
into a DOM
Document
.
stream
- InputStream
providing the XML.validate
- A boolean flag to indicate if schema validation is required.
Document
.
XMLDocumentException
- if the document does not validate
against the XML Schema.
XMLParseException
- if there is a problem with
the parser setup or execution.
ExternalModuleException
- if there is an internal problem
within Xerces when creating the Document
.
java.lang.IllegalArgumentException
- if stream
is
null
.public static org.w3c.dom.Document xmlReaderToDOM(java.io.Reader reader, boolean validate) throws XMLDocumentException, XMLParseException
java.io.Reader
into a DOM
Document
reader
- Reader
providing the XML.validate
- A boolean flag to indicate if schema validation is required.
Document
.
XMLDocumentException
- if the document does not validate
against the XML Schema.
XMLParseException
- if there is a problem with
the parser setup or execution.
ExternalModuleException
- if there is an internal problem
within Xerces when creating the Document
.
java.lang.IllegalArgumentException
- if reader
is
null
.public static void xmlDOMToFile(org.w3c.dom.Document doc, java.lang.String fileName) throws XMLFileException, XMLParseException
Document
into a file. If the file does
not exist it will be created otherwise it will be overwritten.
doc
- The document to writefileName
- Destination file.
XMLFileException
- if there is a problem accessing the
file.
XMLParseException
- if there is a problem with
the parser setup or execution.
java.lang.IllegalArgumentException
- if fileName
or
doc
are null
.public static java.lang.String xmlDOMToString(org.w3c.dom.Node node)
String
.
node
- DOM object to serialize.
java.lang.IllegalArgumentException
- if node
is
null
.public static void xmlDOMToStream(org.w3c.dom.Node node, java.io.OutputStream stream) throws XMLParseException
java.io.OutputStream
.
node
- DOM object to serialize.stream
- OutputStream
to serialize to.
XMLParseException
- if there is a problem with
the parser setup or execution.
java.lang.IllegalArgumentException
- if either argument is
null
.public static void xmlDOMToStream(org.w3c.dom.Node node, java.io.Writer writer) throws XMLParseException
java.io.Writer
.
node
- DOM object to serialize.writer
- Writer
to serialize to - will only be used if
stream
is null
.
XMLParseException
- if there is a problem with
the parser setup or execution.
java.lang.IllegalArgumentException
- if either argument is
null
.private static void xmlDOMToStream(org.w3c.dom.Node node, java.io.OutputStream stream, java.io.Writer writer) throws XMLParseException
java.io.OutputStream
or a java.io.Writer
.
node
- DOM object to serialize.stream
- OutputStream
to serialize to.writer
- Writer
to serialize to - will only be used if
stream
is null
.
XMLParseException
- if there is a problem with
the parser setup or execution.public static java.lang.String getTextContent(org.w3c.dom.Node node)
This is achieved by appending the character data contained in any text or CDATA child nodes. Surrounding whitespace is trimmed and duplicate whitespace removed.
node
- Node
String
containing a concatenation of the
text children of the specified node, or an empty string if
there are no child nodes.
java.lang.IllegalArgumentException
- if node
is
null
.public static java.util.Iterator getChildElementIterator(org.w3c.dom.Node parent)
parent
- Parent node.
java.util.Iterator
containing
Element
objects.
java.lang.IllegalArgumentException
- if parent
is
null
.public static org.w3c.dom.Document createEmptyDocument() throws ExternalModuleException
org.w3c.dom.Document
.
Document
.
ExternalModuleException
- if there is an internal problem
within Xerces when creating the Document
.public static java.lang.String replaceSpecialCharacters(java.lang.String input)
input
- String to process.
public static org.w3c.dom.Element getElement(org.w3c.dom.Document doc, java.lang.String eltName)
doc
- Document.eltName
- Element name.
null
if there are no elements with that name.
java.lang.IllegalArgumentException
- if any argument is
null
.public static java.lang.String getElementValue(org.w3c.dom.Document doc, java.lang.String eltName)
doc
- Document.eltName
- Element name.
null
if there are no elements with that name.
java.lang.IllegalArgumentException
- if any argument is
null
.public static java.lang.String getAttributeValue(org.w3c.dom.Document doc, java.lang.String eltName, java.lang.String attributeName)
doc
- Document.eltName
- Element name.attributeName
- Name of attribute whose value is to be returned.
null
if there is no such
element or attribute.
java.lang.IllegalArgumentException
- if any argument is
null
.public static long parseLong(org.w3c.dom.Element parent, java.lang.String childName, java.lang.String attributeName)
long
.
If there is more than one child with the same name, then the value of the attribute from the first child is used. If the attribute value is not a number then -1 is returned.
parent
- An element.childName
- Name of a child element.attributeName
- Name of attribute of child element.
long
attribute value or
-1
if the value is not set or is not a number.
java.lang.IllegalArgumentException
- if any argument is
null
.public static java.lang.String parseElementValue(org.w3c.dom.Element element, boolean nonEmpty) throws XMLEmptyElementException, XMLParseException
nonEmpty
flag can be used to control the parsing
validation.
element
- The Element
containing the value.nonEmpty
- Set to true
if element
must contain
a value, otherwise false
.
String
value. If nonEmpty
is set to false
then this may be null
.
XMLEmptyElementException
- If the element should not be empty but is.
XMLParseException
- If there is a system problem parsing the value.getChildText(org.w3c.dom.Node)
public static java.lang.String parseChildElementValue(org.w3c.dom.Element parent, java.lang.String namespaceURI, java.lang.String childName, boolean required, boolean nonEmpty) throws XMLEmptyElementException, XMLMissingElementException, XMLParseException
required
and nonEmpty
flags should be set to control the validation during the parsing.
parent
- The parent Element
that may contain the child.namespaceURI
- The URI for the namespace of the child element.childName
- The name of the child element.required
- Set to true
if the child element is required,
false
otherwise.nonEmpty
- Set to true
if the child element is cannot be
empty false
otherwise.
XMLEmptyElementException
- If the element should not be empty but is.
XMLMissingElementException
- If the element is missing and it shouldn't be.
XMLParseException
- If there is a system problem parsing the value.getChildElement(Element, String, String)
,
parseElementValue(Element, boolean)
public static java.lang.String parseAttribute(org.w3c.dom.Element element, java.lang.String attributeName, boolean nonEmpty) throws XMLMissingAttributeException, XMLParseException
element
- The Element
containing the attribute.attributeName
- The name of the attribute.nonEmpty
- Set to true
if the attribute must have a value,
false
otherwise.
null
if there is no value
XMLMissingAttributeException
- If the attribute is missing and it shouldn't be.
XMLParseException
- If there is a system problem parsing the value.public static java.lang.String parseChildElementAttribute(org.w3c.dom.Element parent, java.lang.String namespaceURI, java.lang.String childName, java.lang.String attributeName, boolean required, boolean nonEmpty) throws XMLEmptyElementException, XMLMissingElementException, XMLMissingAttributeException, XMLParseException
parent
- The Element
that may contain the child.namespaceURI
- The URI for the namespace of the child element.childName
- The name of the child element.attributeName
- The name of the attribute.required
- Set to true
if the child element is required,
false
otherwise.nonEmpty
- Set to true
if the attribute cannot be empty
false
otherwise.
null
if the attribute is
not present (and not required).
XMLEmptyElementException
- If the element should not be empty but is.
XMLMissingElementException
- If the element is missing and it shouldn't be.
XMLMissingAttributeException
- If the attribute is missing and it shouldn't be.
XMLParseException
- If there is a system problem parsing the value.public static java.util.List getChildElements(org.w3c.dom.Element parent)
parent
- The Element
containing the children.
List
of Element
objects.public static org.w3c.dom.Element getChildElement(org.w3c.dom.Element parent, java.lang.String namespaceURI, java.lang.String childName)
parent
- The parent Element
.namespaceURI
- The URI for the namespace of the child element.childName
- The name of the child element.
Element
or null
if the
child does not exist.public static java.lang.String getChildElementAttribute(org.w3c.dom.Element parent, java.lang.String namespaceURI, java.lang.String childName, java.lang.String attributeName)
parent
- The parent Element
.namespaceURI
- The URI for the namespace of the child element.childName
- The name of the child element.attributeName
- The name of the attribute of the child.
null
if the child does not
exist.public static java.lang.String getChildText(org.w3c.dom.Element parent, java.lang.String namespaceURI, java.lang.String childName) throws XMLParseException
parent
- The parent Element
.namespaceURI
- The URI for the namespace of the child element.childName
- The name of the child element.
String
containing the concatenated text
children, trimmed of surrounding white space, or null
if the parent does not contain the specified child.
XMLParseException
- If there is a system problem parsing the value.public static java.lang.String getChildText(org.w3c.dom.Node parent) throws XMLParseException
parent
- Parent node
XMLParseException
- If an internal problem occurs.public static java.lang.String elementToString(org.w3c.dom.Element element) throws XMLParseException
Element
to its String
representation.
This is similar to xmlDOMToString
but omits
an XML header.
element
- Element to convert.
XMLParseException
- If an internal problem occurs.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |