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 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.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;

import java.io.InputStream;

import java.io.StringWriter;

import java.util.Set;

public class Main {
    private static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    private static XPath xPath = XPathFactory.newInstance().newXPath();

    /**
     * Merge 2 xml documents into one by preserving some fields from the original xml
     * while updating other fields from the incoming xml xml. The resulting document is save into
     * the result xml.
     *
     * @param origXml         the original xml
     * @param incomingXml     the incoming xml
     * @param preservedFields a set of preserved fields
     * @throws Exception if any
     */
    public static String mergeXmlByPreservingField(String origXml, String incomingXml, Set<String> preservedFields)
            throws Exception {
        Document origDoc = xmlToDocument(origXml);
        Document incomingDoc = xmlToDocument(incomingXml);
        Document mergedDoc = mergeXmlByPreservingField(origDoc, incomingDoc, preservedFields);

        return docToString(mergedDoc);
    }

    private static Document mergeXmlByPreservingField(Document origDoc, Document incomingDoc,
            Set<String> preservedFields) throws Exception {
        for (String preservedField : preservedFields) {
            NodeList origNode = findNodeByXpath(origDoc, preservedField);
            NodeList incomingNode = findNodeByXpath(incomingDoc, preservedField);
            if (origNode != null && origNode != null) {
                for (int i = 0; i < incomingNode.getLength(); i++) {
                    incomingNode.item(i).getFirstChild()
                            .setNodeValue(origNode.item(i).getFirstChild().getNodeValue());
                }
            }
        }
        return incomingDoc;
    }

    /**
     * Marshall the xml to a document
     *
     * @param xml the xml
     * @return the xml document parsed from the
     * @throws Exception if any
     */
    public static Document xmlToDocument(String xml) throws Exception {
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream incomingIs = new ByteArrayInputStream(xml.getBytes());
        Document doc = db.parse(incomingIs);
        incomingIs.close();

        return doc;
    }

    private static String docToString(Document doc) throws Exception {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(source, result);

        return result.getWriter().toString();
    }

    /**
     * Find the node matching the xpath in the xml document
     *
     * @param doc   the xml document
     * @param xpath the xpath expression
     * @return the node list matching the xpath, null if not found
     */
    public static NodeList findNodeByXpath(Document doc, String xpath) {
        try {
            return (NodeList) xPath.compile(xpath).evaluate(doc, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            return null;
        }
    }
}