List of utility methods to do XML Transform Usage
Document | parse(File f) parse DocumentBuilder db = documentBuilder(); try { return db.parse(f); } finally { db.reset(); |
void | parse(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)); |
String | path(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(); ... |
Document | readDoc(Reader in) Returns a document read from an Reader . return doReadDoc(in, new StreamSource(in)); |
String | renderElement(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"); ... |
String | safeToXml(Element element) safe To Xml try { return toXml(element); } catch (TransformerException e) { return "Error transforming to xml: " + e.toString(); |
javax.xml.transform.stream.StreamSource | streamSource(File file) stream Source return new StreamSource(new FileInputStream(file)); |
Source | toNonValidatingSAXSource(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); |
XMLInputSource | toXMLInputSource(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()); |
Throwable | unwrapException(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) { ... |