Example usage for org.w3c.dom Document createElement

List of usage examples for org.w3c.dom Document createElement

Introduction

In this page you can find the example usage for org.w3c.dom Document createElement.

Prototype

public Element createElement(String tagName) throws DOMException;

Source Link

Document

Creates an element of the type specified.

Usage

From source file:com.hangum.tadpole.engine.sql.util.QueryUtils.java

/**
 * execute DML//from   ww w. j  av  a  2s  .  c  o m
 * 
 * @param userDB
 * @param strQuery
 * @param listParam
 * @param resultType
 * @throws Exception
 */
public static String executeDML(final UserDBDAO userDB, final String strQuery, final List<Object> listParam,
        final String resultType) throws Exception {
    SqlMapClient client = TadpoleSQLManager.getInstance(userDB);
    Object effectObject = runSQLOther(userDB, strQuery, listParam);

    String strReturn = "";
    if (resultType.equals(RESULT_TYPE.CSV.name())) {
        final StringWriter stWriter = new StringWriter();
        CSVWriter csvWriter = new CSVWriter(stWriter, ',');

        String[] arryString = new String[2];
        arryString[0] = "effectrow";
        arryString[1] = String.valueOf(effectObject);
        csvWriter.writeNext(arryString);

        strReturn = stWriter.toString();
    } else if (resultType.equals(RESULT_TYPE.JSON.name())) {
        final JsonArray jsonArry = new JsonArray();
        JsonObject jsonObj = new JsonObject();
        jsonObj.addProperty("effectrow", String.valueOf(effectObject));
        jsonArry.add(jsonObj);

        strReturn = JSONUtil.getPretty(jsonArry.toString());
    } else {//if(resultType.equals(RESULT_TYPE.XML.name())) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        final Document doc = builder.newDocument();
        final Element results = doc.createElement("Results");
        doc.appendChild(results);

        Element row = doc.createElement("Row");
        results.appendChild(row);
        Element node = doc.createElement("effectrow");
        node.appendChild(doc.createTextNode(String.valueOf(effectObject)));
        row.appendChild(node);

        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", 4);

        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        final StringWriter stWriter = new StringWriter();
        StreamResult sr = new StreamResult(stWriter);
        transformer.transform(domSource, sr);

        strReturn = stWriter.toString();
    }

    return strReturn;
}

From source file:Main.java

public static void addNodeToXml(String nodeParentXpathStr, String xmlFilePath, String nodeName, String value,
        Map<String, String> attr) throws Exception {
    Document doc = null;
    if (xmlFilePath == null || nodeParentXpathStr == null || nodeName == null || nodeParentXpathStr == null)
        throw new Exception("some parameters can not be null!");
    doc = dombuilder.parse(new File(xmlFilePath));
    Node pNode = (Node) xpath.compile(nodeParentXpathStr).evaluate(doc, XPathConstants.NODE);
    if (pNode == null)
        throw new Exception("can not find the node specified in nodeParentXpathStr!");
    ;//from   w w  w .  j  a  v a  2  s  .c  o m

    Element newNode = doc.createElement(nodeName);
    newNode.setTextContent(value);
    if (attr != null && !attr.isEmpty()) {
        for (String key : attr.keySet())
            newNode.setAttribute(key, attr.get(key));
    }

    pNode.appendChild(newNode);
    writeToXmlFile(doc, xmlFilePath);

}

From source file:com.aurel.track.exchange.docx.exporter.CustomXML.java

/**
 * Add watcher nodes to xml//from  www. j  av a 2s .  c  o  m
 * @param watcherPersonBeans
 * @param root
 * @param dom
 * @param commentsName
 * @param watcherElement
 */
private static void addCommentNodes(List<HistoryValues> comments, Element root, Document dom, Locale locale) {
    if (comments == null || comments.isEmpty()) {
        appendChild(root, CUSTOM_XML_ELEMENTS.COMMENTS + CUSTOM_XML_ELEMENTS.PRESENT_SUFFIX,
                Boolean.FALSE.toString(), dom);
    } else {
        Element consultedList = dom.createElement(CUSTOM_XML_ELEMENTS.COMMENTS);
        for (HistoryValues historyValue : comments) {
            String commentText = historyValue.getNewShowValue();
            commentText = StringEscapeUtils.escapeHtml3(commentText);
            Element commentTextElement = createDomElement(CUSTOM_XML_ELEMENTS.COMMENT_TEXT, commentText, dom);
            Element commentedByElement = createDomElement(CUSTOM_XML_ELEMENTS.COMMENTED_BY,
                    historyValue.getChangedByName(), dom);
            Element commentedAtElement = createDomElement(CUSTOM_XML_ELEMENTS.COMMENTED_AT,
                    DateTimeUtils.getInstance().formatGUIDate(historyValue.getLastEdit(), locale), dom);
            Element commentElement = createDomElement(CUSTOM_XML_ELEMENTS.COMMENT, "", dom);
            commentElement.appendChild(commentTextElement);
            commentElement.appendChild(commentedByElement);
            commentElement.appendChild(commentedAtElement);
            consultedList.appendChild(commentElement);
        }
        root.appendChild(consultedList);
        appendChild(root, CUSTOM_XML_ELEMENTS.COMMENTS + CUSTOM_XML_ELEMENTS.PRESENT_SUFFIX,
                Boolean.TRUE.toString(), dom);
    }
}

From source file:com.hphoto.server.ApiServlet.java

private static Element addNode(Document doc, Node parent, String name) {
    Element child = doc.createElement(name);
    parent.appendChild(child);//from w  w  w  . j a v a  2  s . co  m
    return child;
}

From source file:Main.java

public static Document makeEmptyXmlDocument(String rootElementName) {
    Document document = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(true);// w ww  .ja v a2 s  .  c o m
    // factory.setNamespaceAware(true);
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();

        document = builder.newDocument();
    } catch (Exception e) {
        //Debug.logError(e, module);
    }

    if (document == null)
        return null;

    if (rootElementName != null) {
        Element rootElement = document.createElement(rootElementName);
        document.appendChild(rootElement);
    }

    return document;
}

From source file:com.hphoto.server.ApiServlet.java

private static Element addNode(Document doc, Node parent, String name, String text) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(getLegalXml(text)));
    parent.appendChild(child);/*ww  w .  j  av  a 2s  .  c o  m*/
    return child;
}

From source file:com.aurel.track.exchange.docx.exporter.CustomXML.java

/**
 * Add watcher nodes to xml//from  w  w w . j a va2 s  .co  m
 * @param watcherPersonBeans
 * @param root
 * @param dom
 * @param watcherListName
 * @param watcherElement
 */
private static void addWatcherNodes(List<TPersonBean> watcherPersonBeans, Element root, Document dom,
        String watcherListName, String watcherElement) {
    if (watcherPersonBeans == null || watcherPersonBeans.isEmpty()) {
        appendChild(root, watcherListName + CUSTOM_XML_ELEMENTS.PRESENT_SUFFIX, Boolean.FALSE.toString(), dom);
    } else {
        Element watcherList = dom.createElement(watcherListName);
        for (TPersonBean personBean : watcherPersonBeans) {
            Element nameElement = createDomElement(CUSTOM_XML_ELEMENTS.NAME, personBean.getName(), dom);
            Element person = createDomElement(watcherElement, "", dom);
            person.appendChild(nameElement);
            watcherList.appendChild(person);
        }
        root.appendChild(watcherList);
        appendChild(root, watcherListName + CUSTOM_XML_ELEMENTS.PRESENT_SUFFIX, Boolean.TRUE.toString(), dom);
    }
}

From source file:fr.ece.epp.tools.Utils.java

public static void updatePom(String path, String[] repo, boolean outOrno) {
    Document document = null;
    try {/*w w  w.  j av  a  2 s.  c o  m*/
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
        Element root = document.getDocumentElement();
        Node repositories = root.getElementsByTagName("repositories").item(0);
        for (int i = 0; i < repo.length; i++) {
            Element repository = document.createElement("repository");

            Element id = document.createElement("id");
            id.appendChild(document.createTextNode("repository" + i));
            repository.appendChild(id);

            Element layout = document.createElement("layout");
            layout.appendChild(document.createTextNode("p2"));
            repository.appendChild(layout);

            Element url = document.createElement("url");
            url.appendChild(document.createTextNode(repo[i]));

            repository.appendChild(url);
            repositories.appendChild(repository);
        }
        output(root, path);
        if (outOrno) {
            output(root, null);
        }

    } catch (SAXException e) {
    } catch (IOException e) {
    } catch (ParserConfigurationException e) {
    }
}

From source file:com.viettel.ws.client.JDBCUtil.java

/**
 * Create document using DOM api/*from  ww  w .j a v a  2  s  .co  m*/
 *
 * @param rs a result set
 * @param doc a input document for append content
 * @param rsName name of the appended element
 * @return a document after append content
 * @throws ParserConfigurationException If error when parse XML string
 * @throws SQLException If error when read data from database
 */
public static Document add2Document(List<Object> rs, Document doc, String rsName)
        throws ParserConfigurationException, SQLException {

    if (rs == null) {
        return doc;
    }

    //Get root element
    Element root = doc.getDocumentElement();
    Element rsElement = doc.createElement(rsName);
    root.appendChild(rsElement);

    for (Object object : rs) {
        Element row = createRowElement(object, doc);
        rsElement.appendChild(row);
    }

    return doc;
}

From source file:com.photon.phresco.service.util.ServerUtil.java

/**
 * To create pom.xml file for artifact upload
 * /*from ww w . j av a2s.co  m*/
 * @param info
 * @return
 * @throws PhrescoException
 */
public static File createPomFile(ArtifactGroup info) throws PhrescoException {
    FileWriter writer = null;
    File pomFile = getPomFile();
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder domBuilder = domFactory.newDocumentBuilder();

        org.w3c.dom.Document newDoc = domBuilder.newDocument();
        Element rootElement = newDoc.createElement(ServerConstants.POM_PROJECT);
        newDoc.appendChild(rootElement);

        Element modelVersion = newDoc.createElement(ServerConstants.POM_MODELVERSION);
        modelVersion.setTextContent(ServerConstants.POM_MODELVERSION_VAL);
        rootElement.appendChild(modelVersion);

        Element groupId = newDoc.createElement(ServerConstants.POM_GROUPID);
        groupId.setTextContent(info.getGroupId());
        rootElement.appendChild(groupId);

        Element artifactId = newDoc.createElement(ServerConstants.POM_ARTIFACTID);
        artifactId.setTextContent(info.getArtifactId());
        rootElement.appendChild(artifactId);

        Element version = newDoc.createElement(ServerConstants.POM_VERSION);
        version.setTextContent(info.getVersions().get(0).getVersion());
        rootElement.appendChild(version);

        Element packaging = newDoc.createElement(ServerConstants.POM_PACKAGING);
        packaging.setTextContent(info.getPackaging());
        rootElement.appendChild(packaging);

        Element description = newDoc.createElement(ServerConstants.POM_DESC);
        description.setTextContent(ServerConstants.POM_DESC_VAL);
        rootElement.appendChild(description);

        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ServerConstants.POM_OMMIT);
        trans.setOutputProperty(OutputKeys.INDENT, ServerConstants.POM_DESC);

        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(newDoc);
        trans.transform(source, result);
        String xmlString = sw.toString();
        writer = new FileWriter(pomFile);
        writer.write(xmlString);
        writer.close();
    } catch (Exception e) {
        throw new PhrescoException(e);
    } finally {
        Utility.closeStream(writer);
    }

    return pomFile;
}