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:Main.java

public static Element beanToXml(Document document, Object obj)
        throws ParserConfigurationException, InvocationTargetException, IllegalAccessException {
    String name = obj.getClass().getName();
    Element rootElement = document.createElement(name);
    Method[] methods = obj.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String methodName = method.getName();
        String fieldName = analyzeFieldName(methodName);
        Element fieldElement = document.createElement(fieldName);
        if (methodName.startsWith("get") && !"getClass".equals(methodName)) {
            Object value = method.invoke(obj);
            if (value != null && !"".equals(value.toString())) {
                fieldElement.setTextContent(value.toString());
            } else {
                continue;
            }/*w  ww  .  j a va  2s.co  m*/
        } else {
            continue;
        }
        rootElement.appendChild(fieldElement);
    }
    return rootElement;
}

From source file:Utils.java

public static Element findElementElseCreateAndSet(Document document, Element parent, String child,
        String value) {//from  w w  w .  j  av  a 2 s .  c  o  m
    Element ret = null;
    NodeList nl = parent.getElementsByTagName(child);
    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(child));
        ret = (Element) parent.getElementsByTagName(child).item(0);
        ret.appendChild(document.createTextNode(value));
    }
    return ret;
}

From source file:Main.java

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);/* ww  w  .j a v  a 2  s . c  om*/

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

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

/**
 * make content/*from  w ww  . j  a  va  2  s . c o m*/
 * 
 * @param tableName
 * @param rsDAO
 * @return
 */
public static String makeContent(String tableName, QueryExecuteResultDTO rsDAO, int intLimitCnt)
        throws Exception {
    final StringWriter stWriter = new StringWriter();
    final List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.newDocument();
    final Element results = doc.createElement(tableName);
    doc.appendChild(results);

    Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();
    for (int i = 0; i < dataList.size(); i++) {
        Map<Integer, Object> mapColumns = dataList.get(i);
        Element row = doc.createElement("Row");
        results.appendChild(row);

        for (int j = 1; j < mapColumns.size(); j++) {
            String columnName = mapLabelName.get(j);
            String strValue = mapColumns.get(j) == null ? "" : "" + mapColumns.get(j);

            Element node = doc.createElement(columnName);
            node.appendChild(doc.createTextNode(strValue));
            row.appendChild(node);
        }

        if (i == intLimitCnt)
            break;
    }

    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");//"ISO-8859-1");
    StreamResult sr = new StreamResult(stWriter);
    transformer.transform(domSource, sr);

    return stWriter.toString();
}

From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

private static Element buildAssociation(Document doc, AssociationBean associationBean) {
    // association
    Element association = doc.createElement(ENTRY_ASSOCIATION_NODE);
    association.setAttribute(ENTRY_QUALIFIEDNAME, associationBean.getAssociationName());
    if (associationBean.getAssoType().equals(AssoType.Simple)) {
        association.setAttribute(ENTRY_ASSOCIATION_TYPE, ENTRY_ASSOCIATION_TYPE_SIMPLE);
    } else {/*from   w  w  w  .j  ava  2  s.com*/
        association.setAttribute(ENTRY_ASSOCIATION_TYPE, ENTRY_ASSOCIATION_TYPE_COMPOSITION);
    }
    // target
    String targetValue = associationBean.getTargetId();
    String targetLabel = associationBean.getTargetLabel();

    String targetNodeType = associationBean.getTargetQualifiedName();
    Element target = doc.createElement(ENTRY_ASSOCIATION_TARGET_NODE);
    target.setAttribute(ENTRY_QUALIFIEDNAME, targetNodeType);
    target.setTextContent(targetValue);
    if (StringUtils.trimToNull(targetLabel) != null) {
        target.setAttribute("label", targetLabel);
    }
    association.appendChild(target);

    return association;
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static Element value(Document doc, String attname, String val) {
    synchronized (doc) {
        Element strElm = doc.createElement("str");
        if (attname != null)
            strElm.setAttribute("name", attname);
        strElm.setTextContent(val);
        return strElm;
    }/*from  w w w.jav a2s.c o  m*/
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static Element value(Document doc, String attname, Integer val) {
    synchronized (doc) {
        Element strElm = doc.createElement("int");
        if (attname != null)
            strElm.setAttribute("name", attname);
        strElm.setTextContent("" + val);
        return strElm;
    }/*from ww  w. j  av a 2  s . com*/
}

From source file:com.msopentech.odatajclient.engine.data.atom.AtomSerializer.java

private static Element feed(final AtomFeed feed) throws ParserConfigurationException {
    final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
    final Document doc = builder.newDocument();

    final Element feedElem = doc.createElement(ODataConstants.ATOM_ELEM_FEED);
    feedElem.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, ODataConstants.NS_ATOM);
    feedElem.setAttribute(ODataConstants.XMLNS_METADATA, ODataConstants.NS_METADATA);
    feedElem.setAttribute(ODataConstants.XMLNS_DATASERVICES, ODataConstants.NS_DATASERVICES);
    feedElem.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
    feedElem.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
    if (feed.getBaseURI() != null) {
        feedElem.setAttribute(ODataConstants.ATTR_XMLBASE, feed.getBaseURI().toASCIIString());
    }/*from   w  w  w .  j a v a2  s.c  o  m*/
    doc.appendChild(feedElem);

    if (StringUtils.isNotBlank(feed.getTitle())) {
        final Element title = doc.createElement(ODataConstants.ATOM_ELEM_TITLE);
        title.appendChild(doc.createTextNode(feed.getTitle()));
        feedElem.appendChild(title);
    }

    if (StringUtils.isNotBlank(feed.getSummary())) {
        final Element summary = doc.createElement(ODataConstants.ATOM_ELEM_SUMMARY);
        summary.appendChild(doc.createTextNode(feed.getSummary()));
        feedElem.appendChild(summary);
    }

    for (AtomEntry entry : feed.getEntries()) {
        feedElem.appendChild(doc.importNode(entry(entry), true));
    }

    return feedElem;
}

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

/**
 * make content file/*ww  w  .ja  v  a2s . c  o  m*/
 * 
 * @param tableName
 * @param rsDAO
 * @return
 * @throws Exception
 */
public static String makeContentFile(String tableName, QueryExecuteResultDTO rsDAO) throws Exception {
    String strTmpDir = PublicTadpoleDefine.TEMP_DIR + tableName + System.currentTimeMillis()
            + PublicTadpoleDefine.DIR_SEPARATOR;
    String strFile = tableName + ".xml";
    String strFullPath = strTmpDir + strFile;

    final StringWriter stWriter = new StringWriter();
    final List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.newDocument();
    final Element results = doc.createElement(tableName);
    doc.appendChild(results);

    Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();
    for (int i = 0; i < dataList.size(); i++) {
        Map<Integer, Object> mapColumns = dataList.get(i);
        Element row = doc.createElement("Row");
        results.appendChild(row);

        for (int j = 1; j < mapColumns.size(); j++) {
            String columnName = mapLabelName.get(j);
            String strValue = mapColumns.get(j) == null ? "" : "" + mapColumns.get(j);

            Element node = doc.createElement(columnName);
            node.appendChild(doc.createTextNode(strValue));
            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");//"ISO-8859-1");
    StreamResult sr = new StreamResult(stWriter);
    transformer.transform(domSource, sr);

    FileUtils.writeStringToFile(new File(strFullPath), stWriter.toString(), true);

    return strFullPath;
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static Element arr(Document doc, String attname, List vals) {
    synchronized (doc) {
        Element arrElm = doc.createElement("arr");
        if (attname != null)
            arrElm.setAttribute("name", attname);
        for (Object obj : vals) {
            if (obj instanceof String) {
                arrElm.appendChild(value(doc, (String) obj));
            } else if (obj instanceof Integer) {
                arrElm.appendChild(value(doc, (Integer) obj));
            } else
                throw new IllegalArgumentException("unsupported type " + obj.getClass().getName() + "");
        }/*from  w w w. j ava  2 s . com*/
        return arrElm;
    }
}