Example usage for org.dom4j Document addElement

List of usage examples for org.dom4j Document addElement

Introduction

In this page you can find the example usage for org.dom4j Document 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.service.Summoner.java

License:Open Source License

/**
 * create list xml/*from w  ww . j a va 2s  .  c om*/
 *
 * @param params
 * @param listElement
 * @return
 */
public String listCreateXml(List<Map<String, Object>> params, String listElement) {
    Document document = null;
    try {
        if (params != null && params.size() > 0) {
            document = DocumentHelper.createDocument();
            Element root = document.addElement("payload");
            Element response = root.addElement("response");
            for (Map<String, Object> param : params) {
                Element dept = response.addElement(listElement);
                param.keySet().stream().forEach(key -> {
                    Element element = dept.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");
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);
    }
    return document != null ? document.asXML() : null;
}

From source file:com.heren.turtle.server.service.Summoner.java

License:Open Source License

/**
 * create normal xml/*w  w  w  .  j a va2s.  com*/
 *
 * @param params
 * @return
 */
public String createXml(Map<String, Object> params) {
    Document document = null;
    try {
        document = DocumentHelper.createDocument();
        Element root = document.addElement("payload");
        Element response = root.addElement("response");
        params.keySet().stream().forEach(key -> {
            Element element = response.addElement(key);
            element.setText(String.valueOf(params.get(key)));
        });
        Element userId = response.addElement("user_id");
        userId.setText("0001");
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);
    }
    return document != null ? document.asXML() : null;
}

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 . c  o  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//w  w  w.j a  va 2  s  .  c o  m
 * @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

/**
 * XML// w  w  w  .j a v a  2s  .  co  m
 *
 * @param document
 *            XML
 * @param rootName???
 * @return element?
 */
public Element createRootElement(Document document, String rootName) {
    Element element = document.addElement(rootName);
    return element;

}

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

License:Open Source License

/**
 * Convert a piechartdata to xml//from   www.j  a  v  a2  s.  co 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.ExporterImpl.java

License:Open Source License

protected void process() {
    Document indexDocument = DocumentHelper.createDocument();
    indexRootElement = indexDocument.addElement(getIndexRootElementName());

    setOutputProperties();//from   w w w  .j ava2 s . c  om
    invokeModules();

    writeIndexDocument(indexDocument);
}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * //  w ww  . j av a  2 s  . co  m
 * @param document
 */
private Element generateRoot(Document document) {
    Element root = document.addElement(ROOT_ELEMENT_NAME);
    root.addAttribute("namespace", daoClazz.getName());
    return root;
}

From source file:com.laudandjolynn.mytv.MyTvData.java

License:Apache License

public void writeData(String parent, String tag, String value) {
    logger.debug("write data to my tv data file: " + Constant.MY_TV_DATA_FILE_PATH);
    File file = new File(Constant.MY_TV_DATA_FILE_PATH);
    if (!file.exists()) {
        Document doc = DocumentHelper.createDocument();
        doc.addElement(Constant.APP_NAME);
        try {/* www  .j  a  v a 2s .  c  o m*/
            FileUtils.writeWithNIO(doc.asXML().getBytes(), Constant.MY_TV_DATA_FILE_PATH);
        } catch (IOException e) {
            throw new MyTvException(
                    "error occur while write data to file. -- " + Constant.MY_TV_DATA_FILE_PATH);
        }
    }
    SAXReader reader = new SAXReader();
    try {
        Document xmlDoc = reader.read(file);
        Element parentElement = xmlDoc.getRootElement();
        if (parent != null) {
            List<?> nodes = xmlDoc.selectNodes("//" + parent);
            if (nodes != null && nodes.size() > 0) {
                parentElement = (Element) nodes.get(0);
            }
        }
        parentElement.addElement(tag).setText(value);
        try {
            XMLWriter writer = new XMLWriter(new FileWriter(file));
            writer.write(xmlDoc);
            writer.close();
        } catch (IOException e) {
            throw new MyTvException(
                    "error occur while write data to file. -- " + Constant.MY_TV_DATA_FILE_PATH);
        }
    } catch (DocumentException e) {
        String msg = "can't parse xml file. -- " + Constant.MY_TV_DATA_FILE_PATH;
        throw new MyTvException(msg);
    }
}

From source file:com.liferay.alloy.tools.transformer.AlloyDocsTransformer.java

License:Open Source License

private void _createXML() {
    ArrayList<Component> components = getComponents();

    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("components");

    root.addAttribute("short-name", _DEFAULT_TAGLIB_SHORT_NAME);
    root.addAttribute("uri", _DEFAULT_TAGLIB_URI);
    root.addAttribute("tlib-version", _DEFAULT_TAGLIB_VERSION);

    for (Component component : components) {
        Element componentNode = root.addElement("component");

        componentNode.addAttribute("name", component.getName());
        componentNode.addAttribute("module", component.getModule());
        componentNode.addAttribute("package", component.getPackage());
        componentNode.addAttribute("bodyContent", String.valueOf(component.isBodyContent()));
        componentNode.addAttribute("alloyComponent", String.valueOf(component.isAlloyComponent()));

        Element descriptionNode = componentNode.addElement("description");
        descriptionNode.addCDATA(component.getDescription());
        Element attributesNode = componentNode.addElement("attributes");
        Element eventsNode = componentNode.addElement("events");

        for (Attribute attribute : component.getAttributes()) {
            Element attributeNode = attributesNode.addElement("attribute");

            Element defaultValueNode = attributeNode.addElement("defaultValue");
            Element attributeDescriptionNode = attributeNode.addElement("description");
            Element javaScriptTypeNode = attributeNode.addElement("javaScriptType");
            Element nameNode = attributeNode.addElement("name");
            Element readOnlyNode = attributeNode.addElement("readOnly");

            defaultValueNode.setText(attribute.getDefaultValue());
            attributeDescriptionNode.addCDATA(_getAttributeDescription(attribute));
            javaScriptTypeNode.setText(attribute.getJavaScriptType());
            nameNode.setText(attribute.getName());
            readOnlyNode.setText(Boolean.toString(attribute.isReadOnly()));
        }//from w ww  . jav a 2 s.  com

        for (Attribute event : component.getEvents()) {
            Element eventNode = eventsNode.addElement("event");
            Element nameNode = eventNode.addElement("name");
            Element typeNode = eventNode.addElement("type");
            Element elementDescriptionNode = eventNode.addElement("description");

            nameNode.setText(event.getName());
            elementDescriptionNode.addCDATA(_getAttributeDescription(event));
            typeNode.setText(event.getType());
        }
    }

    try {
        File file = new File(_outputXML);

        file.getParentFile().mkdirs();

        FileOutputStream fos = new FileOutputStream(file);

        OutputFormat format = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(fos, format);

        writer.write(doc);
        writer.flush();

        System.out.println("Writing " + _outputXML);
    } catch (IOException e) {
        e.printStackTrace();
    }
}