Java Utililty Methods XML String Transform

List of utility methods to do XML String Transform

Description

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

Method

voidaddElementToXml(String xmlFile, String nodeToAdd, String nodeContent)
add Element To Xml
Document doc = getXmlDoc(xmlFile);
NodeList nodes = doc.getChildNodes();
Node node = doc.createElement(nodeToAdd);
node.setNodeValue(nodeContent);
nodes.item(0).appendChild(node);
saveXml(doc, xmlFile);
voidaddNode(String nodeType, String idField, String nodeID, File destFile, ArrayList attributes)
Adds a new node to a file.
if (attributes != null) {
    for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) {
        if (it.next().length != 2) {
            throw new IllegalArgumentException("Invalid attribute combination");
DocumentBuilder docBuilder;
...
voidapplyXSL(File xmlFile, File xslFile, String outputFilename)
Transforms an xml-file (xmlFilename) using an xsl-file (xslFilename) and writes the output into file (outputFilename).
try {
    StreamSource xslStream = new StreamSource(xslFile);
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer(xslStream);
    StreamSource xmlStream = new StreamSource(xmlFile);
    StreamResult result = new StreamResult(new BufferedWriter(new FileWriter(outputFilename)));
    transformer.transform(xmlStream, result);
    result.getWriter().close();
...
Stringbase64encode(String decodedString)
baseencode
return DatatypeConverter.printBase64Binary(decodedString.getBytes());
voidconvertPlist(File info_plist_file, String script_url, Map script_parameters)
convert Plist
try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder document_builder = factory.newDocumentBuilder();
    document_builder.setEntityResolver((String publicId, String systemId) -> {
        if (publicId.equals("-//Apple Computer//DTD PLIST 1.0//EN"))
            return new InputSource(
                    new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
        else
...
voidconvertResult(StreamResult result, StringWriter writer)
convert Result
ByteArrayOutputStream baos = (ByteArrayOutputStream) result.getOutputStream();
writer.write(baos.toString());
voidconvertValidatorResult(Result result, StringWriter writer)
Writes the content of the given Result into a StringWriter
if (result instanceof DOMResult) {
    convertResult((DOMResult) result, writer);
} else if (result instanceof StreamResult) {
    convertResult((StreamResult) result, writer);
} else {
    throw new RuntimeException(String.format("Could not evaluate Schematron validation result of type '%s'",
            result.getClass().getCanonicalName()));
ElementcreateElement(Node node, String name, String value, Map attributes)
create Element
Document doc = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument();
Element element = doc.createElement(name);
element.setTextContent(value);
addAttributes(element, attributes);
return element;
voidcreateFullReportLog(String sessionLogDir)
Generate a file in logDir refererring all logfiles.
File xml_logs_report_file = new File(sessionLogDir + File.separator + "report_logs.xml");
if (xml_logs_report_file.exists()) {
    xml_logs_report_file.delete();
    xml_logs_report_file.createNewFile();
xml_logs_report_file = new File(sessionLogDir + File.separator + "report_logs.xml");
OutputStream report_logs = new FileOutputStream(xml_logs_report_file);
List<File> files = null;
...
ObjectcreateObject(String classname)
create an object using reflection Examples JDReflectionUtil.createObject("com.ibm.db2.jcc.DB2XADataSource") callMethod_V(ds, "setTranslateHex", "character");
Class objectClass1 = Class.forName(classname);
Class[] noArgTypes = new Class[0];
Object[] noArgs = new Object[0];
Object newObject = null;
try {
    Constructor constructor = objectClass1.getConstructor(noArgTypes);
    newObject = constructor.newInstance(noArgs);
} catch (java.lang.reflect.InvocationTargetException ite) {
...