|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.muse.util.xml.XmlUtils
public class 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 Document |
EMPTY_DOC
An empty Document that can be used as a DOM factory for creation of random XML fragments. |
static String |
TARGET_NS
The targetNamespace attribute name. |
static String |
XML_HEADER
The standard XML document header - does not include encoding. |
static String |
XMLNS_PREFIX
Standard prefix for XML namespace attributes. |
Constructor Summary | |
---|---|
XmlUtils()
|
Method Summary | |
---|---|
static Node[] |
convertToArray(NodeList list)
|
static Node |
convertToNode(Document document,
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 Document |
createDocument()
|
static Document |
createDocument(File file)
|
static Document |
createDocument(InputSource source)
|
static Document |
createDocument(InputStream stream)
|
static Document |
createDocument(String xml)
|
static Document |
createDocument(URI uri)
This is a convenience method that converts the given URI to a File and invokes createDocument(File). |
static Element |
createElement(Document doc,
QName qname)
|
static Element |
createElement(Document doc,
QName qname,
Node value,
boolean embedChildren)
|
static Element |
createElement(Document doc,
QName qname,
Object value)
|
static Element |
createElement(QName qname)
This is a convenience method that creates a new, empty Element; the owner of the Element is EMPTY_DOC. |
static Element |
createElement(QName qname,
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(Element e1,
Element e2)
|
static String |
extractText(Element element)
|
static Element |
findFirstInSubTree(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 Element[] |
findInSubTree(Element context,
QName qname)
Searches through an entire sub-tree for child Elements whose QName matches the one given. |
static Element[] |
getAllElements(Node context)
|
static Element[] |
getAllElements(Node context,
String namespace)
|
static Element[] |
getAllElements(Node context,
String namespace,
String localName)
|
static Map |
getAllNamespaces(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 String |
getAttribute(Element xml,
QName qname)
|
static Boolean |
getBoolean(Element xml)
|
static Date |
getDate(Element xml)
|
static Element |
getDocumentRoot(Node xml)
|
static Double |
getDouble(Element xml)
|
static Element |
getElement(Node context,
QName qname)
This is a convenience method that returns the zeroth child Element under the given Node. |
static Element |
getElement(Node context,
QName qname,
int index)
|
static QName |
getElementQName(Element xml)
|
static Element[] |
getElements(Node context,
QName qname)
|
static String[] |
getElementsText(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 String |
getElementText(Node context,
QName qname)
Searches a sub-tree for an Element with the given QName, and then returns its text content, if any. |
static Element |
getFirstElement(Node context)
|
static Float |
getFloat(Element xml)
|
static int |
getHashCode(Element xml)
|
static Integer |
getInteger(Element xml)
|
static Long |
getLong(Element xml)
|
static QName |
getQName(Element xml)
Retrieves the text from the element specified and parses it into a fully-resolved QName. |
static QName |
getQNameFromChild(Element xml,
QName childQName)
Retrieves the text from the child element specified and parses it into a fully-resolved QName. |
static Short |
getShort(Element xml)
|
static boolean |
haveMatchingAttributes(Element e1,
Element e2)
|
static boolean |
haveMatchingChildren(Element e1,
Element e2)
Recursively compares the children in the Element sub-trees to see if they are of equal name, value, and order. |
static Node |
moveSubTree(Node from,
Node to)
Copies all child nodes from the first Element tree into the second Element tree. |
static Node |
moveSubTree(Node from,
Node to,
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(String qname,
Element namespaceContext)
Parses the given String into a QName object and resolves the prefix to a namespace URI. |
static QName |
parseSchemaName(String name,
Element namespaceContext)
|
static String |
resolveNamespace(String qname,
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(Element context,
QName qname,
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(Element context,
QName qname,
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(Element element,
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(Element element,
String prefix,
String namespaceURI)
Adds a new XML namespace declaration attribute to the given Element. |
static void |
toFile(Node xml,
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(Node xml,
File file,
boolean printHeader)
Serializes the given XML sub-tree to the given file. |
static String |
toString(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 String |
toString(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 String |
toString(Node xml,
boolean printHeader,
boolean printIndents)
Serializes the given XML tree to string form, including the standard XML header and indentation if desired. |
static 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 Document EMPTY_DOC
public static final String TARGET_NS
public static final String XML_HEADER
public static final String XMLNS_PREFIX
Constructor Detail |
---|
public XmlUtils()
Method Detail |
---|
public static Node[] convertToArray(NodeList list)
list
- The list of Nodes to copy into a Node array.
public static Node convertToNode(Document document, 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 Document createDocument()
public static Document createDocument(File file) throws IOException, SAXException
file
- The XML file to parse.
IOException
- SAXException
- public static Document createDocument(InputSource source) throws IOException, SAXException
source
- A SAX InputSource that points to a valid XML document.
IOException
- SAXException
- public static Document createDocument(InputStream stream) throws IOException, SAXException
stream
- A stream containing a valid XML document.
IOException
- SAXException
- public static Document createDocument(String xml) throws IOException, SAXException
xml
- A string containing a valid XML document.
IOException
- SAXException
- public static Document createDocument(URI uri) throws IOException, SAXException
uri
- The URI of the XML file to parse.
IOException
SAXException
createDocument(File)
public static Element createElement(Document doc, QName qname)
doc
- The Document that is the owner for the new Element.qname
- The QName of the new Element.
public static Element createElement(Document doc, QName qname, 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 Element createElement(Document doc, QName qname, 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 Element createElement(QName qname)
createElement(QName, Object)
public static Element createElement(QName qname, Object value)
EMPTY_DOC
,
createElement(Document, QName, Object)
public static boolean equals(Element e1, Element e2)
e1
- e2
-
public static String extractText(Element element)
element
- The Element that may or may not have Text nodes.
public static Element findFirstInSubTree(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 Element[] findInSubTree(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 Element[] getAllElements(Node context)
context
- The Node whose direct child Elements will be returned.
public static Element[] getAllElements(Node context, 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 Element[] getAllElements(Node context, String namespace, 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 Map getAllNamespaces(Element xml)
xml
- The root of the sub-tree to perform the search on.
public static String getAttribute(Element xml, QName qname)
public static Boolean getBoolean(Element xml)
public static Date getDate(Element xml) throws ParseException
ParseException
- XsdUtils.getLocalTime(String)
public static Element getDocumentRoot(Node xml)
xml
-
public static Double getDouble(Element xml)
public static Element getElement(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 Element getElement(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(Element xml)
xml
- The Element whose QName will be returned.
public static Element[] getElements(Node context, QName qname)
context
- The root node to perform the search on.qname
- The QName to search for.
public static String[] getElementsText(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 String getElementText(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 Element getFirstElement(Node context)
context
- The root Node to perform the search against.
public static Float getFloat(Element xml)
public static int getHashCode(Element xml)
xml
-
public static Integer getInteger(Element xml)
public static Long getLong(Element xml)
public static QName getQName(Element xml)
xml
-
public static QName getQNameFromChild(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 Short getShort(Element xml)
public static boolean haveMatchingAttributes(Element e1, Element e2)
e1
- e2
-
public static boolean haveMatchingChildren(Element e1, Element e2)
e1
- e2
-
haveMatchingAttributes(Element, Element)
public static Node moveSubTree(Node from, Node to)
from
- The sub-tree to copy Nodes from.to
- The sub-tree to copy Nodes to.
public static Node moveSubTree(Node from, Node to, 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(String qname, 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(String name, 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 String resolveNamespace(String qname, Node xml)
qname
- The qualified name whose prefix is searched for.xml
- The Node from which namespace resolution will start.
public static void setElement(Element context, QName qname, Node node, boolean embedChildren)
createElement(Document, QName, Node, boolean)
public static void setElement(Element context, QName qname, Object value)
createElement(Document, QName, Object)
public static void setElementText(Element element, String text)
element
- The Element whose text content will be modified.text
- The new text value.public static void setNamespaceAttribute(Element element, String prefix, String namespaceURI)
element
- prefix
- namespaceURI
- public static void toFile(Node xml, File file) throws IOException
IOException
toFile(Node, File, boolean)
public static void toFile(Node xml, File file, boolean printHeader) throws 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.
IOException
- toString(Node, boolean)
public static String toString(Node xml)
toString(Node, boolean)
public static String toString(Node xml, boolean printHeader)
toString(Node, boolean, boolean)
public static String toString(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 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 |