Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import javax.xml.xpath.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
    public static String editPath(String xmlSource, String xPath, String newValue)
            throws ParserConfigurationException, SAXException, IOException, XPathExpressionException,
            TransformerException {
        Document xmlDoc = readString(xmlSource);
        XPathExpression expr = evaluateThePath(xPath);
        NodeList nl = (NodeList) expr.evaluate(xmlDoc, XPathConstants.NODESET);
        for (int i = nl.getLength(); i > 0; i--) {
            nl.item(0).setNodeValue(newValue);
        }
        return xmlDocToString(xmlDoc);
    }

    private static Document readString(String xmlSource)
            throws IOException, SAXException, ParserConfigurationException {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        return docBuilder.parse(new ByteArrayInputStream(xmlSource.getBytes(StandardCharsets.UTF_8)));
    }

    private static XPathExpression evaluateThePath(String xPath) throws XPathExpressionException {
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        return xpath.compile(xPath + "/text()");//small hack to get to the actual value as this is what we actually need and is mostly used
    }

    public static String xmlDocToString(Document document) throws TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        return writer.getBuffer().toString();
    }
}