Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();

        // Create original document
        Document document = db.newDocument();
        Element root = document.createElementNS("urn:FOO", "ns0:Root");
        document.appendChild(root);
        Element request = document.createElementNS("urn:FOO", "ns0:Request");
        root.appendChild(request);

        // Create new Request element.
        Element newRequest = document.createElementNS("urn:BAR", "ns1:Request");

        // Replace Request element
        root.replaceChild(newRequest, request);

        // Output the new document
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(System.out);
        t.transform(source, result);
    }

}