Java Utililty Methods XML Transform Usage

List of utility methods to do XML Transform Usage

Description

The list of methods to do XML Transform Usage are organized into topic(s).

Method

Documentparse(File f)
parse
DocumentBuilder db = documentBuilder();
try {
    return db.parse(f);
} finally {
    db.reset();
voidparse(final InputStream file, final ContentHandler handler)
Parse a file and send the sax events to the content handler.
final Transformer transformer = FACTORY.newTransformer();
transformer.transform(new StreamSource(file), new SAXResult(handler));
Stringpath(Element element)
Return an path expression describing an element
StringBuilder sb = new StringBuilder();
Node iterator = element;
while (iterator.getNodeType() == Node.ELEMENT_NODE) {
    sb.insert(0, ((Element) iterator).getTagName());
    sb.insert(0, "/");
    iterator = iterator.getParentNode();
return sb.toString();
...
DocumentreadDoc(Reader in)
Returns a document read from an Reader .
return doReadDoc(in, new StreamSource(in));
StringrenderElement(Element theElem)
render Element
try {
    Writer aOutput = new StringWriter();
    Source aSource = new DOMSource(theElem);
    StreamResult aStreamResult = new StreamResult(aOutput);
    Transformer aTransformer = TransformerFactory.newInstance().newTransformer();
    aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
    aTransformer.setOutputProperty(OutputKeys.METHOD, "html");
...
StringsafeToXml(Element element)
safe To Xml
try {
    return toXml(element);
} catch (TransformerException e) {
    return "Error transforming to xml: " + e.toString();
javax.xml.transform.stream.StreamSourcestreamSource(File file)
stream Source
return new StreamSource(new FileInputStream(file));
SourcetoNonValidatingSAXSource(InputStream in)
Returns a SAXSource for the provided InputStream that explicitly forfeit external DTD validation
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
InputSource inputSource = new InputSource(in);
return new SAXSource(xmlReader, inputSource);
XMLInputSourcetoXMLInputSource(StreamSource in)
Creates a proper XMLInputSource from a StreamSource .
if (in.getReader() != null)
    return new XMLInputSource(in.getPublicId(), in.getSystemId(), in.getSystemId(), in.getReader(), null);
if (in.getInputStream() != null)
    return new XMLInputSource(in.getPublicId(), in.getSystemId(), in.getSystemId(), in.getInputStream(),
            null);
return new XMLInputSource(in.getPublicId(), in.getSystemId(), in.getSystemId());
ThrowableunwrapException(Throwable t)
unwrap Exception
Throwable current = t;
Throwable next = null;
while (current != null) {
    if (current instanceof InvocationTargetException) {
        next = ((InvocationTargetException) current).getTargetException();
    } else if (current instanceof ClassNotFoundException) {
        next = ((ClassNotFoundException) current).getException();
    } else if (current instanceof ExceptionInInitializerError) {
...