|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.muse.util.xml.XmlUtils
XmlUtils is a collection of utility methods related to XML parsing and manipulation. It is mostly based on JAXP, but some of the methods require additional features that are only found in Apache Xerces.
Field Summary | |
---|---|
static org.w3c.dom.Document |
EMPTY_DOC
An empty Document that can be used as a DOM factory for creation of random XML fragments. |
static java.lang.String |
TARGET_NS
The targetNamespace attribute name. |
static java.lang.String |
XML_HEADER
The standard XML document header - does not include encoding. |
static java.lang.String |
XMLNS_PREFIX
Standard prefix for XML namespace attributes. |
Constructor Summary | |
---|---|
XmlUtils()
|
Method Summary | |
---|---|
static org.w3c.dom.Node[] |
convertToArray(org.w3c.dom.NodeList list)
|
static org.w3c.dom.Node |
convertToNode(org.w3c.dom.Document document,
java.lang.Object value)
Converts an object into its XML representation; this usually means that the object's toString() method is called and the result used to create a DOM Text object. |
static org.w3c.dom.Document |
createDocument()
|
static org.w3c.dom.Document |
createDocument(java.io.File file)
|
static org.w3c.dom.Document |
createDocument(org.xml.sax.InputSource source)
|
static org.w3c.dom.Document |
createDocument(java.io.InputStream stream)
|
static org.w3c.dom.Document |
createDocument(java.lang.String xml)
|
static org.w3c.dom.Document |
createDocument(java.net.URI uri)
This is a convenience method that converts the given URI to a File and invokes createDocument(File). |
static org.w3c.dom.Element |
createElement(org.w3c.dom.Document doc,
QName qname)
|
static org.w3c.dom.Element |
createElement(org.w3c.dom.Document doc,
QName qname,
org.w3c.dom.Node value,
boolean embedChildren)
|
static org.w3c.dom.Element |
createElement(org.w3c.dom.Document doc,
QName qname,
java.lang.Object value)
|
static org.w3c.dom.Element |
createElement(QName qname)
This is a convenience method that creates a new, empty Element; the owner of the Element is EMPTY_DOC. |
static org.w3c.dom.Element |
createElement(QName qname,
java.lang.Object value)
This is a convenience method that creates a new Element with the given value; the owner of the Element is EMPTY_DOC. |
static boolean |
equals(org.w3c.dom.Element e1,
org.w3c.dom.Element e2)
|
static java.lang.String |
extractText(org.w3c.dom.Element element)
|
static org.w3c.dom.Element |
findFirstInSubTree(org.w3c.dom.Element context,
QName qname)
This is a convenience method that returns the first Element in the array returned by findInSubTree(Element, QName), if any. |
static org.w3c.dom.Element[] |
findInSubTree(org.w3c.dom.Element context,
QName qname)
Searches through an entire sub-tree for child Elements whose QName matches the one given. |
static org.w3c.dom.Element[] |
getAllElements(org.w3c.dom.Node context)
|
static org.w3c.dom.Element[] |
getAllElements(org.w3c.dom.Node context,
java.lang.String namespace)
|
static org.w3c.dom.Element[] |
getAllElements(org.w3c.dom.Node context,
java.lang.String namespace,
java.lang.String localName)
|
static java.util.Map |
getAllNamespaces(org.w3c.dom.Element xml)
Searches the given sub-tree and returns all of the namespace URIs that are used to declare the root Element and its child Elements. |
static java.lang.String |
getAttribute(org.w3c.dom.Element xml,
QName qname)
|
static java.lang.Boolean |
getBoolean(org.w3c.dom.Element xml)
|
static java.util.Date |
getDate(org.w3c.dom.Element xml)
|
static org.w3c.dom.Element |
getDocumentRoot(org.w3c.dom.Node xml)
|
static java.lang.Double |
getDouble(org.w3c.dom.Element xml)
|
static org.w3c.dom.Element |
getElement(org.w3c.dom.Node context,
QName qname)
This is a convenience method that returns the zeroth child Element under the given Node. |
static org.w3c.dom.Element |
getElement(org.w3c.dom.Node context,
QName qname,
int index)
|
static QName |
getElementQName(org.w3c.dom.Element xml)
|
static org.w3c.dom.Element[] |
getElements(org.w3c.dom.Node context,
QName qname)
|
static java.lang.String[] |
getElementsText(org.w3c.dom.Node context,
QName qname)
Searches a sub-tree for all direct child Elements with the given Qname and returns the text content of those Elements, if any. |
static java.lang.String |
getElementText(org.w3c.dom.Node context,
QName qname)
Searches a sub-tree for an Element with the given QName, and then returns its text content, if any. |
static org.w3c.dom.Element |
getFirstElement(org.w3c.dom.Node context)
|
static java.lang.Float |
getFloat(org.w3c.dom.Element xml)
|
static int |
getHashCode(org.w3c.dom.Element xml)
|
static java.lang.Integer |
getInteger(org.w3c.dom.Element xml)
|
static java.lang.Long |
getLong(org.w3c.dom.Element xml)
|
static QName |
getQName(org.w3c.dom.Element xml)
Retrieves the text from the element specified and parses it into a fully-resolved QName. |
static QName |
getQNameFromChild(org.w3c.dom.Element xml,
QName childQName)
Retrieves the text from the child element specified and parses it into a fully-resolved QName. |
static java.lang.Short |
getShort(org.w3c.dom.Element xml)
|
static boolean |
haveMatchingAttributes(org.w3c.dom.Element e1,
org.w3c.dom.Element e2)
|
static boolean |
haveMatchingChildren(org.w3c.dom.Element e1,
org.w3c.dom.Element e2)
Recursively compares the children in the Element sub-trees to see if they are of equal name, value, and order. |
static org.w3c.dom.Node |
moveSubTree(org.w3c.dom.Node from,
org.w3c.dom.Node to)
Copies all child nodes from the first Element tree into the second Element tree. |
static org.w3c.dom.Node |
moveSubTree(org.w3c.dom.Node from,
org.w3c.dom.Node to,
org.w3c.dom.Node context)
Copies all child nodes from the first Element tree into the second Element tree inserted before the given context node. |
static QName |
parseQName(java.lang.String qname,
org.w3c.dom.Element namespaceContext)
Parses the given String into a QName object and resolves the prefix to a namespace URI. |
static QName |
parseSchemaName(java.lang.String name,
org.w3c.dom.Element namespaceContext)
|
static java.lang.String |
resolveNamespace(java.lang.String qname,
org.w3c.dom.Node xml)
Parses the prefix from the given qualified name and finds the first XML namespace declaration that maps that prefix to a namespace URI. |
static void |
setElement(org.w3c.dom.Element context,
QName qname,
org.w3c.dom.Node node,
boolean embedChildren)
If the given context node has no child Elements with the given name, one is created; otherwise, the first direct child with the name is used. |
static void |
setElement(org.w3c.dom.Element context,
QName qname,
java.lang.Object value)
If the given context node has no child Elements with the given name, one is created; otherwise, the first direct child with the name is used. |
static void |
setElementText(org.w3c.dom.Element element,
java.lang.String text)
If the given Element has no text content, a new Text node added with the given text; otherwise, the first Text node will have its value changed to the given text. |
static void |
setNamespaceAttribute(org.w3c.dom.Element element,
java.lang.String prefix,
java.lang.String namespaceURI)
Adds a new XML namespace declaration attribute to the given Element. |
static void |
toFile(org.w3c.dom.Node xml,
java.io.File file)
This is a convenience method that calls toFile(Node, File, boolean) with the last parameter set to "true" (to add the standard XML header to the new XML file). |
static void |
toFile(org.w3c.dom.Node xml,
java.io.File file,
boolean printHeader)
Serializes the given XML sub-tree to the given file. |
static java.lang.String |
toString(org.w3c.dom.Node xml)
This is a convenience method that serializes the given XML tree with the XML header and indentation; it is the equivalent of calling toString(Node, boolean) with the last parameter set to "true". |
static java.lang.String |
toString(org.w3c.dom.Node xml,
boolean printHeader)
This is a convenience method that serializes the given XML tree with indentation; the XML header is included if the second parameter is "true". |
static java.lang.String |
toString(org.w3c.dom.Node xml,
boolean printHeader,
boolean printIndents)
Serializes the given XML tree to string form, including the standard XML header and indentation if desired. |
static java.lang.String |
toString(QName qname)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final org.w3c.dom.Document EMPTY_DOC
public static final java.lang.String TARGET_NS
public static final java.lang.String XML_HEADER
public static final java.lang.String XMLNS_PREFIX
Constructor Detail |
public XmlUtils()
Method Detail |
public static org.w3c.dom.Node[] convertToArray(org.w3c.dom.NodeList list)
list
- The list of Nodes to copy into a Node array.
public static org.w3c.dom.Node convertToNode(org.w3c.dom.Document document, java.lang.Object value)
document
- The Document that will be used to create the new Node (or
import the existing one, if the value is already a Node).value
- The value that will be converted into a DOM Node.
public static org.w3c.dom.Document createDocument()
public static org.w3c.dom.Document createDocument(java.io.File file) throws java.io.IOException, org.xml.sax.SAXException
file
- The XML file to parse.
java.io.IOException
- org.xml.sax.SAXException
- public static org.w3c.dom.Document createDocument(org.xml.sax.InputSource source) throws java.io.IOException, org.xml.sax.SAXException
source
- A SAX InputSource that points to a valid XML document.
java.io.IOException
- org.xml.sax.SAXException
- public static org.w3c.dom.Document createDocument(java.io.InputStream stream) throws java.io.IOException, org.xml.sax.SAXException
stream
- A stream containing a valid XML document.
java.io.IOException
- org.xml.sax.SAXException
- public static org.w3c.dom.Document createDocument(java.lang.String xml) throws java.io.IOException, org.xml.sax.SAXException
xml
- A string containing a valid XML document.
java.io.IOException
- org.xml.sax.SAXException
- public static org.w3c.dom.Document createDocument(java.net.URI uri) throws java.io.IOException, org.xml.sax.SAXException
uri
- The URI of the XML file to parse.
java.io.IOException
org.xml.sax.SAXException
createDocument(File)
public static org.w3c.dom.Element createElement(org.w3c.dom.Document doc, QName qname)
doc
- The Document that is the owner for the new Element.qname
- The QName of the new Element.
public static org.w3c.dom.Element createElement(org.w3c.dom.Document doc, QName qname, org.w3c.dom.Node value, boolean embedChildren)
doc
- The Document that is the owner for the new Element.qname
- The Qname of the new Element.value
- The value of the new Element - this is either a child element
or the actual element. The fourth parameter is used to
determine how the value is set.embedChildren
- True if the third parameter's children should be extracted
and appended to the new Element; false if the third parameter
should simply be appended as a child of the new Element.
<Type2>
<Type3/>
</Type2>
<Type1>
<Type3/>
</Type1>
<Type1>
<Type2>
<Type3/>
<Type2>
</Type1>
public static org.w3c.dom.Element createElement(org.w3c.dom.Document doc, QName qname, java.lang.Object value)
doc
- The Document that is the owner of the new Element.qname
- The QName of the new Element.value
- The value of the new Element. If this is a DOM Node, it's
children will be embedded into the new Element and the root
node discarded. If it is not a DOM Node, it will be converted
into one using convertToNode(Document, Object) and be appended
to the new Element as a Text node.
convertToNode(Document, Object)
,
createElement(Document, QName, Node, boolean)
public static org.w3c.dom.Element createElement(QName qname)
createElement(QName, Object)
public static org.w3c.dom.Element createElement(QName qname, java.lang.Object value)
EMPTY_DOC
,
createElement(Document, QName, Object)
public static boolean equals(org.w3c.dom.Element e1, org.w3c.dom.Element e2)
e1
- e2
-
public static java.lang.String extractText(org.w3c.dom.Element element)
element
- The Element that may or may not have Text nodes.
public static org.w3c.dom.Element findFirstInSubTree(org.w3c.dom.Element context, QName qname)
context
- The root node from which the search will be done.qname
- The QName of the Element to search for.
findInSubTree(Element, QName)
public static org.w3c.dom.Element[] findInSubTree(org.w3c.dom.Element context, QName qname)
context
- The root node from which the search will be done.qname
- The QName of the Element to search for.
Element.getElementsByTagNameNS(String, String)
,
getAllElements(Node)
,
getElement(Node, QName)
,
getElements(Node, QName)
public static org.w3c.dom.Element[] getAllElements(org.w3c.dom.Node context)
context
- The Node whose direct child Elements will be returned.
public static org.w3c.dom.Element[] getAllElements(org.w3c.dom.Node context, java.lang.String namespace)
context
- The Node whose direct child Elements will be returned.namespace
- The namespace URI to match against all child Elements. All
returned results will have QNames with this namespace.
public static org.w3c.dom.Element[] getAllElements(org.w3c.dom.Node context, java.lang.String namespace, java.lang.String localName)
context
- The Node whose direct child Elements will be returned.namespace
- The namespace URI to match against all child Elements. All
returned results will have QNames with this namespace.localName
- The local (non-prefixed) name to match against all child
Elements. All returned results will have QNames with this name.
public static java.util.Map getAllNamespaces(org.w3c.dom.Element xml)
xml
- The root of the sub-tree to perform the search on.
public static java.lang.String getAttribute(org.w3c.dom.Element xml, QName qname)
public static java.lang.Boolean getBoolean(org.w3c.dom.Element xml)
public static java.util.Date getDate(org.w3c.dom.Element xml) throws java.text.ParseException
java.text.ParseException
- XsdUtils.getLocalTime(String)
public static org.w3c.dom.Element getDocumentRoot(org.w3c.dom.Node xml)
xml
-
public static java.lang.Double getDouble(org.w3c.dom.Element xml)
public static org.w3c.dom.Element getElement(org.w3c.dom.Node context, QName qname)
context
- The root Node to perform the search on.qname
- The QName to search for.
getElement(Node, QName, int)
public static org.w3c.dom.Element getElement(org.w3c.dom.Node context, QName qname, int index)
context
- The root Node to perform the search on.qname
- The QName to search for.index
- The occurrence of QName to return (zero-based). If index is 0,
the first match is returned, if it's 3, the fourth match is
returned, etc.
public static QName getElementQName(org.w3c.dom.Element xml)
xml
- The Element whose QName will be returned.
public static org.w3c.dom.Element[] getElements(org.w3c.dom.Node context, QName qname)
context
- The root node to perform the search on.qname
- The QName to search for.
public static java.lang.String[] getElementsText(org.w3c.dom.Node context, QName qname)
context
- The root node to perform the search on.qname
- The QName to search for.
extractText(Element)
,
getElementText(Node, QName)
public static java.lang.String getElementText(org.w3c.dom.Node context, QName qname)
<Type1>
<Type2>here is some text</Type2>
</Type1>
context
- The root Node to perform the search on.qname
- The QName to search for.
extractText(Element)
public static org.w3c.dom.Element getFirstElement(org.w3c.dom.Node context)
context
- The root Node to perform the search against.
public static java.lang.Float getFloat(org.w3c.dom.Element xml)
public static int getHashCode(org.w3c.dom.Element xml)
xml
-
public static java.lang.Integer getInteger(org.w3c.dom.Element xml)
public static java.lang.Long getLong(org.w3c.dom.Element xml)
public static QName getQName(org.w3c.dom.Element xml)
xml
-
public static QName getQNameFromChild(org.w3c.dom.Element xml, QName childQName)
xml
- The root Element to perform the search on.childQName
- The name of the child element whose text is a QName.
public static java.lang.Short getShort(org.w3c.dom.Element xml)
public static boolean haveMatchingAttributes(org.w3c.dom.Element e1, org.w3c.dom.Element e2)
e1
- e2
-
public static boolean haveMatchingChildren(org.w3c.dom.Element e1, org.w3c.dom.Element e2)
e1
- e2
-
haveMatchingAttributes(Element, Element)
public static org.w3c.dom.Node moveSubTree(org.w3c.dom.Node from, org.w3c.dom.Node to)
from
- The sub-tree to copy Nodes from.to
- The sub-tree to copy Nodes to.
public static org.w3c.dom.Node moveSubTree(org.w3c.dom.Node from, org.w3c.dom.Node to, org.w3c.dom.Node context)
from
- The sub-tree to copy Nodes from.to
- The sub-tree to copy Nodes to.context
- The Node before which the children will be inserted
public static QName parseQName(java.lang.String qname, org.w3c.dom.Element namespaceContext)
qname
- The qualified name, in string form.namespaceContext
- The Element from which to start namespace resolution. The
search will start with this Element and move up through its
parents until a match is found or the root is hit.
resolveNamespace(String, Node)
public static QName parseSchemaName(java.lang.String name, org.w3c.dom.Element namespaceContext)
name
- The unqualified name that must be matched to a schema namespace
to become qualified.namespaceContext
- The node from which the target namespace resolution will occur.
public static java.lang.String resolveNamespace(java.lang.String qname, org.w3c.dom.Node xml)
qname
- The qualified name whose prefix is searched for.xml
- The Node from which namespace resolution will start.
public static void setElement(org.w3c.dom.Element context, QName qname, org.w3c.dom.Node node, boolean embedChildren)
createElement(Document, QName, Node, boolean)
public static void setElement(org.w3c.dom.Element context, QName qname, java.lang.Object value)
createElement(Document, QName, Object)
public static void setElementText(org.w3c.dom.Element element, java.lang.String text)
element
- The Element whose text content will be modified.text
- The new text value.public static void setNamespaceAttribute(org.w3c.dom.Element element, java.lang.String prefix, java.lang.String namespaceURI)
element
- prefix
- namespaceURI
- public static void toFile(org.w3c.dom.Node xml, java.io.File file) throws java.io.IOException
java.io.IOException
toFile(Node, File, boolean)
public static void toFile(org.w3c.dom.Node xml, java.io.File file, boolean printHeader) throws java.io.IOException
xml
- The XML tree to serialize - must be a Document or Element.file
- The file to write to. If it already exists, its contents will
be overwritten; if it does not, it will be created.printHeader
- True if the standard XML document header should be added to
the top of the file.
java.io.IOException
- toString(Node, boolean)
public static java.lang.String toString(org.w3c.dom.Node xml)
toString(Node, boolean)
public static java.lang.String toString(org.w3c.dom.Node xml, boolean printHeader)
toString(Node, boolean, boolean)
public static java.lang.String toString(org.w3c.dom.Node xml, boolean printHeader, boolean printIndents)
xml
- The XML tree to serialize.printHeader
- True if you want the XML header printed before the XML.printIndents
- True if you want pretty-printing - child elements will be
indented with symmetry.
public static java.lang.String toString(QName qname)
qname
- The QName to serialize into prefix:localName form.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |