Code example for Document

Methods: appendChild, createElement, createElementNS, createTextNode

0
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document xmlDoc = builder.newDocument();
 
        Element root = xmlDoc.createElementNS(NS, "Autodiscover");
        root.setAttribute("xmlns", NS);
        root.setAttribute("xmlns:xsi", XSI_NS);
        root.setAttribute("xmlns:xsd", XSD_NS);
        xmlDoc.appendChild(root);
 
        //Add the response element. 
        Element response = xmlDoc.createElementNS(NS, "Response");
        root.appendChild(response);
 
        //Add culture to to response 
        Element culture = xmlDoc.createElement("Culture");
        culture.appendChild(xmlDoc.createTextNode("en:en"));
        response.appendChild(culture);
 
        //User 
        Element user = xmlDoc.createElement("User");
        Element displayNameElm = xmlDoc.createElement("DisplayName");
        displayNameElm.appendChild(xmlDoc.createTextNode(displayName));
        user.appendChild(displayNameElm);
        Element emailAddr = xmlDoc.createElement("EMailAddress");
        emailAddr.appendChild(xmlDoc.createTextNode(email));
        user.appendChild(emailAddr);
        response.appendChild(user);
 
        //Action 
        Element action = xmlDoc.createElement("Action");
        Element settings = xmlDoc.createElement("Settings");
        Element server = xmlDoc.createElement("Server");
 
        Element type = xmlDoc.createElement("Type");
        type.appendChild(xmlDoc.createTextNode("MobileSync"));
        server.appendChild(type);
 
        Element url = xmlDoc.createElement("Url");
        url.appendChild(xmlDoc.createTextNode(serviceUrl));
        server.appendChild(url);
 
        Element name = xmlDoc.createElement("Name");
        name.appendChild(xmlDoc.createTextNode(serviceUrl));
        server.appendChild(name);
 
        settings.appendChild(server);
        action.appendChild(settings);
        response.appendChild(action);