Example usage for org.dom4j DocumentHelper parseText

List of usage examples for org.dom4j DocumentHelper parseText

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper parseText.

Prototype

public static Document parseText(String text) throws DocumentException 

Source Link

Document

parseText parses the given text as an XML document and returns the newly created Document.

Usage

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

License:Open Source License

public static Map<String, Object> getMessageReceiveSample(String message) throws DocumentException {
    message = replaceWrongPart(message);
    Document document = DocumentHelper.parseText(message);
    Element rootElement = document.getRootElement();
    Element request = rootElement.element("request");
    List elements = request.elements();
    Map<String, Object> result = new HashMap<>();
    for (Object element : elements) {
        Element subElement = (Element) element;
        String name = subElement.getName();
        if (name.equalsIgnoreCase("item")) {
            List subElements = subElement.elements();
            for (Object subEle : subElements) {
                Element grantSubElement = (Element) subEle;
                result.put(grantSubElement.getName(), grantSubElement.getTextTrim());
            }//from w  w  w.  ja v a2 s .com
        } else if (subElement.isTextOnly()) {
            result.put(name, subElement.getTextTrim());
        }
    }
    return result;
}

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

License:Open Source License

public static List<Map<String, Object>> getMessageReceiveSampleContainItems(String message)
        throws DocumentException {
    message = replaceWrongPart(message);
    Document document = DocumentHelper.parseText(message);
    Element rootElement = document.getRootElement();
    Element request = rootElement.element("request");
    Element items = request.element("items");
    List elements = items.elements();
    List<Map<String, Object>> resultList = new ArrayList<>();
    for (Object element : elements) {
        Map<String, Object> result = new HashMap<>();
        Element subElement = (Element) element;
        String name = subElement.getName();
        if (name.equalsIgnoreCase("item")) {
            List subElements = subElement.elements();
            for (Object subEle : subElements) {
                Element grantSubElement = (Element) subEle;
                result.put(grantSubElement.getName(), grantSubElement.getTextTrim());
            }/*from ww w.j  av  a  2  s.c om*/
        } else if (subElement.isTextOnly()) {
            result.put(name, subElement.getTextTrim());
        }
        resultList.add(result);
    }
    return resultList;
}

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

License:Open Source License

/**
 * delete the label of xml that is not use
 *
 * @param message/*from ww  w.j  a  v  a2s  .co m*/
 * @return
 */
public static Map<String, Object> getMessageContainsMap(String message) throws DocumentException {
    message = replaceWrongPart(message);
    Document document = DocumentHelper.parseText(message);
    Element rootElement = document.getRootElement();
    Element request = rootElement.element("request");
    List elements = request.elements();
    Map<String, Object> result = new HashMap<>();
    for (Object element : elements) {
        Element subElement = (Element) element;
        if (subElement.isTextOnly()) {
            result.put(subElement.getName(), subElement.getTextTrim());
        } else {
            List<Map<String, String>> subList = new ArrayList<>();
            List subEle = subElement.elements();
            for (Object aSubEle : subEle) {
                Element itemElements = (Element) aSubEle;
                Map<String, String> subMap = new HashMap<>();
                List grandSubElements = itemElements.elements();
                for (Object grandSubElement : grandSubElements) {
                    Element grandItemElements = (Element) grandSubElement;
                    subMap.put(grandItemElements.getName().trim(), grandItemElements.getTextTrim());
                }
                subList.add(subMap);
            }
            result.put(subElement.getName(), subList);
        }
    }
    return result;
}

From source file:com.hwsoft.util.idcard.IDCardVerifyTools.java

/**
 * ?Nciicl?/*from   w w  w.j  av a  2 s  .  c  o  m*/
 *
 * @param backXMl?
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static IdCardInfo parseXml(String backXMl, String userName, String idCard) {
    Document doc = null;
    String result_gmsfhm = null;
    String result_xm = null;
    String gmsfhm = null;
    String xm = null;
    String xp = null;
    String errormesage = null;
    IdCardInfo IdCardInfo = null;
    System.out.println("idcard bind Nciic  backInfo:" + backXMl);

    try {
        //XML
        doc = DocumentHelper.parseText(backXMl);

        // ?
        Element rootElt = doc.getRootElement();

        if ("RESPONSE".equalsIgnoreCase(rootElt.getName())) {
            //???
            List<DefaultAttribute> list = rootElt.attributes();

            for (DefaultAttribute e : list) {
                if ("errorcode".equals(e.getName())) {
                    if ("-70".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("?", null);
                    } else if ("-71".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("?", null);
                    } else if ("-72".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("IP ???", null);
                    } else if ("-80".equalsIgnoreCase(e.getValue())) {
                        throw new RuntimeException("??", null);
                    }
                }
            }
        } else if ("ROWS".equalsIgnoreCase(rootElt.getName())) {
            Iterator iterForRow = rootElt.elementIterator("ROW");

            while (iterForRow.hasNext()) {
                Element recordEleForRow = (Element) iterForRow.next();

                //?????
                Iterator itersForInput = recordEleForRow.elementIterator("INPUT");

                while (itersForInput.hasNext()) {
                    Element recordEleForInput = (Element) itersForInput.next();
                    gmsfhm = recordEleForInput.elementTextTrim("gmsfhm");

                    if (!(idCard.equalsIgnoreCase(gmsfhm))) {
                        throw new RuntimeException("?????", null);
                    }

                    xm = recordEleForInput.elementTextTrim("xm");

                    if (!(userName.equalsIgnoreCase(xm))) {
                        throw new RuntimeException("?????", null);
                    }
                }

                //??????
                Iterator itersForOutput = recordEleForRow.elementIterator("OUTPUT");

                while (itersForOutput.hasNext()) {
                    Element recordEleForSubRow = (Element) itersForOutput.next();
                    Iterator itersForItem = recordEleForSubRow.elementIterator("ITEM");

                    while (itersForItem.hasNext()) {
                        Element recordEleForItem = (Element) itersForItem.next();
                        gmsfhm = recordEleForItem.elementTextTrim("gmsfhm");
                        xm = recordEleForItem.elementTextTrim("xm");
                        xp = recordEleForItem.elementTextTrim("xp");
                        errormesage = recordEleForItem.elementTextTrim("errormesage");

                        if (gmsfhm != null) {
                            result_gmsfhm = recordEleForItem.elementTextTrim("result_gmsfhm");
                        }

                        if (xm != null) {
                            result_xm = recordEleForItem.elementTextTrim("result_xm");
                        }

                        if (xp != null) {
                            xp = recordEleForItem.elementTextTrim("xp");
                        }

                        if (errormesage != null) {
                            errormesage = recordEleForItem.elementTextTrim("errormesage");
                            break;
                        }
                    }
                }

                if (null != errormesage) {
                    throw new RuntimeException("??!", null);
                } else {
                    if (!"".equalsIgnoreCase(result_xm)) {
                        throw new RuntimeException("?????", null);
                    }

                    if (!"".equalsIgnoreCase(result_gmsfhm)) {
                        throw new RuntimeException("?????", null);
                    }

                    IdCardInfo = new IdCardInfo();
                    IdCardInfo.setIdCardNumber(idCard);
                    IdCardInfo.setRealName(userName);
                    IdCardInfo.setAvatar(xp);
                }
            }
        }
    } catch (DocumentException e) {
        System.out.println("idcard bind erroinfo:XML??");
    } catch (Exception e) {
        System.out.println("idcard bind erroinfo:XML??");
    }
    return IdCardInfo;
}

From source file:com.iisigroup.cap.hg.bean.DoNothingHGTxnBean.java

License:Open Source License

@Override
public void setSendData(Object requestData) {

    if (requestData instanceof String && !CapString.isEmpty((String) requestData)) {
        try {/*w w w  . j  av  a  2 s.c om*/
            this.sendData = DocumentHelper.parseText((String) requestData);
        } catch (DocumentException e) {
            logger.debug(e.getMessage(), e);
        }
    }

}

From source file:com.iisigroup.cap.utils.CapXmlUtil.java

License:Open Source License

/**
 * XML?JSON//from  w  w w  .  j  a  va2  s .  c  om
 * 
 * @param xmlString
 *            xmlString
 * @return jsonObject
 */
public static JSONObject convertXmlStringToJson(String xmlString) {
    try {
        Document document = DocumentHelper.parseText(xmlString);
        Element root = document.getRootElement();
        return travelXML(root);
    } catch (DocumentException e) {
        throw new CapMessageException(e, CapXmlUtil.class);
    }
}

From source file:com.iisigroup.cap.utils.CapXmlUtil.java

License:Open Source License

/**
 *  XML Document/*from w ww  . j  a  v  a2 s . c om*/
 * 
 * @param xmlString
 *            xmlString
 * @return Document
 * @throws CapException
 */
public static Document convertXMLStringToDocument(String xmlString) {
    try {
        return DocumentHelper.parseText(xmlString);
    } catch (DocumentException e) {
        throw new CapMessageException(e, CapXmlUtil.class);
    }
}

From source file:com.intuit.tank.script.ScriptEditor.java

License:Open Source License

public String getPrettyString(String s, String mimetype) {
    if (StringUtils.isNotBlank(s)) {
        if (StringUtils.containsIgnoreCase(mimetype, "json")) {
            try {
                JsonParser parser = new JsonParser();
                Gson gson = new GsonBuilder().setPrettyPrinting().create();
                JsonElement el = parser.parse(s);
                s = gson.toJson(el); // done
            } catch (JsonSyntaxException e) {
                LOG.warn("Cannot format json string: " + e);
            }//from  ww  w  .  j a v  a 2s .co m
        } else if (StringUtils.containsIgnoreCase(mimetype, "xml")) {
            try {
                StringWriter sw;
                final OutputFormat format = OutputFormat.createPrettyPrint();
                final org.dom4j.Document document = DocumentHelper.parseText(s);
                sw = new StringWriter();
                final XMLWriter writer = new XMLWriter(sw, format);
                writer.write(document);
                s = sw.toString();
            } catch (Exception e) {
                LOG.warn("Cannot format xml string: " + e);
            }
        }
        s = s.trim();
    }
    return s;
}

From source file:com.jfaker.framework.flow.web.ProcessController.java

License:Apache License

/**
 * ???//  w  ww.j a v  a  2  s  .  com
 */
public void getProcessTaskInfo() {
    Process process = engine.process().getProcessById(getPara(PARA_PROCESSID));
    //Process process = engine.process().getProcessById("23e85c95caf74a9eaef8cc9df9594448");
    String flowId = this.getPara("flowId");
    String flowName = this.getPara("flowName");
    setAttr("process", process);
    if (process.getDBContent() != null) {
        try {
            String content = new String(process.getDBContent(), "UTF-8");
            System.out.println(new String(process.getDBContent(), "UTF-8"));
            Document document = DocumentHelper.parseText(content);
            Element rootEle = document.getRootElement();
            List taskNodes = rootEle.elements("task");
            List<Object> list = new ArrayList<Object>();
            Map map = null;
            for (Iterator it = taskNodes.iterator(); it.hasNext();) {
                map = new HashMap();
                Element elm = (Element) it.next();
                map.put("processId", process.getId());
                map.put("processName", process.getName());
                map.put("SUB_FLOW_ID", flowId);
                map.put("FLOW_NAME", flowName);
                map.put("tacheCode", elm.attribute("name").getText());
                map.put("tachName", elm.attribute("displayName").getText());
                list.add(map);

            }
            setAttr("tacheList", list);
            renderJson(list);
            //renderJson(JSONArray.fromObject(list).toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.jfinal.ext.weixin.msg.InMsgParaser.java

License:Apache License

/**
 * ?/*from  ww  w .j a  va 2  s  . c  o m*/
 * 1text ?
 * 2image ?
 * 3voice ?
 * 4video ?
 * 5location ???
 * 6link ?
 * 7event 
 */
private static InMsg doParse(String xml) throws DocumentException {
    Document doc = DocumentHelper.parseText(xml);
    Element root = doc.getRootElement();
    String toUserName = root.elementText("ToUserName");
    String fromUserName = root.elementText("FromUserName");
    Integer createTime = Integer.parseInt(root.elementText("CreateTime"));
    String msgType = root.elementText("MsgType");
    if ("text".equals(msgType))
        return parseInTextMsg(root, toUserName, fromUserName, createTime, msgType);
    if ("image".equals(msgType))
        return parseInImageMsg(root, toUserName, fromUserName, createTime, msgType);
    if ("voice".equals(msgType))
        return parseInVoiceMsg(root, toUserName, fromUserName, createTime, msgType);
    if ("video".equals(msgType))
        return parseInVideoMsg(root, toUserName, fromUserName, createTime, msgType);
    if ("location".equals(msgType))
        return parseInLocationMsg(root, toUserName, fromUserName, createTime, msgType);
    if ("link".equals(msgType))
        return parseInLinkMsg(root, toUserName, fromUserName, createTime, msgType);
    if ("event".equals(msgType))
        return parseInEvent(root, toUserName, fromUserName, createTime, msgType);
    throw new RuntimeException("???");
}