com.opensymphony.util
Class XMLUtils

java.lang.Object
  extended by com.opensymphony.util.XMLUtils

public class XMLUtils
extends Object

XMLUtils is a bunch of quick access utility methods to common XML operations.

These include:

  • Parsing XML stream into org.w3c.dom.Document.
  • Creating blank Documents.
  • Serializing (pretty-printing) Document back to XML stream.
  • Extracting nodes using X-Path expressions.
  • Cloning nodes.
  • Performing XSL transformations.

This class contains static methods only and is not meant to be instantiated. It also contains only basic (common) functions - for more control access appropriate API directly.

Version:
$Revision: 125 $
Author:
Joe Walnes, Hani Suleiman

Constructor Summary
XMLUtils()
           
 
Method Summary
static Node cloneNode(Node node, Document target, boolean deep)
          Clone given Node into target Document.
static int getCacheSize()
          Accessor for the internal XSL transformer cache
static String getElementText(Element element)
          Return the contained text within an Element.
static Document newDocument()
          Create blank Document.
static Document newDocument(String rootElementName)
          Create blank Document, and insert root element with given name.
static Document parse(File file)
          Parse a File of XML into Document.
static Document parse(InputSource in)
          Parse an InputSource of XML into Document.
static Document parse(InputStream in)
          Parse an InputStream of XML into Document.
static Document parse(Reader in)
          Parse a Reader of XML into Document.
static Document parse(String xml)
          Parse a String containing XML data into a Document.
static Document parse(URL url)
          Parse the contents of a URL's XML into Document.
static String print(Document document)
          Pretty-print a Document back to String of XML.
static void print(Document document, File file)
          Pretty-print a Document to File.
static void print(Document document, OutputStream out)
          Pretty-print a Document to OutputStream.
static void print(Document document, Writer out)
          Pretty-print a Document to Writer.
static void setCacheSize(int newCacheSize)
          Sets the internal cache size for XSL sheets
static Document transform(Document xml, Document xsl)
          Perform XSL transformations using given Documents and return new Document.
static void transform(InputStream xml, InputStream xsl, OutputStream result)
          Perform XSL transformation.
static void transform(Reader xml, Reader xsl, Writer result)
          Perform XSL transformation.
static void transform(Reader xml, Reader xsl, Writer result, Map parameters)
          Perform XSL transformation, with params.
static void transform(Reader xml, Reader xsl, Writer result, Map parameters, String xslkey)
          This method applies an XSL sheet to an XML document.
static String transform(String xml, String xsl)
          Perform XSL transformation.
static Node xpath(Node base, String xpath)
          Return single Node from base Node using X-Path expression.
static NodeList xpathList(Node base, String xpath)
          Return multiple Nodes from base Node using X-Path expression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtils

public XMLUtils()
Method Detail

getElementText

public static final String getElementText(Element element)
Return the contained text within an Element. Returns null if no text found.


cloneNode

public static final Node cloneNode(Node node,
                                   Document target,
                                   boolean deep)
                            throws DOMException
Clone given Node into target Document. If targe is null, same Document will be used. If deep is specified, all children below will also be cloned.

Throws:
DOMException

newDocument

public static final Document newDocument()
                                  throws ParserConfigurationException
Create blank Document.

Throws:
ParserConfigurationException

newDocument

public static final Document newDocument(String rootElementName)
                                  throws ParserConfigurationException
Create blank Document, and insert root element with given name.

Throws:
ParserConfigurationException

parse

public static final Document parse(InputSource in)
                            throws ParserConfigurationException,
                                   IOException,
                                   SAXException
Parse an InputSource of XML into Document.

Throws:
ParserConfigurationException
IOException
SAXException

parse

public static final Document parse(InputStream in)
                            throws ParserConfigurationException,
                                   IOException,
                                   SAXException
Parse an InputStream of XML into Document.

Throws:
ParserConfigurationException
IOException
SAXException

parse

public static final Document parse(Reader in)
                            throws ParserConfigurationException,
                                   IOException,
                                   SAXException
Parse a Reader of XML into Document.

Throws:
ParserConfigurationException
IOException
SAXException

parse

public static final Document parse(File file)
                            throws ParserConfigurationException,
                                   IOException,
                                   SAXException
Parse a File of XML into Document.

Throws:
ParserConfigurationException
IOException
SAXException

parse

public static final Document parse(URL url)
                            throws ParserConfigurationException,
                                   IOException,
                                   SAXException
Parse the contents of a URL's XML into Document.

Throws:
ParserConfigurationException
IOException
SAXException

parse

public static final Document parse(String xml)
                            throws ParserConfigurationException,
                                   IOException,
                                   SAXException
Parse a String containing XML data into a Document. Note that String contains XML itself and is not URI.

Throws:
ParserConfigurationException
IOException
SAXException

print

public static final void print(Document document,
                               Writer out)
                        throws IOException
Pretty-print a Document to Writer.

Throws:
IOException

print

public static final void print(Document document,
                               OutputStream out)
                        throws IOException
Pretty-print a Document to OutputStream.

Throws:
IOException

print

public static final void print(Document document,
                               File file)
                        throws IOException
Pretty-print a Document to File.

Throws:
IOException

print

public static final String print(Document document)
                          throws IOException
Pretty-print a Document back to String of XML.

Throws:
IOException

transform

public static final void transform(Reader xml,
                                   Reader xsl,
                                   Writer result)
                            throws TransformerException
Perform XSL transformation.

Throws:
TransformerException

xpath

public static final Node xpath(Node base,
                               String xpath)
                        throws TransformerException
Return single Node from base Node using X-Path expression.

Throws:
TransformerException

xpathList

public static final NodeList xpathList(Node base,
                                       String xpath)
                                throws TransformerException
Return multiple Nodes from base Node using X-Path expression.

Throws:
TransformerException

setCacheSize

public static void setCacheSize(int newCacheSize)
Sets the internal cache size for XSL sheets

Parameters:
newCacheSize -

getCacheSize

public static int getCacheSize()
Accessor for the internal XSL transformer cache

Returns:
the cache size

transform

public static final void transform(Reader xml,
                                   Reader xsl,
                                   Writer result,
                                   Map parameters,
                                   String xslkey)
                            throws TransformerException
This method applies an XSL sheet to an XML document.

2002/Apr/7, fixed bug 540875, first reported by Erik Weber, and added configurable cache size.

Parameters:
xml - the XML source
xsl - the XSL source
result - where to put the response
parameters - a map consisting of params for the transformer
xslkey - a key used to refer to the XSL
Throws:
TransformerException

transform

public static final void transform(Reader xml,
                                   Reader xsl,
                                   Writer result,
                                   Map parameters)
                            throws TransformerException
Perform XSL transformation, with params.

Throws:
TransformerException

transform

public static final void transform(InputStream xml,
                                   InputStream xsl,
                                   OutputStream result)
                            throws TransformerException
Perform XSL transformation.

Throws:
TransformerException

transform

public static final String transform(String xml,
                                     String xsl)
                              throws TransformerException
Perform XSL transformation. XML and XSL supplied in Strings and result returned as String. Note that inputs are actual XML/XSL data, not URIs.

Throws:
TransformerException

transform

public static final Document transform(Document xml,
                                       Document xsl)
                                throws ParserConfigurationException,
                                       TransformerException
Perform XSL transformations using given Documents and return new Document.

Throws:
ParserConfigurationException
TransformerException

OSCore Project Page

Copyright 2000-2005 - OpenSymphony