|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.bristle.javalib.xml.XMLUtil
public class XMLUtil
This class contains utility routines for manipulating XML.
Usage:
- Some typical scenarios for using this class are:
- To create an XML Document from scratch and convert it to a String:
Document dom = XMLUtil.createEmptyDocument();
Node xmlNode = XMLUtil.appendElement(dom, "Person");
XMLUtil.appendElementContainingText(xmlNode, "Name", "John Smith");
XMLUtil.appendElementContainingText(xmlNode, "Phone", "800-555-1212");
String strXML = XMLUtil.serialize(dom);
- To load and query an existing XML Document:
Document dom = XMLUtil.loadDocumentFromURL
("http://www.xmlsource.com");
String strName = XMLUtil.getTextNodeValueViaXPath
(xmlNode, ".//Name");
String strPhone = XMLUtil.getTextNodeValueViaXPath
(xmlNode, "Phone");
- To load and modify an existing XML Document:
Document dom = XMLUtil.loadDocumentFromURL
("http://www.xmlsource.com");
String strName = XMLUtil.setTextNodeValueViaXPath
(xmlNode, ".//Name", "John Smith");
String strPhone = XMLUtil.setTextNodeValueViaXPath
(xmlNode, "Phone", "800-555-1212");
- See the source code of the inner Tester class for more examples.
Assumptions:
Effects:
- None.
Anticipated Changes:
Notes:
- Pay particular attection to the distinction between the different
XML DOM node types. The names and comments in this class use these
different terms very precisely:
Document An entire XML DOM document
DocumentFragment
A fragment of an XML document.
Element An XML element or tag (an item in angle brackets)
Text A sequence of text stored in an Element
Node Could be any of the above, or an XML attribute, CDATA,
comment, document type, entity, etc.
See the org.w3c.dom.Node documentation for details.
- The term "append" when used in method names in this class typically
means "append a newly created child node to the specified node".
Implementation Notes:
Portability Issues:
Revision History:
$Log$
| Nested Class Summary | |
|---|---|
static class |
XMLUtil.NoSuchXMLNodeException
This exception is thrown when the specified XML node doesn't exist. |
static class |
XMLUtil.Tester
Each class contains a Tester inner class with a main() for easier unit testing. |
| Field Summary | |
|---|---|
static Node |
nodeINSERT_AFTER_LAST
Constant for use as parameter to insertBefore. |
| Constructor Summary | |
|---|---|
XMLUtil()
|
|
| Method Summary | |
|---|---|
static Element |
appendElement(Node xmlNode,
String strName)
Append a child Element to the specified Node. |
static Element |
appendElementContainingText(Node xmlNode,
String strName,
String strText)
Append a child Element containing a Text node to the specified Node. |
static Text |
appendTextNode(Node xmlNode,
String strText)
Append a child Text node to the specified Node. |
static Document |
createEmptyDocument()
Creates an empty XML Document. |
static String |
formatEndTag(String strTag)
Format the end version of the specified XML tag. |
static String |
formatStartTag(String strTag)
Format the start version of the specified XML tag. |
static String |
formatStartTagAndAttributes(String strTag,
String strAttributes)
Format the start version of the specified XML tag, and include a set of attributes. |
static Element |
getFirstChildElement(Node xml)
Returns the first child Element of a Node, or null if no child Element found. |
static int |
getIntTextNodeValueViaXPath(Node xmlNode,
String strXPath)
Get the int value of the text node that is embedded in the node that is located at the specified XPath from the specified node, defaulting to 0 if the text node has an empty value. |
static Element |
getOrAppendElement(Node xmlNode,
String strName)
Get the child Element with the specified name. |
static void |
getOrAppendElementAndSetTextNodeValue(Node xmlNode,
String strName,
String strValue)
Get the child Element with the specified name. |
static Document |
getOwnerDocument(Node xmlNode)
Get the owner document of the specified node. |
static Element |
getRootElement(Document dom)
Returns the root Element of a Document, or null if no root found. |
static Document |
getSystemProperties()
Get an XML Document containing all Java system properties. |
static String |
getTextNodeValueOrEmptyStringViaXPath(Node xmlNode,
String strXPath)
Get the value of the text node that is embedded in the node that is located at the specified XPath from the specified node. |
static String |
getTextNodeValueViaXPath(Node xmlNode,
String strXPath)
Get the value of the text node that is embedded in the node that is located at the specified XPath from the specified node. |
static Node |
insertBefore(Node xmlParent,
Node xmlNewChild,
Node xmlRefChild)
Inserts xmlNewChild as a child of xmlParent before the existing child xmlRefChild, or as the last child if xmlRefChild is null, copying the new Node and its entire subtree from a different Document if necessary. |
static Document |
loadDocumentFromInputSource(InputSource inputSource)
Load the specified URL into an XML Document. |
static Document |
loadDocumentFromString(String strXML)
Load the specified XML string into an XML Document. |
static Document |
loadDocumentFromURL(String strURL)
Load the specified URL into an XML Document. |
static void |
removeAllChildren(Node xmlNode)
Remove all child nodes from the specified Node. |
static String |
serialize(Document xml)
Serialize the XML Document into a string of XML. |
static String |
serialize(DocumentFragment xml)
Serialize the XML DocumentFragment into a string of XML. |
static OutputStream |
serialize(DocumentFragment xml,
OutputStream out)
Serialize the XML DocumentFragment into the specified OutputStream, silently tolerating a null DocumentFragment. |
static Writer |
serialize(DocumentFragment xml,
Writer writer)
Serialize the XML DocumentFragment into the specified Writer, silently tolerating a null DocumentFragment. |
static OutputStream |
serialize(Document xml,
OutputStream out)
Serialize the XML Document into the specified OutputStream, silently tolerating a null Document. |
static Writer |
serialize(Document xml,
Writer writer)
Serialize the XML Document into the specified Writer, silently tolerating a null Document. |
static String |
serialize(Element xml)
Serialize the XML Element into a string of XML. |
static OutputStream |
serialize(Element xml,
OutputStream out)
Serialize the XML Element into the specified OutputStream, silently tolerating a null Element. |
static Writer |
serialize(Element xml,
Writer writer)
Serialize the XML Element into the specified Writer, silently tolerating a null Element. |
static String |
serializeChildElements(Node xml)
Serialize the child Elements of the XML Node into a string of XML |
static OutputStream |
serializeChildElements(Node xml,
OutputStream out)
Serialize the child Elements of the XML Node into the specified OutputStream, silently tolerating a null Node. |
static Writer |
serializeChildElements(Node xml,
Writer writer)
Serialize the child Elements of the XML Node into the specified Writer, silently tolerating a null Node. |
static void |
setNodeValueViaXPath(Node xmlNode,
String strXPath,
String strText)
Set the value of the specified XML Node. |
static void |
setTextNodeValueViaXPath(Node xmlNode,
String strXPath,
String strText)
Set the value of the text node that is embedded in the node that is located at the specified XPath from the specified node. |
static Document |
wrapThrowableInXML(Throwable e,
Object objSource)
Wraps the info from a Java Throwable in an XML Document. |
static String |
wrapThrowableInXMLString(Throwable e,
Object objSource)
Wraps the info from a Java Throwable in an XML String. |
static String |
wrapThrowableSafelyInXMLString(Throwable e,
Object objSource)
Wraps the info from a Java Throwable in an XML String, without risk of throwing another Throwable. |
static Writer |
writeEndTag(Writer writer,
String strTag)
Write the end version of the specified XML tag to the specified Writer. |
static Writer |
writeStartTag(Writer writer,
String strTag)
Write the start version of the specified XML tag to the specified Writer. |
static Writer |
writeStartTagAndAttributes(Writer writer,
String strTag,
String strAttributes)
Write the start version of the specified XML tag to the specified Writer, including an attribute string. |
static Writer |
writeTagAndValue(Writer writer,
String strTag,
String strValue)
Write the specified value, enclosed in the start and end versions of the specified XML tag, to the specified Writer. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final Node nodeINSERT_AFTER_LAST
| Constructor Detail |
|---|
public XMLUtil()
| Method Detail |
|---|
public static Document createEmptyDocument()
public static Document loadDocumentFromURL(String strURL)
throws SAXException,
IOException
Anticipated Changes:
None.
strURL - URL containing XML to load.
SAXException - When URL contains invalid XML.
IOException - When an error occurs reading from the URL.
public static Document loadDocumentFromInputSource(InputSource inputSource)
throws SAXException,
IOException
Anticipated Changes:
None.
inputSource - InputSource containing XML to load.
SAXException - When InputSource contains invalid XML.
IOException - When an error occurs reading from the InputSource.
public static Document loadDocumentFromString(String strXML)
throws SAXException,
IOException
Anticipated Changes:
None.
strXML - String of XML to load.
SAXException - When string contains invalid XML.
IOException - When an error occurs reading from the string.public static Document getOwnerDocument(Node xmlNode)
xmlNode - Node to get the owner document of.
public static Text appendTextNode(Node xmlNode,
String strText)
xmlNode - Node to append to.strText - String to put in text node.
public static Element appendElement(Node xmlNode,
String strName)
xmlNode - Node to append to.strName - String to use as name of new Node.
public static Element appendElementContainingText(Node xmlNode,
String strName,
String strText)
xmlNode - Node to append to.strName - String to use as name of new Element.strText - String to put in Text node.
public static Document wrapThrowableInXML(Throwable e,
Object objSource)
e - Throwable to wrap as XML.objSource - Object throwing the Throwable.
public static Writer serialize(Document xml,
Writer writer)
throws IOException
xml - XML Documentwriter - The Writer to serialize to.
IOException - When the Document can't be serialized.
public static OutputStream serialize(Document xml,
OutputStream out)
throws IOException
xml - XML Documentout - The OutputStream to serialize to.
IOException - When the Document can't be serialized.
public static String serialize(Document xml)
throws IOException
xml - XML Document
IOException - When the Document can't be serialized.
public static Writer serialize(DocumentFragment xml,
Writer writer)
throws IOException
xml - XML DocumentFragmentwriter - The Writer to serialize to.
IOException - When the DocumentFragment can't be serialized.
public static OutputStream serialize(DocumentFragment xml,
OutputStream out)
throws IOException
xml - XML DocumentFragmentout - The OutputStream to serialize to.
IOException - When the DocumentFragment can't be serialized.
public static String serialize(DocumentFragment xml)
throws IOException
xml - XML DocumentFragment
IOException - When the DocumentFragment can't be serialized.
public static Writer serialize(Element xml,
Writer writer)
throws IOException
xml - XML Elementwriter - The Writer to serialize to.
IOException - When the Element can't be serialized.
public static OutputStream serialize(Element xml,
OutputStream out)
throws IOException
xml - XML Elementout - The OutputStream to serialize to.
IOException - When the Element can't be serialized.
public static String serialize(Element xml)
throws IOException
xml - XML Element
IOException - When the Element can't be serialized.
public static Writer serializeChildElements(Node xml,
Writer writer)
throws IOException
xml - XML Nodewriter - The Writer to serialize to.
IOException - When the child Elements of the Node can't be
serialized.
public static OutputStream serializeChildElements(Node xml,
OutputStream out)
throws IOException
xml - XML Nodeout - The OutputStream to serialize to.
IOException - When the child Elements of the Node can't be
serialized.
public static String serializeChildElements(Node xml)
throws IOException
xml - XML Node
IOException - When the child Elements of the Node can't be
serialized.
public static String wrapThrowableInXMLString(Throwable e,
Object objSource)
throws IOException
e - Throwable to wrap as XML.objSource - Object throwing the Throwable.
IOException - When the XML can't be serialized.
public static String wrapThrowableSafelyInXMLString(Throwable e,
Object objSource)
e - Throwable to wrap as XML.objSource - Object throwing the Throwable.
public static String getTextNodeValueViaXPath(Node xmlNode,
String strXPath)
throws TransformerException,
XMLUtil.NoSuchXMLNodeException
xmlNode - Node to search from.strXPath - XPath to get from xmlNode to the desired node.
TransformerException - When the specified XPath is invalid.
XMLUtil.NoSuchXMLNodeException - When the node specified by the XPath doesn't exist.
public static String getTextNodeValueOrEmptyStringViaXPath(Node xmlNode,
String strXPath)
throws TransformerException
xmlNode - Node to search from.strXPath - XPath to get from xmlNode to the desired node.
TransformerException - When the specified XPath is invalid.
public static int getIntTextNodeValueViaXPath(Node xmlNode,
String strXPath)
throws TransformerException,
XMLUtil.NoSuchXMLNodeException,
NumberFormatException
xmlNode - Node to search from.strXPath - XPath to get from xmlNode to the desired node.
TransformerException - When the specified XPath is invalid.
XMLUtil.NoSuchXMLNodeException - When the node specified by the XPath doesn't exist.
NumberFormatException - When the value of the text node is not "" or an integer.
public static void setTextNodeValueViaXPath(Node xmlNode,
String strXPath,
String strText)
throws TransformerException,
XMLUtil.NoSuchXMLNodeException
xmlNode - Node to search from.strXPath - XPath to get from xmlNode to the desired node.strText - String to put in text node.
TransformerException - When the specified XPath is invalid.
XMLUtil.NoSuchXMLNodeException - When the node specified by the XPath doesn't exist.
public static void setNodeValueViaXPath(Node xmlNode,
String strXPath,
String strText)
throws TransformerException,
XMLUtil.NoSuchXMLNodeException
xmlNode - Node to search from.strXPath - XPath to get from xmlNode to the desired Node.strText - New value of the node.
TransformerException - When the specified XPath is invalid.
XMLUtil.NoSuchXMLNodeException - When the node specified by the XPath doesn't exist.
public static Element getOrAppendElement(Node xmlNode,
String strName)
throws TransformerException
xmlNode - Node to append child to.strName - Name of the Element to get or create.
TransformerException - When an error occurs searching the XML via XPath,
probably because of an invalid value for strName.
public static void getOrAppendElementAndSetTextNodeValue(Node xmlNode,
String strName,
String strValue)
throws TransformerException
xmlNode - Node to append child to.strName - Name of the Element to get or create.strValue - String to put in text node.
TransformerException - When an error occurs searching the XML via XPath,
probably because of an invalid value for strName.public static void removeAllChildren(Node xmlNode)
xmlNode - Node to remove from.public static Element getFirstChildElement(Node xml)
xml - Node to find first child Element of
public static Element getRootElement(Document dom)
dom - Document to find root Element of
public static Node insertBefore(Node xmlParent,
Node xmlNewChild,
Node xmlRefChild)
Note: This is identical to org.w3c.dom.Node.insertBefore() except that
1. It calls org.w3c.dom.Document.importNode() to copy the Node
and its subtree from another Document, if necessary to avoid
throwing an exception.
2. If xmlNewChild is an entire Document, it inserts the single
Element child of the Document (the root Element) instead of
the Document itself, to avoid throwing an exception.
xmlParent - Node to insert the child intoxmlNewChild - Node to insert as childxmlRefChild - Existing child Node to insert before, or null.
public static String formatStartTag(String strTag)
strTag - XML tag (without angle brackets).
public static String formatStartTagAndAttributes(String strTag,
String strAttributes)
strTag - XML tag (without angle brackets).strAttributes - Attributes to be included in the start tag.
public static String formatEndTag(String strTag)
strTag - XML tag (without angle brackets or slash).
public static Writer writeStartTag(Writer writer,
String strTag)
throws IOException
writer - Writer to write start tag to.strTag - XML tag (without angle brackets).
IOException - When an error occurs writing to the Writer.
public static Writer writeStartTagAndAttributes(Writer writer,
String strTag,
String strAttributes)
throws IOException
writer - Writer to write start tag to.strTag - XML tag (without angle brackets).strAttributes - Attributes to be included in the start tag.
IOException - When an error occurs writing to the Writer.
public static Writer writeEndTag(Writer writer,
String strTag)
throws IOException
writer - Writer to write end tag to.strTag - XML tag (without angle brackets or slash).
IOException - When an error occurs writing to the Writer.
public static Writer writeTagAndValue(Writer writer,
String strTag,
String strValue)
throws IOException
writer - Writer to write to.strTag - XML tag (without angle brackets).strValue - String value to enclose in XML tags.
IOException - When an error occurs writing to the Writer.public static Document getSystemProperties()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||