Here's a utility class with some static methods I use for this:
package com.hostedtelecom.callcentreweb.util;
import java.io.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;
import org.xml.sax.InputSource;
/**
* Utilty class for XML basic tasks
*/
public class XMLHelper
{
/** Convert W3C XML Document to String.
@param document
@return String
@throws XMLHelperException
*/
public static final String getDocumentAsString(Document document)
throws XMLHelperException
{
try
{
// Create source and result objects
Source source = new DOMSource(document);
StringWriter out = new StringWriter();
Result result = new StreamResult(out);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.transform(source, result);
return out.toString();
}
catch(Exception e)
{
throw new XMLHelperException("XML Document to String Error", e);
}
}
/** Convert String to a W3C XML Document.
@param xmlString
@return Document
@throws XMLHelperException
*/
public static final Document getDocument(String xmlString) throws
XMLHelperException
{
try
{
String nstr = null;
//cannot have whitespace in the beginning of an xml document
if (xmlString.charAt(0) != ' ')
{
nstr = removeInitialWS(xmlString);
}
else
{
nstr = xmlString;
}
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setIgnoringElementContentWhitespace(false);
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource isXml = new InputSource (new StringReader(nstr));
return builder.parse(isXml);
}
catch(Exception e)
{
throw new XMLHelperException("String to XML Document Error for
String:\n\n "+xmlString+" ", e);
}
}
/**
Remove any blank spaces in beginning of the XML declaration
*/
public static final String removeInitialWS(String xmlString) throws
XMLHelperException
{
try
{
int pos = xmlString.indexOf("<");
if (-1 == pos)
{
throw new Exception("Invalid XML, char '<' not found");
}
return xmlString.substring(pos);
}
catch(Exception e)
{
throw new XMLHelperException("String to XML Document Error for
String: \n\n "+xmlString+" ", e);
}
}
public static final String getNodeToString(Node node) throws
XMLHelperException
{
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty("omit-xml-declaration", "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource( node );
transformer.transform( source, result );
return sw.getBuffer().toString();
}
catch(Exception e)
{
throw new XMLHelperException("XML Document to String Err", e);
}
}
}
HTH,
iksrazal
http://www.braziloutsource.com
Nomak - 18 May 2005 14:15 GMT
> [...]
and how do you use it?
i can't believe knowbody has think about that in the xerces team...