List of usage examples for org.dom4j DocumentHelper parseText
public static Document parseText(String text) throws DocumentException
parseText
parses the given text as an XML document and returns the newly created Document.
From source file:com.jfinal.ext.weixin.msg.InMsgParaser.java
License:Apache License
@SuppressWarnings("unused") public static void main(String[] args) throws DocumentException { String xml = "<xml>\n" + "<ToUserName><![CDATA[James]]></ToUserName>\n" + "<FromUserName><![CDATA[JFinal]]></FromUserName>\n" + "<CreateTime>1348831860</CreateTime>\n" + "<MsgType><![CDATA[text]]></MsgType>\n" + "<Content><![CDATA[this is a test]]></Content>\n" + "<MsgId>1234567890123456</MsgId>\n" + "</xml>"; // InTextMsg msg = (InTextMsg)parse(xml); // System.out.println(msg.getToUserName()); // System.out.println(msg.getFromUserName()); // System.out.println(msg.getContent()); String xml_2 = "<xml>\n" + "<ToUserName><![CDATA[James]]></ToUserName>\n" + "<FromUserName><![CDATA[JFinal]]></FromUserName>\n" + "<CreateTime>1348831860</CreateTime>\n" + "<MsgType><![CDATA[text]]></MsgType>\n" + "<Content><![CDATA[this is a test]]></Content>\n" + "<MsgId>1234567890123456</MsgId>\n" + "</xml>"; Document doc = DocumentHelper.parseText(xml_2); Element root = doc.getRootElement(); String value = root.elementText("abc"); System.out.println(value);//from w ww . j a v a 2s . c o m }
From source file:com.jfinal.weixin.sdk.message.InMessageParaser.java
License:Apache License
/** * ?//from www . jav a 2 s . c o m * 1text ? * 2image ? * 3voice ? * 4video ? * 5location ??? * 6link ? * 7event */ private static InMessage 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 parseInTextMessage(root, toUserName, fromUserName, createTime, msgType); if ("image".equals(msgType)) return parseInImageMessage(root, toUserName, fromUserName, createTime, msgType); if ("voice".equals(msgType)) return parseInVoiceMessage(root, toUserName, fromUserName, createTime, msgType); if ("video".equals(msgType)) return parseInVideoMessage(root, toUserName, fromUserName, createTime, msgType); if ("location".equals(msgType)) return parseInLocationMessage(root, toUserName, fromUserName, createTime, msgType); if ("link".equals(msgType)) return parseInLinkMessage(root, toUserName, fromUserName, createTime, msgType); if ("event".equals(msgType)) return parseInEvent(root, toUserName, fromUserName, createTime, msgType); throw new RuntimeException("???"); }
From source file:com.jfinal.weixin.sdk.message.InMessageParaser.java
License:Apache License
public static void main(String[] args) throws DocumentException { String xml = "<xml>\n" + "<ToUserName><![CDATA[James]]></ToUserName>\n" + "<FromUserName><![CDATA[JFinal]]></FromUserName>\n" + "<CreateTime>1348831860</CreateTime>\n" + "<MsgType><![CDATA[text]]></MsgType>\n" + "<Content><![CDATA[this is a test]]></Content>\n" + "<MsgId>1234567890123456</MsgId>\n" + "</xml>"; // InTextMessage msg = (InTextMessage)parse(xml); // System.out.println(msg.getToUserName()); // System.out.println(msg.getFromUserName()); // System.out.println(msg.getContent()); String xml_2 = "<xml>\n" + "<ToUserName><![CDATA[James]]></ToUserName>\n" + "<FromUserName><![CDATA[JFinal]]></FromUserName>\n" + "<CreateTime>1348831860</CreateTime>\n" + "<MsgType><![CDATA[text]]></MsgType>\n" + "<Content><![CDATA[this is a test]]></Content>\n" + "<MsgId>1234567890123456</MsgId>\n" + "</xml>"; Document doc = DocumentHelper.parseText(xml_2); Element root = doc.getRootElement(); String value = root.elementText("abc"); System.out.println(value);//from w ww.j a v a 2 s .c om }
From source file:com.jfinal.weixin.sdk.msg.InMsgParaser.java
License:Apache License
/** * ?/*ww w . ja 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 parseInVoiceMsgAndInSpeechRecognitionResults(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("???"); }
From source file:com.jfinal.weixin.sdk.msg.InMsgParser.java
License:Apache License
/** * ?//from ww w. j ava 2 s . c o m * 1text ? * 2image ? * 3voice ? * 4video ? * shortvideo ?? * 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 parseInVoiceMsgAndInSpeechRecognitionResults(root, toUserName, fromUserName, createTime, msgType); if ("video".equals(msgType)) return parseInVideoMsg(root, toUserName, fromUserName, createTime, msgType); if ("shortvideo".equals(msgType)) //?? return parseInShortVideoMsg(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( "? " + msgType + "??"); }
From source file:com.jinglingtec.ijiazu.util.CCPRestSmsSDK.java
License:Open Source License
/** * @param xml/*from w ww . jav a2 s. c o m*/ * * @return Map * * @description xml??map */ private HashMap<String, Object> xmlToMap(String xml) { HashMap<String, Object> map = new HashMap<String, Object>(); Document doc = null; try { doc = DocumentHelper.parseText(xml); // XML Element rootElt = doc.getRootElement(); // ? HashMap<String, Object> hashMap2 = new HashMap<String, Object>(); for (Iterator i = rootElt.elementIterator(); i.hasNext();) { Element e = (Element) i.next(); if ("statusCode".equals(e.getName()) || "statusMsg".equals(e.getName())) { map.put(e.getName(), e.getText()); } else { if ("SubAccount".equals(e.getName()) || "totalCount".equals(e.getName()) || "token".equals(e.getName()) || "downUrl".equals(e.getName())) { if (!"SubAccount".equals(e.getName())) { hashMap2.put(e.getName(), e.getText()); } else { ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Iterator i2 = e.elementIterator(); i2.hasNext();) { Element e2 = (Element) i2.next(); hashMap3.put(e2.getName(), e2.getText()); arrayList.add(hashMap3); } hashMap2.put("SubAccount", arrayList); } map.put("data", hashMap2); } else { HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Iterator i2 = e.elementIterator(); i2.hasNext();) { Element e2 = (Element) i2.next(); // hashMap2.put(e2.getName(),e2.getText()); hashMap3.put(e2.getName(), e2.getText()); } if (hashMap3.size() != 0) { hashMap2.put(e.getName(), hashMap3); } else { hashMap2.put(e.getName(), e.getText()); } map.put("data", hashMap2); } } } } catch (DocumentException e) { e.printStackTrace(); LoggerUtil.error(e.getMessage()); } catch (Exception e) { LoggerUtil.error(e.getMessage()); e.printStackTrace(); } return map; }
From source file:com.jiusit.onePurchase.pay.Pay.java
License:Open Source License
public static Map<String, String> xml2Map(String xml) { Map<String, String> map = new HashMap<String, String>(); try {//w w w . jav a 2 s .c o m Document document = DocumentHelper.parseText(xml); Element root = document.getRootElement(); for (Iterator it = root.elementIterator(); it.hasNext();) { Element element = (Element) it.next(); map.put(element.getName(), element.getTextTrim()); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return map; }
From source file:com.jiuyi.doctor.call.AuthController.java
License:Open Source License
@RequestMapping("callauth") public void doAuth(HttpServletRequest request, HttpServletResponse response) { Document doc = null;//from w w w .j a v a2s . c o m String body = ""; log.info(" =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= starts =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= "); try { // ???log InputStream in = request.getInputStream(); BufferedReader bf = new BufferedReader(new InputStreamReader(in)); String str = null; StringBuffer xmlfile = new StringBuffer(); while ((str = bf.readLine()) != null) { xmlfile.append(str); } log.info(" --- xml body --- :" + xmlfile); doc = DocumentHelper.parseText(xmlfile.toString()); } catch (DocumentException e) { log.error(" *** DocumentException ***", e); } catch (IOException e1) { log.error(" *** IOException ***", e1); } Element root = doc.getRootElement(); String action = root.elementTextTrim("action"); if (action.equals("CallAuth")) { // ??? body = parseCallAuth(root); } else if (action.equals("CallEstablish")) { // ? body = parseCallEstablish(root); } else if (action.equals("Hangup")) { // ? body = parseHangup(root); } // header response.setHeader("Status-Code", "HTTP/1.1 200 OK"); response.setHeader("Date", new Date() + ""); response.setHeader("Content-Length", body.length() + ""); try { // OutputStream opt = response.getOutputStream(); OutputStreamWriter out = new OutputStreamWriter(opt); out.write(body); out.flush(); } catch (IOException e) { log.error(" *** IOException ***", e); } log.info("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= end =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r\n"); }
From source file:com.kingmed.dp.mail.util.XMLHandler.java
@SuppressWarnings("rawtypes") public static Map<String, String> transXmltoMapForSpec(String xmlInfo) throws DocumentException { Document doc = DocumentHelper.parseText(xmlInfo); // xml Map<String, String> map = new HashMap<String, String>(); Element rootElt = doc.getRootElement(); Iterator iter = rootElt.elementIterator(); while (iter.hasNext()) { Element dataElement = (Element) iter.next(); map.put(dataElement.getName(), dataElement.getText()); }/* w ww . j a va 2s . c o m*/ Element e = (Element) (rootElt.element("specimen").elements().get(0)); map.put("specimen", e.asXML()); return map; }
From source file:com.kingmed.dp.mail.util.XMLHandler.java
public static Map<String, Object> transXmltoMapForReport(String xmlInfo) throws DocumentException { Document doc = DocumentHelper.parseText(xmlInfo); // xml Map<String, Object> map = new HashMap<String, Object>(); Element rootElt = doc.getRootElement(); @SuppressWarnings("rawtypes") Iterator iter = rootElt.elementIterator(); while (iter.hasNext()) { Element dataElement = (Element) iter.next(); map.put(dataElement.getName(), dataElement.getText()); }//from w ww .j av a2 s . c o m /* Element e = (Element) (rootElt.element("Data")); String data = ""; if (e != null) { data = ((Element) e.elements().get(0)).asXML(); map.put("Data", "<Data>" + data + "</Data>"); } else { data = xmlInfo; map.put("Data", data); } */ return map; }