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.haulmont.yarg.structure.xml.impl.DefaultXmlWriter.java

License:Apache License

protected void writeTemplates(Report report, Element root) {
    Map<String, ReportTemplate> reportTemplates = report.getReportTemplates();
    Element reportTemplatesElement = root.addElement("templates");
    for (ReportTemplate reportTemplate : reportTemplates.values()) {
        Element reportTemplateElement = reportTemplatesElement.addElement("template");
        reportTemplateElement.addAttribute("code", reportTemplate.getCode());
        reportTemplateElement.addAttribute("documentName", reportTemplate.getDocumentName());
        reportTemplateElement.addAttribute("documentPath", reportTemplate.getDocumentPath());
        reportTemplateElement.addAttribute("outputType", reportTemplate.getOutputType().getId());
        reportTemplateElement.addAttribute("outputNamePattern", reportTemplate.getOutputNamePattern());
    }//from  w w w  .j  a va  2 s . c o  m
}

From source file:com.haulmont.yarg.structure.xml.impl.DefaultXmlWriter.java

License:Apache License

protected void writeBandDefinition(Element element, ReportBand bandDefinition) {
    element.addAttribute("name", bandDefinition.getName());
    element.addAttribute("orientation", bandDefinition.getBandOrientation().id);
    Element childrenBandsElement = element.addElement("bands");

    Element reportQueriesElement = element.addElement("queries");
    if (bandDefinition.getReportQueries() != null) {
        for (ReportQuery reportQuery : bandDefinition.getReportQueries()) {
            Element reportQueryElement = reportQueriesElement.addElement("query");
            reportQueryElement.addAttribute("name", reportQuery.getName());
            reportQueryElement.addAttribute("type", reportQuery.getLoaderType());
            reportQueryElement.addElement("script").setText(reportQuery.getScript());
        }/*from  w w  w . ja v  a  2s.c  om*/
    }

    if (bandDefinition.getChildren() != null) {
        for (ReportBand childBandDefinition : bandDefinition.getChildren()) {
            Element childBandElement = childrenBandsElement.addElement("band");
            writeBandDefinition(childBandElement, childBandDefinition);
        }
    }
}

From source file:com.hazzard.gui.PiNet.java

public void sendCommand(String command, String data) throws Exception {
    Document document;//w  w  w  .j a  v a  2  s . c o  m
    document = DocumentHelper.createDocument();
    Element root = document.addElement("commands");
    Element commandElem = root.addElement("command");
    commandElem.setText(command);

    commandElem.addAttribute("data", data);

    final Writer writer = new StringWriter();
    new XMLWriter(writer).write(root);
    String text = writer.toString().trim();

    DatagramPacket pack = new DatagramPacket(text.getBytes(), text.length(), InetAddress.getByName(group),
            port);
    sock_send.send(pack);
}

From source file:com.heren.turtle.entry.util.LoadUtils.java

License:Open Source License

public static String resultMessage(String receiveMessage, String resultMessageCode, String resultMessage) {
    switch (ModeUtils.whichMode(receiveMessage)) {
    case ModeUtils.TYPE_JSON:
        Map<String, Object> message = new HashMap<>();
        Map<String, Object> headers = new HashMap<>();
        Map<String, Object> payload = new HashMap<>();
        Map<String, Object> errorInfo = new HashMap<>();
        errorInfo.put(GlobalConstant.ERROR_CODE, resultMessageCode);
        errorInfo.put(GlobalConstant.ERROR_MESSAGE, resultMessage);
        message.put(GlobalConstant.HEAD, headers);
        message.put(GlobalConstant.PAYLOAD, payload);
        message.put(GlobalConstant.ERROR_INFO, errorInfo);
        String response = JSON.toJSONString(message);
        receiveMessage = response;//  www  . j av a2 s  . c om
        break;
    case ModeUtils.TYPE_XML:
        Document document = DocumentHelper.createDocument();
        Element messageEl = document.addElement("Message");
        Element headersEl = messageEl.addElement(GlobalConstant.HEAD);
        Element payloadEl = messageEl.addElement(GlobalConstant.PAYLOAD);
        Element errorInfoEl = messageEl.addElement(GlobalConstant.ERROR_INFO);
        Element errorCodeEl = errorInfoEl.addElement(GlobalConstant.ERROR_CODE);
        errorCodeEl.setText(resultMessageCode);
        Element errorMessageEl = errorInfoEl.addElement(GlobalConstant.ERROR_MESSAGE);
        errorMessageEl.setText(resultMessage);
        receiveMessage = document.asXML();
        break;
    case ModeUtils.TYPE_UN_DEFINITION:
        break;
    default:
        break;
    }
    return receiveMessage;
}

From source file:com.heren.turtle.gear.meta.AbstractService.java

License:Open Source License

protected String createXml(List<Map<String, Object>> paramsList, String resultCode, String errorMsg,
        String itemName) {//from ww  w .ja v a2s. c  o m
    Document document = null;
    try {
        document = DocumentHelper.createDocument();
        Element response = document.addElement("response");
        for (Map<String, Object> map : paramsList) {
            Element itemEle = response.addElement(itemName);
            for (String key : map.keySet()) {
                Element element = itemEle.addElement(key);
                element.setText(String.valueOf(map.get(key)));
            }
        }
        Element resultCodeEle = response.addElement("resultCode");
        resultCodeEle.setText(resultCode);
        Element errorMsgEle = response.addElement("errorMsg");
        errorMsgEle.setText(errorMsg);
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);
    }
    return document != null ? document.asXML().replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "") : null;
}

From source file:com.heren.turtle.gear.meta.AbstractService.java

License:Open Source License

protected String createExamXml(List<Map<String, Object>> paramsList, String resultCode, String errorMsg,
        String itemName) {/*from   w  w  w  . j ava  2s.  c  o  m*/
    Document document = null;
    try {
        document = DocumentHelper.createDocument();
        Element response = document.addElement("response");
        for (Map<String, Object> map : paramsList) {
            Element itemEle = response.addElement(itemName);
            for (String key : map.keySet()) {
                if (key.equals("1")) {
                    Element elecList = itemEle.addElement("elecList");
                    Element elecItem = elecList.addElement("elecItem");
                    Element element = elecItem.addElement(key);
                    element.setText(String.valueOf(map.get(key)));
                } else {
                    Element ucgList = itemEle.addElement("ucgList");
                    Element ucgItem = ucgList.addElement("ucgItem");
                    Element element = ucgItem.addElement(key);
                    element.setText(String.valueOf(map.get(key)));
                }
            }
        }
        Element resultCodeEle = response.addElement("resultCode");
        resultCodeEle.setText(resultCode);
        Element errorMsgEle = response.addElement("errorMsg");
        errorMsgEle.setText(errorMsg);
    } 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 list xml//from   w w  w  .  ja v a2  s.  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/*from  w ww  .  ja v a 2s.c o  m*/
 *
 * @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

/**
 * back failure information//from w  w w.ja va  2 s .  c  om
 *
 * @param message
 * @return
 */
public static String errorMessage(String message) {
    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(message == null ? "" : message);
    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

/**
 * back correct information/*from  ww  w . j  a v  a2  s  .co m*/
 *
 * @param flag 0 no data 1 some exception
 * @return
 */
public static String correctMessage(int flag) {
    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");
    switch (flag) {
    case 0:
        resultText.setText("no data found!");
        break;
    case -1:
        resultText.setText("success!");
        break;
    default:
        break;
    }
    Element userId = response.addElement("userId");
    userId.setText("0001");
    return document.asXML();
}