public class XmlUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_NS_PREFIX
The prefix name that is used for the default namespace (i.e.
|
static XmlUtil |
XU
An instance of this class which can be easily included
as static import and then used like
XU.parseXmlString(string) . |
Modifier | Constructor and Description |
---|---|
protected |
XmlUtil()
Initialize the new instance with default values.
|
Modifier and Type | Method and Description |
---|---|
void |
addNamespacesFrom(NamespacePrefixMapper namespaces,
org.w3c.dom.Element element)
Adds all namespaces that are declared in the given element with their
prefix to the provided namespace prefix mapper.
|
void |
addNamespacesFromElementAndChildren(NamespacePrefixMapper namespaces,
org.w3c.dom.Element element)
Adds all namespaces that are declared in the given element and all child elements
together with their prefixes to the provides namespace prefix mapper.
|
java.lang.String |
asString(org.w3c.dom.Element element) |
protected void |
close(java.io.Closeable closeable) |
static XmlUtil |
current()
Returns the only instance this class supports (design pattern "Singleton")
|
javax.xml.namespace.NamespaceContext |
extractNamespacesFrom(org.w3c.dom.Document document)
Extracts all namespace definitions from the given document and returns
them in a NamespaceContext implementation.
|
javax.xml.namespace.NamespaceContext |
extractNamespacesFrom(org.w3c.dom.Element rootElement)
Extracts all namespace definitions from the given root element and returns
them in a NamespaceContext implementation.
|
protected java.io.File |
findFile(java.lang.String filename)
Tries to find the file with the given name.
|
java.lang.String |
getAttributeLN(org.w3c.dom.Element element,
java.lang.String localName)
Returns the value of the attribute with the given local name
in the specified element.
|
java.util.List<org.w3c.dom.Element> |
getChildElements(org.w3c.dom.Element element)
Returns a list of all direct children of the given element.
|
org.w3c.dom.Element |
getFirstChildElement(org.w3c.dom.Element element)
Returns the first Element under the given element.
|
org.w3c.dom.Element |
getFirstElementByTagName(org.w3c.dom.Document document,
java.lang.String tagName)
Returns the first element in the given document that matches the specified
tagName.
|
org.w3c.dom.Element |
getFirstElementByTagName(org.w3c.dom.Element element,
java.lang.String tagName)
Returns the first element under the given element that matches the specified
tagName.
|
java.lang.String |
getNamespaceURIOf(org.w3c.dom.Element element)
Returns the namespace URI of the given element or null if none can be found.
|
java.util.List<org.w3c.dom.Node> |
nodeListToListOfNodes(org.w3c.dom.NodeList nodeList)
Converts a nodeList to a list of Node objects.
|
protected org.w3c.dom.Document |
parse(org.xml.sax.InputSource inputSource) |
protected org.w3c.dom.Document |
parse(org.xml.sax.InputSource inputSource,
javax.xml.validation.Schema schema) |
org.w3c.dom.Document |
parseXmlString(java.lang.String xml) |
javax.xml.validation.Schema |
readSchema(java.lang.String schemaFileName)
Returns the Schema object for the specified XML schema file.
|
org.w3c.dom.Document |
readXmlDocument(java.lang.String xmlFileName,
javax.xml.validation.Schema schema)
Returns the Document object for the specified XML file.
|
org.w3c.dom.Document |
readXmlDocument(java.lang.String xmlFileName,
java.lang.String schemaFileName)
Returns the Document object for the specified XML file.
|
org.w3c.dom.Document |
readXmlFile(java.lang.String filename)
Returns the document object read from the specified XML file.
|
public static final XmlUtil XU
XU.parseXmlString(string)
.public static final java.lang.String DEFAULT_NS_PREFIX
public static XmlUtil current()
public org.w3c.dom.Document readXmlFile(java.lang.String filename) throws java.lang.Exception
java.lang.Exception
public org.w3c.dom.Document readXmlDocument(java.lang.String xmlFileName, java.lang.String schemaFileName) throws java.lang.Exception
xmlFileName
- The name of the XML file to read (must not be null)schemaFileName
- The name of the XML schema (XSD) file (may be null)java.lang.Exception
public org.w3c.dom.Document readXmlDocument(java.lang.String xmlFileName, javax.xml.validation.Schema schema) throws java.lang.Exception
xmlFileName
- The name of the XML file to read (must not be null)schema
- The schema to validate the XML data against (may be null)java.lang.Exception
public javax.xml.validation.Schema readSchema(java.lang.String schemaFileName) throws java.io.FileNotFoundException, org.xml.sax.SAXException
schemaFileName
- The name of the XML schema file to read (may be null)java.io.FileNotFoundException
org.xml.sax.SAXException
public void addNamespacesFrom(NamespacePrefixMapper namespaces, org.w3c.dom.Element element)
namespaces
- An implementation of a NamespaceContext that can collect prefix to namespace mappings (must not be null)element
- The element from which to extract namespace declarations.public void addNamespacesFromElementAndChildren(NamespacePrefixMapper namespaces, org.w3c.dom.Element element)
namespaces
- An implementation of a NamespaceContext that can collect prefix to namespace mappings (must not be null)element
- The element from which to start recursivly extracting namespace declarations.public javax.xml.namespace.NamespaceContext extractNamespacesFrom(org.w3c.dom.Element rootElement)
public javax.xml.namespace.NamespaceContext extractNamespacesFrom(org.w3c.dom.Document document)
public java.lang.String getNamespaceURIOf(org.w3c.dom.Element element)
element
- The element of interest.public org.w3c.dom.Element getFirstElementByTagName(org.w3c.dom.Document document, java.lang.String tagName)
document
- The document in which to find the element (must not be null)tagName
- The name of the element to find (must not be null)public org.w3c.dom.Element getFirstElementByTagName(org.w3c.dom.Element element, java.lang.String tagName)
element
- The element under which to find the element (must not be null)tagName
- The name of the element to find (must not be null)public java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Element element)
public org.w3c.dom.Element getFirstChildElement(org.w3c.dom.Element element)
public java.lang.String getAttributeLN(org.w3c.dom.Element element, java.lang.String localName)
element
- The element from which to retrieve the attributelocalName
- The local name (i.e. without any prefix) of the attributepublic java.lang.String asString(org.w3c.dom.Element element) throws javax.xml.transform.TransformerException
javax.xml.transform.TransformerException
public org.w3c.dom.Document parseXmlString(java.lang.String xml) throws java.lang.Exception
java.lang.Exception
public java.util.List<org.w3c.dom.Node> nodeListToListOfNodes(org.w3c.dom.NodeList nodeList)
protected org.w3c.dom.Document parse(org.xml.sax.InputSource inputSource) throws org.xml.sax.SAXException, java.io.IOException, javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException
protected org.w3c.dom.Document parse(org.xml.sax.InputSource inputSource, javax.xml.validation.Schema schema) throws org.xml.sax.SAXException, java.io.IOException, javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException
protected java.io.File findFile(java.lang.String filename) throws java.io.FileNotFoundException
filename
- The name of the file to findjava.io.FileNotFoundException
- If the file could not be found.protected void close(java.io.Closeable closeable)