|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.bowstreet.util.XmlUtil
public class XmlUtil
The XmlUtil class consists exclusively of static methods that return XML objects. It provides methods for creating new XML objects from text representations. Once you have created an IXml, JDOM, or DOM object you can manipulate it with methods of the IXml, JDOM, or DOM interface respectively. In addition, it provides methods for creating alternative XML object representations that have been registered with the Factory. Methods are also provided to convert XML documents from one object representation to another. Below we use the term "document" to mean a string or a character stream of XML. When parsing XML documents into IXml, comments will not be preserved. If preserving comments is required, then an alternative representation such as DOM or JDOM should be used.
IXml
Field Summary | |
---|---|
static java.lang.String |
ALT_NAME_ATTRIBUTE
Used in addition to "name" attribute to trigger full parse. |
static java.lang.String |
HTMLCONTENT
|
static java.lang.String |
HTMLWRAPPER
|
static java.lang.String |
ID_ATTRIBUTE
Used in addition to "name" & "_name_" attributes to trigger full parse. |
Constructor Summary | |
---|---|
XmlUtil()
|
Method Summary | |
---|---|
static void |
copyAttributes(IXml schema,
IXml data)
Unsupported - INTERNAL USE ONLY |
static void |
copyElementContents(IXml data,
IXml schema)
Unsupported - INTERNAL USE ONLY |
static int |
countChildren(IXml parent)
Counts the number of immediate children in parent |
static int |
countChildren(IXml parent,
java.lang.String filter)
Counts the number of immediate children in parent with a specific name |
static IXml |
create(java.lang.String name)
Create a new, empty IXml element. |
static IXml |
create(java.lang.String localname,
java.lang.String prefix,
java.lang.String namespaceURI)
Create a new namespace-aware XML element with the specified localName and optional prefix and namespace URI. |
static org.w3c.dom.Document |
createDOM(IXml source)
Convert an IXml document into a DOM document and return it. |
static org.w3c.dom.Document |
createDOM(java.io.Reader source)
Parses an XML document from the specified Reader into a well formed DOM document (org.w3c.dom.Document). |
static org.w3c.dom.Document |
createDOM(java.lang.String source)
Parses an XML document from the specified String into a well formed DOM document (org.w3c.dom.Document). |
static java.lang.Object |
createObject(java.lang.Object source,
java.lang.String className)
Convert an XML document into another representation and return it. |
static IXml |
createRawTextElement(java.lang.String rawText)
Create a section of "raw" text for use in conjunction with writeHtml, to include text with no escaping or quoting of special characters. |
static IXml |
createXml(java.lang.Object source)
Convert an XML document into an IXml document and return it. |
static IXml |
findCommonAncestor(IXml node1,
IXml node2)
Finds the closest common ancestor to the two nodes. |
static java.util.List<IXml> |
findElements(IXml x,
java.lang.String xpath)
Takes an xpath and returns a List of the matching elements. |
static boolean |
hasMultipleRoots(java.lang.String xmlStr,
java.lang.String rootName)
This method detects if multiple roots are present in the given XML string. |
static boolean |
isContainedWithin(IXml inner,
IXml outer)
Tests to see if inner (1st arg) is contained anywhere within outer, including recursively |
static IXml |
lastChildOf(IXml parent)
Finds the last child of an IXml element |
static IXml |
parseHtml(java.io.Reader in)
Parses an HTML document from the specified Reader into a well formed IXml object. |
static IXml |
parseHtml(java.lang.String in)
Parses an HTML document from the specified String into a well formed IXml object. |
static IXml |
parseXml(java.io.Reader in)
Parses an XML document from the specified Reader into a well formed IXml object. |
static IXml |
parseXml(java.io.Reader in,
boolean validateNamespacePrefixes)
Parses an XML document from the specified Reader into a well formed IXml object. |
static java.lang.Object |
parseXml(java.io.Reader source,
java.lang.String className)
Parses an XML document from the specified Reader into a well formed object of the specified class name. |
static IXml |
parseXml(java.lang.String in)
Parses an XML document from the specified String into a well formed IXml object. |
static java.lang.Object |
parseXml(java.lang.String source,
java.lang.String className)
Parses an XML document from the specified String into a well formed object of the specified class name. |
static java.lang.String |
prettyPrint(IXml xml)
|
static void |
removeAttributes(IXml node)
Remove all attributes except those that define namespaces |
static IXml |
safeParseXml(java.lang.String in)
Parses an XML document from the specified String into a well formed IXml object. |
static IXml |
safeParseXmlFromFile(java.lang.String filename)
Parses an XML document from a file. |
static java.lang.String |
toString(java.lang.Object source)
Return the string representation of the specified XML document. |
static void |
writeXml(java.lang.Object source,
java.io.Writer out)
Write an XML document (IXml, DOM, JDOM) to the specified Writer. |
static void |
writeXml(java.lang.Object source,
java.io.Writer out,
boolean trimWhitespace)
Same as writeXml, but with option to remove all whitespace from character data and between xml tags, which can be useful when needing to compare several XML documents. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String ALT_NAME_ATTRIBUTE
public static final java.lang.String HTMLCONTENT
public static final java.lang.String HTMLWRAPPER
public static final java.lang.String ID_ATTRIBUTE
Constructor Detail |
---|
public XmlUtil()
Method Detail |
---|
public static void copyAttributes(IXml schema, IXml data)
public static void copyElementContents(IXml data, IXml schema)
public static int countChildren(IXml parent)
parent
- The Parent to count children of
public static int countChildren(IXml parent, java.lang.String filter)
parent
- The Parent to count children of
public static IXml create(java.lang.String name)
name
- The name of the IXml element to be created.
We can create an empty element with: XmlUtil.create("boat"); => An element with name "boat".
public static IXml create(java.lang.String localname, java.lang.String prefix, java.lang.String namespaceURI)
localname
- - name of the element (no prefix or : allowed)prefix
- - optional prefix qualifier, may be nullnamespaceURI
- of the namespace to associate this element with.
public static org.w3c.dom.Document createDOM(IXml source) throws XMLException
source
- The IXml document that is to be converted to DOM.
XMLException
- Any XML exception, possibly
wrapping another exception.XmlUtil.createDOM(document) => a DOM representation of the IXml document.
public static org.w3c.dom.Document createDOM(java.io.Reader source) throws XMLException
source
- The Reader containing the XML document that is to be parsed.
XMLException
- Any XML exception, possibly
wrapping another exception.XmlUtil.createDOM(new StringReader("<foo/>")) => an DOM element named "foo".
public static org.w3c.dom.Document createDOM(java.lang.String source) throws XMLException
source
- The String containing the XML document that is to be parsed.
XMLException
- Any XML exception, possibly
wrapping another exception.XmlUtil.createDOM("<boat color=\"red\"/>") => an DOM element named "boat" with one attribute named "color".
public static java.lang.Object createObject(java.lang.Object source, java.lang.String className) throws XMLException
source
- The Object containing the XML document that is to be converted/cloned.className
- The class name that implements the desired XML object type.
XMLException
- Any XML exception, possibly
wrapping another exception.XmlUtil.createObject(document,"org.jdom.Document") => a JDOM representation of the IXml document.
public static IXml createRawTextElement(java.lang.String rawText)
rawText
- The raw text to be included.
XmlUtil.createRawTextElement("<a href=\"http://<%= getURL() %>\"> ") => creates a special IXml element containing the raw text. This will be written as-is by writeHtml.
public static IXml createXml(java.lang.Object source) throws XMLException
source
- The XML document that is to be converted to IXml.
XMLException
- Any XML exception, possibly
wrapping another exception.XmlUtil.createXml(document) => an IXml representation of the XML document.
public static IXml findCommonAncestor(IXml node1, IXml node2)
node1
- node2
-
public static java.util.List<IXml> findElements(IXml x, java.lang.String xpath)
x
- the XML to be examinedxpath
- the xpath specifying the elements to be returned
public static boolean hasMultipleRoots(java.lang.String xmlStr, java.lang.String rootName)
xmlStr
- string to to test for multiple XML root elementsrootName
- name of the XML root element
true
if multiple XML root elements are found or
false
otherwise.public static boolean isContainedWithin(IXml inner, IXml outer)
inner
- IXml which might be an inner childouter
- IXml which might be the parent or great-parent
public static IXml lastChildOf(IXml parent)
parent
-
public static IXml parseHtml(java.io.Reader in) throws java.io.IOException, com.bowstreet.util.parser.ParserException
in
- The Reader to the HTML data that is to be parse.
java.io.IOException
com.bowstreet.util.parser.ParserException
StringReader sr = new StringReader("<html><body> hello world </body></html>"); XmlUtil.parseHtml(sr).getName(); => "HTML"
public static IXml parseHtml(java.lang.String in) throws java.io.IOException, com.bowstreet.util.parser.ParserException
in
- The string representation of the HTML data that is to be parsed.
java.io.IOException
com.bowstreet.util.parser.ParserException
XmlUtil.parseHtml("<html><body> hello world </body></html>");
public static IXml parseXml(java.io.Reader in) throws java.io.IOException
in
- The Reader for the XML data that is to be parsed.
java.io.IOException
XmlUtil.parseXml(new StringReader("<snowmobile/>")); => an element with name "snowmobile".
public static IXml parseXml(java.io.Reader in, boolean validateNamespacePrefixes) throws java.io.IOException
in
- The Reader for the XML data that is to be parsed.validateNamespacePrefixes
- Use false to disable namespace prefix checking.
java.io.IOException
public static java.lang.Object parseXml(java.io.Reader source, java.lang.String className) throws XMLException
source
- The Reader containing the XML document that is to be parsed.className
- The class name that implements the desired XML object type.
XMLException
- Any XML exception, possibly
wrapping another exception.XmlUtil.parseXml(new StringReader("<foo/>"), "org.jdom.Document") => an JDOM document containing an element named "foo".
public static IXml parseXml(java.lang.String in) throws java.io.IOException
in
- The String containing the XML document that is to be parsed.
java.io.IOException
XmlUtil.parseXml("<boat color=\"red\"/>") => an element named "boat" with one attribute named "color".
public static java.lang.Object parseXml(java.lang.String source, java.lang.String className) throws XMLException
source
- The String containing the XML document that is to be parsed.className
- The class name that implements the desired XML object type.
XMLException
- Any XML exception, possibly
wrapping another exception.XmlUtil.parseXml("<boat color=\"red\"/>", "org.jdom.Document") => an JDOM element named "boat" with one attribute named "color".
public static java.lang.String prettyPrint(IXml xml)
public static void removeAttributes(IXml node)
node
- node from which to remove all attributes.public static IXml safeParseXml(java.lang.String in)
in
- The String containing the XML document that is to be parsed.
XmlUtil.parseXml("<boat color=\"red\"/>") => an element named "boat" with one attribute named "color".
public static IXml safeParseXmlFromFile(java.lang.String filename)
filename
- public static java.lang.String toString(java.lang.Object source) throws XMLException
source
- The XML document that is to be returned as a string.
XMLException
- Any XML exception, possibly
wrapping another exception.XmlUtil.toString(document) => returns the string representation of the XML document.
public static void writeXml(java.lang.Object source, java.io.Writer out) throws XMLException
Writer writer = new StringWriter(); XmlUtil.writeXml( document, writer, true); return writer.toString();
source
- The XML document that is to be written.out
- The Writer that the XML document is to be written to.
XMLException
- Any XML exception, possibly
wrapping another exception.public static void writeXml(java.lang.Object source, java.io.Writer out, boolean trimWhitespace) throws XMLException
source
- The XML document that is to be written.out
- The Writer that the XML document is to be written to.trimWhitespace
- indicates if white space is to be trimmed or not
XMLException
- Any XML exception, possibly
wrapping another exception.
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |