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

StringtoLCCNDisplay(String packedLCCN)
Convert a packed LCCN String to MARC display format.
StringBuffer sb = new StringBuffer();
if (Character.isDigit(packedLCCN.charAt(2))) {
    sb.append(packedLCCN.substring(0, 2).trim());
    sb.append(packedLCCN.substring(2, 6));
    sb.append("-");
    int i = Integer.parseInt(packedLCCN.substring(6).trim());
    sb.append(Integer.toString(i));
} else {
...
IntegertomcatPort(String fileName)
tomcat Port
String xpathExpression = "/Server/Service/Connector[@protocol='HTTP/1.1']/@port";
String value = string(fileName, xpathExpression);
try {
    int parseInt = Integer.parseInt(value);
    return parseInt;
} catch (NumberFormatException e) {
    e.printStackTrace();
    return null;
...
StringtoString(Collection c, String separator)
Converts collection to a separated string.
StringBuffer b = new StringBuffer();
for (Iterator<String> iter = c.iterator(); iter.hasNext();) {
    b.append(iter.next());
    b.append(separator);
if (c.size() > 0) {
    b.delete(b.length() - separator.length(), b.length());
return b.toString();
StringtoString(Source input)
Adapter method used to convert any type of Source to a String
if (input == null)
    return null;
StringWriter writer = new StringWriter();
Transformer trasformer;
try {
    trasformer = TransformerFactory.newInstance().newTransformer();
    Result result = new StreamResult(writer);
    trasformer.transform(input, result);
...
voidwrite(Node node, OutputStream out, String... props)
Writes a node to an OutputStream .
StreamResult streamResult = new StreamResult(out);
write(node, streamResult, props);
voidwriteElement(Element element, String fileName)
Writes an element out to a file.
File file = new File(fileName);
file.delete();
DOMSource domSource = new DOMSource(element);
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(file);
    StreamResult result = new StreamResult(fos);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
...
voidwriteNodeSubtreeXMLString(final Node node, final Writer writer)
write Node Subtree XML String
try {
    final DOMSource domSource = new DOMSource(node);
    final StreamResult result = new StreamResult(writer);
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
} catch (TransformerException ex) {
    ex.printStackTrace();
...
StringwritePropToString(Properties props)
Convert Properties to string
try {
    org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    org.w3c.dom.Element conf = doc.createElement("configuration");
    doc.appendChild(conf);
    conf.appendChild(doc.createTextNode("\n"));
    for (Enumeration e = props.keys(); e.hasMoreElements();) {
        String name = (String) e.nextElement();
        Object object = props.get(name);
...
voidwriteResultToFile(final StreamResult result, final String path)
save StreamResult to file given in path
final String fileString = result.getWriter().toString();
try {
    final BufferedWriter out = new BufferedWriter(new FileWriter(path));
    out.write(fileString);
    out.close();
} catch (final IOException e) {
    e.printStackTrace();
org.w3c.dom.DocumentwriteToFile(String xmlContent, String path)
write To File
System.out.println("This is the path " + path);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
    builder = factory.newDocumentBuilder();
    org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlContent)));
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
...