ezvcard.util
Class XmlUtils

java.lang.Object
  extended by ezvcard.util.XmlUtils

public class XmlUtils
extends Object

Generic XML utility methods.

Author:
Michael Angstadt

Method Summary
static Document createDocument()
          Creates a new XML document.
static Element getFirstChildElement(Element parent)
          Gets the first child element of an element.
static Element getRootElement(Document parent)
          Gets the root element of a document.
static Document toDocument(InputStream in)
          Parses an XML document from an input stream.
static Document toDocument(Reader reader)
           Parses an XML document from a reader.
static Document toDocument(String xml)
          Parses an XML string into a DOM.
static List<Element> toElementList(NodeList nodeList)
          Gets all the elements out of a NodeList.
static String toString(Node node)
          Converts an XML node to a string.
static String toString(Node node, Map<String,String> outputProperties)
          Converts an XML node to a string.
static void toWriter(Node node, Writer writer)
          Writes an XML node to a writer.
static void toWriter(Node node, Writer writer, Map<String,String> outputProperties)
          Writes an XML node to a writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createDocument

public static Document createDocument()
Creates a new XML document.

Returns:
the XML document

toDocument

public static Document toDocument(String xml)
                           throws SAXException
Parses an XML string into a DOM.

Parameters:
xml - the XML string
Returns:
the parsed DOM
Throws:
SAXException - if the string is not valid XML

toDocument

public static Document toDocument(InputStream in)
                           throws SAXException,
                                  IOException
Parses an XML document from an input stream.

Parameters:
in - the input stream
Returns:
the parsed DOM
Throws:
SAXException - if the XML is not valid
IOException - if there is a problem reading from the input stream

toDocument

public static Document toDocument(Reader reader)
                           throws SAXException,
                                  IOException

Parses an XML document from a reader.

Note that use of this method is discouraged. It ignores the character encoding that is defined within the XML document itself, and should only be used if the encoding is undefined or if the encoding needs to be ignored for whatever reason. The toDocument(InputStream) method should be used instead, since it takes the XML document's character encoding into account when parsing.

Parameters:
reader - the reader
Returns:
the parsed DOM
Throws:
SAXException - if the XML is not valid
IOException - if there is a problem reading from the reader
See Also:
http://stackoverflow.com/q/3482494/13379

toString

public static String toString(Node node)
Converts an XML node to a string.

Parameters:
node - the XML node
Returns:
the string

toString

public static String toString(Node node,
                              Map<String,String> outputProperties)
Converts an XML node to a string.

Parameters:
node - the XML node
outputProperties - the output properties
Returns:
the string

toWriter

public static void toWriter(Node node,
                            Writer writer)
                     throws TransformerException
Writes an XML node to a writer.

Parameters:
node - the XML node
writer - the writer
Throws:
TransformerException - if there's a problem writing to the writer

toWriter

public static void toWriter(Node node,
                            Writer writer,
                            Map<String,String> outputProperties)
                     throws TransformerException
Writes an XML node to a writer.

Parameters:
node - the XML node
writer - the writer
outputProperties - the output properties
Throws:
TransformerException - if there's a problem writing to the writer

toElementList

public static List<Element> toElementList(NodeList nodeList)
Gets all the elements out of a NodeList.

Parameters:
nodeList - the node list
Returns:
the elements

getRootElement

public static Element getRootElement(Document parent)
Gets the root element of a document.

Parameters:
parent - the document
Returns:
the root element

getFirstChildElement

public static Element getFirstChildElement(Element parent)
Gets the first child element of an element.

Parameters:
parent - the parent element
Returns:
the first child element or null if there are no child elements


Copyright © 2012-2014 Michael Angstadt. All Rights Reserved.