|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.fu_berlin.ties.xml.dom.DOMUtils
A static class that provides utility constants and methods for working with DOM-like XML representations, focussing especially on dom4j. No instances of this class can be created, only the static members should be used.
Method Summary | |
static Attribute |
attributeByName(Element element,
String name)
Returns the attribute with the given name, compatible to the name format returned by name(Attribute) . |
static void |
collectText(Branch branch,
StringBuffer appender)
Recursively collects the complete textual content of a branch, i.e. |
static void |
collectText(Branch branch,
Writer writer)
Recursively collects the complete textual content of a branch, i.e. |
static List |
elementsByName(Element element,
String name)
Returns the child elements with the given name, compatible to the name format returned by name(Element) . |
static String |
name(Attribute attrib)
Static method that returns a String representing the name of an attribute in an XML document. |
static String |
name(Element element)
Static method that returns a String representing the name of an element in an XML document. |
static Document |
readDocument(File file,
Configuration config)
Reads an XML document from a local file, using a configured charset. |
static Document |
readDocument(File file,
String charset)
Reads an XML document from a local file, using a given charset. |
static Document |
readDocument(InputStream in)
Reads an XML document from a given stream. |
static Document |
readDocument(Reader reader)
Reads an XML document from a given reader. |
static String |
showElement(Element element)
Builds a simple partial representation of an element, containing the name of the element and its trimmed textual content. |
static String |
showToken(Element element,
String token)
Builds a simple partial representation of a textual token in an element, containing the name of the element and token. |
static void |
writeDocument(Document document,
OutputStream out)
Writes an XML document to a given stream. |
static void |
writeDocument(Document document,
OutputStreamWriter writer)
Writes an XML document to a given writer, using the character set of the underlying output stream. |
static void |
writeDocument(Document document,
Writer writer,
String charset)
Writes an XML document to a given writer, using the given character set. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
public static Attribute attributeByName(Element element, String name)
name(Attribute)
. If there are more than one
attributes with the given name (e.g. in different namespaces) then the
first one is returned.
element
- the element whose attribute to returnname
- the name of the attribute, compatible to the name format
returned by name(Attribute)
null
if none
existspublic static void collectText(Branch branch, StringBuffer appender)
branch
- the branch to recurseappender
- the collected text of the branch and all its child
elements is appended to this string bufferpublic static void collectText(Branch branch, Writer writer) throws IOException
branch
- the branch to recursewriter
- the collected text of the branch and all its child
elements is appended to this writer; flushed but not closed by this
method
IOException
- if an I/O error occurs while writing to the writerpublic static List elementsByName(Element element, String name)
name(Element)
. If no elements are found
then this method returns an empty list.
element
- the element whose child elements to returnname
- the name of the child elements, compatible to the name format
returned by name(Attribute)
Element
s for the given namepublic static String name(Attribute attrib)
name(Element)
for details.
attrib
- the element to name
public static String name(Element element)
Node.getName()
or Element.getQualifiedName()
or similar methods directly in such cases.
Currently, only the local name if used, namespace URIs and namespace prefixes are ignored. Including namespace prefixes in context representations would be quite useless, because in different document different prefixes can represent the same namespace and vice versa.
Including namespace URIs might lead to higher precision by avoiding the risk of confusing elements from totally different namespaces. On other other hand it might lead to lower recall and slower learning because elements from similar namespaces (e.g. different versions of the HTML standard) are all considered separated from each other.
element
- the element to name
public static Document readDocument(File file, Configuration config) throws DocumentException, FileNotFoundException, UnsupportedEncodingException
IOUtils.openReader(File, Configuration)
to determine
the character set.
file
- the file to readconfig
- the configuration to use
DocumentException
- if an error occurs during parsing
FileNotFoundException
- if the file does not exist, is a
directory rather than a regular file, or for some other reason cannot
be opened for reading
UnsupportedEncodingException
- if the configured charset is not
supportedpublic static Document readDocument(File file, String charset) throws DocumentException, FileNotFoundException, UnsupportedEncodingException
file
- the file to readcharset
- the character set to use for reading the file;
if null
, the default charset of the current platform is used
DocumentException
- if an error occurs during parsing
FileNotFoundException
- if the file does not exist, is a
directory rather than a regular file, or for some other reason cannot
be opened for reading
UnsupportedEncodingException
- if the named charset is not
supportedpublic static Document readDocument(InputStream in) throws DocumentException
in
- stream containing the text to parse; not closed by this method
DocumentException
- if an error occurs during parsingpublic static Document readDocument(Reader reader) throws DocumentException
reader
- reader containing the text to parse; not closed by this
method
DocumentException
- if an error occurs during parsingpublic static String showElement(Element element)
element
- the element to show
public static String showToken(Element element, String token)
element
- the element to showtoken
- the token to show
public static void writeDocument(Document document, OutputStream out) throws IOException
document
- the document to writeout
- the stream to write the document text to; flushed
but not closed by this method
IOException
- if an I/O error occurs during writingpublic static void writeDocument(Document document, OutputStreamWriter writer) throws IOException
document
- the document to writewriter
- the writer to write the document text to; flushed
but not closed by this method
IOException
- if an I/O error occurs during writingpublic static void writeDocument(Document document, Writer writer, String charset) throws IOException
document
- the document to writewriter
- the writer to write the document text to; flushed
but not closed by this methodcharset
- the character set of the writer
IOException
- if an I/O error occurs during writing
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |