Example usage for org.dom4j Element addElement

List of usage examples for org.dom4j Element addElement

Introduction

In this page you can find the example usage for org.dom4j Element addElement.

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

/**
 * back successful information/*  ww  w .j  a  va2s .c om*/
 *
 * @return
 */
public static String resultMessage() {
    Document document = DocumentHelper.createDocument();
    Element payload = DocumentHelper.createElement("payload");
    document.setRootElement(payload);
    Element response = payload.addElement("response");
    Element result = response.addElement("result");
    result.setText("true");
    Element resultText = response.addElement("resultText");
    resultText.setText("??");
    Element userId = response.addElement("userId");
    userId.setText("0001");
    return document.asXML();
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

/**
 * @param params ArrayList//from   w w  w . ja  v a  2 s.co m
 * @return String
 */
public static String createResultMessage(List<Map<String, Object>> params) throws Exception {
    Document document;
    if (params != null && params.size() > 0) {
        document = DocumentHelper.createDocument();
        Element root = document.addElement("payload");
        Element response = root.addElement("response");
        Element items = response.addElement("items");
        for (Map<String, Object> param : params) {
            Element item = items.addElement("item");
            param.keySet().forEach(key -> {
                Element element = item.addElement(key);
                element.setText(String.valueOf(param.get(key)));
            });
        }
        Element userId = response.addElement("user_id");
        userId.setText("0001");
    } else {
        document = DocumentHelper.createDocument();
        Element root = document.addElement("payload");
        Element response = root.addElement("response");
        Element userId = response.addElement("user_id");
        userId.setText("0001");
    }
    return document.asXML();
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

/**
 * @param params HashMap//  ww w.j av  a  2s . com
 * @return String
 */
public static String createResultMessage(Map<String, Object> params) throws Exception {
    Document document;
    if (params != null) {
        document = DocumentHelper.createDocument();
        Element root = document.addElement("payload");
        Element response = root.addElement("response");
        params.keySet().forEach(key -> {
            Element element = response.addElement(key);
            element.setText(String.valueOf(params.get(key)));
        });
        Element userId = response.addElement("user_id");
        userId.setText("0001");
    } else {
        document = DocumentHelper.createDocument();
        Element root = document.addElement("payload");
        Element response = root.addElement("response");
        Element userId = response.addElement("user_id");
        userId.setText("0001");
    }
    return document.asXML();
}

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * ?/*from   ww  w . j ava 2 s.c  om*/
 *
 * @param parentelement????
 * @param elementName????
 * @return Element
 */
public Element addElement(Element parentelement, String elementName) {
    Element element = parentelement.addElement(elementName);
    return element;
}

From source file:com.ibm.cognos.ReportObject.java

License:Open Source License

public Document getPackages(CRNConnect connection, String sPath) {
    Document oDom = null;/*from   w  ww.java2 s .  c o m*/
    Element packagesElement;
    try {
        com.cognos.developer.schemas.bibus._3.BaseClass oBase[];
        oBase = connection.getCMService()
                .query(new SearchPathMultipleObject(sPath), new PropEnum[] { PropEnum.defaultName,
                        PropEnum.source, PropEnum.dispatcherPath, PropEnum.searchPath }, new Sort[] {},
                        new QueryOptions());
        packagesElement = DocumentHelper.createElement("packages");
        oDom = DocumentHelper.createDocument(packagesElement);
        for (int i = 0; i < oBase.length; i++) {
            if (oBase[i].getClass().getName().toString()
                    .equals("com.cognos.developer.schemas.bibus._3._package")) {
                Element oElement = DocumentHelper.createElement("package");
                String oBaseName = oBase[i].getDefaultName().getValue();
                String oBaseSearchPath = oBase[i].getSearchPath().getValue();
                oElement.addElement("name").setText(oBaseName);
                oElement.addElement("searchPath").setText(oBaseSearchPath);
                oDom.getRootElement().add(oElement);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return oDom;
}

From source file:com.ikon.servlet.admin.StatsGraphServlet.java

License:Open Source License

/**
 * Convert a piechartdata to xml//w w  w . j  a  v a 2 s  .c o m
 * 
 * @author puspendu.banerjee@gmail.com 
 */
public String repoStatsXML(final String title, final DefaultPieDataset dataset)
        throws IOException, ServletException {
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("RepoStats");
    root.addElement("Title").addCDATA(title);
    Element dataSetElement = root.addElement("DataSet");

    for (int i = 0; i < dataset.getItemCount(); i++) {
        Element itemElement = dataSetElement.addElement("Item");
        itemElement.addElement("name").addCDATA(dataset.getKey(i).toString());
        itemElement.addAttribute("percent", dataset.getValue(i).toString());
        dataSetElement.add(itemElement);
    }

    return document.asXML();
}

From source file:com.jaspersoft.jasperserver.export.modules.logging.access.AccessEventsExporter.java

License:Open Source License

protected void addAccessEventIndexEntry(long counter) {
    Element indexElement = getIndexElement();
    Element tenantElement = indexElement.addElement(accessModuleConfiguration.getAccessEventIndexElement());
    tenantElement.addText(String.valueOf(counter));
}

From source file:com.jaspersoft.jasperserver.export.modules.mt.TenantExporter.java

License:Open Source License

protected void addTenantIndexEntry(Tenant tenant) {
    Element indexElement = getIndexElement();
    Element tenantElement = indexElement.addElement(moduleConfiguration.getTenantIndexElement());
    tenantElement.addText(tenant.getId());
}

From source file:com.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Sets a property to an array of values. Multiple values matching the same property
 * is mapped to an XML file as multiple elements containing each value.
 * For example, using the name "foo.bar.prop", and the value string array containing
 * {"some value", "other value", "last value"} would produce the following XML:
 * <pre>/* w  w w . j  a  va2  s . c  o  m*/
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name   the name of the property.
 * @param values the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values, boolean isEncrypt) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML hierarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator<Element> iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add(iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator<Node> it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            String propValue = StringEscapeUtils.escapeXml(value);
            // check to see if the property is marked as encrypted
            if (isEncrypt) {
                propValue = EncryptUtil.desEncrypt(encryptKey, value);
                childElement.addAttribute(ENCRYPTED_ATTRIBUTE, "true");
            }
            childElement.setText(propValue);
        }
    }
    saveProperties();

}

From source file:com.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Sets the value of the specified property. If the property doesn't
 * currently exist, it will be automatically created.
 *
 * @param name      the name of the property to set.
 * @param value     the new value for the property.
 * @param isEncrypt ?/*  w ww.j  ava 2  s.  c  o m*/
 */
public synchronized void setProperty(String name, String value, boolean isEncrypt) {
    if (!StringEscapeUtils.escapeXml(name).equals(name)) {
        throw new IllegalArgumentException("Property name cannot contain XML entities.");
    }
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML hierarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        String propValue = StringEscapeUtils.escapeXml(value);
        // check to see if the property is marked as encrypted
        if (isEncrypt) {
            propValue = EncryptUtil.desEncrypt(encryptKey, value);
            element.addAttribute(ENCRYPTED_ATTRIBUTE, "true");
        }
        element.setText(propValue);
    }
    // Write the XML properties to disk
    saveProperties();

}