Xml Helper Class for Groovy / Java

Using the standard java XML library could be a little bit complex, especially when you just want a quick handling of XML files and don’t want to dive deep into the codes that parse it.

Following code provides a wrapped helper class which helps to do this.


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.StringWriter;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlUtil {
public static Document getXmlDocumentByString(String xmlString) {
Document doc = null;
try {
InputStream is = new ByteArrayInputStream(xmlString.getBytes());

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(false);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(is);

} catch (Exception ex) {
ex.printStackTrace();
}
return doc;
}

public static Document getXmlDocumentByBytes(byte[] inputBytes) {
Document doc = null;
try {
InputStream is = new ByteArrayInputStream(inputBytes);

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(false);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(is);

} catch (Exception ex) {
ex.printStackTrace();
}
return doc;
}

public static NodeList getXmlNodeListByXPath(Document doc, String xpath) throws Exception {
XPath xPath = XPathFactory.newInstance().newXPath();
return (NodeList) xPath.compile(xpath).evaluate(doc, XPathConstants.NODESET);
}

public static NodeList getXmlNodeListByXPath(Node node, String xpath) throws Exception {
XPath xPath = XPathFactory.newInstance().newXPath();
return (NodeList) xPath.compile(xpath).evaluate(node, XPathConstants.NODESET);
}

public static Node getXmlNodeByXPath(Node node, String xpath) throws Exception {
XPath xPath = XPathFactory.newInstance().newXPath();
return (Node) xPath.compile(xpath).evaluate(node, XPathConstants.NODE);
}

public static Document removeNodesByTag(Document xmlDoc, String nodeTag) {
NodeList nl = xmlDoc.getElementsByTagName(nodeTag);
for (int i = nl.getLength() - 1; i >= 0; i--) {
Node n = nl.item(i);
n.getParentNode().removeChild(n);
}

return xmlDoc;
}

public static String prettyPrint(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
return out.toString();
}

// following file exscapes the xml specific chars inside the string.
public static String escapeXml(String s) {
return s.replaceAll("&", "&")
.replaceAll(">", ">")
.replaceAll("<", "<")
.replaceAll("\"", """)
.replaceAll("'", "'");
}

public static byte[] readBytesFromFile(String filePath) {

FileInputStream fileInputStream = null;
byte[] bytesArray = null;
try {

File file = new File(filePath);
bytesArray = new byte[(int) file.length()];

//read file into bytes[]
fileInputStream = new FileInputStream(file);
fileInputStream.read(bytesArray);

} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytesArray;
}
}