Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
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.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class Main {
    public static void addNewNode(File xmlfile, String nodeXpath, String elementName, String attributeName,
            String attributeValue) throws Exception {
        // Get the Document object.
        //logger.debug("Add node method, adding a new node in xml file " + xmlfile.toString());
        Document doc = getDocument(xmlfile);
        // get the Node Object for the xpath.
        Node node = getNodeObject(nodeXpath, doc);

        // Create a New Element
        Element element = doc.createElement(elementName);
        // Set the Attributes in to the Element
        element.setAttribute(attributeName, attributeValue);
        // Append The Element as a child in side the Parent Node
        node.appendChild(element);

        wirteXmlFile(doc, xmlfile);
        //logger.debug("New node is added to the xml file");
    }

    private static Document getDocument(File xmlfile)
            throws ParserConfigurationException, SAXException, IOException {
        //logger.debug("Creating an Object of the Document");
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(xmlfile);
        return doc;
    }

    private static Node getNodeObject(String xpathString, Document doc) throws XPathExpressionException {
        // Create a xptah object
        //logger.debug("Getting the node by using xpath " + xpathString);
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xpath = xpf.newXPath();
        XPathExpression expr = xpath.compile(xpathString);
        Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        //logger.debug("Returning the Node object from xml file");
        return node;
    }

    private static void wirteXmlFile(Document doc, File file) throws TransformerException {
        // Write the content into xml file
        //logger.debug("Changes are done in xml file, Writing to the XML file");
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(file);
        transformer.transform(source, result);
    }
}