网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Java中使用XML格式和JSON格式数据的示例

小编给大家分享一下Java中使用XML格式和JSON格式数据的示例,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

创新互联建站专注于企业营销型网站建设、网站重做改版、天全网站定制设计、自适应品牌网站建设、HTML5建站商城系统网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为天全等各大城市提供网站开发制作服务。

Java微信开发中使用XML格式和JSON格式数据的示例

注意一下json-lib所需要的jar包

XML微信XML消息model定义:

package cn.wx.server;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
 
/**
 * @title cn.wx.serverXMLMsg.java
 * @todo TODO
 * @author lpe234
 * @time 2014年5月21日下午2:13:27
 */
public class XMLMsg {
//普通消息基本变量
 String ToUserName;
 String FromUserName;
 String CreateTime;
 String MsgType;
 String Content;
 String MsgId;
//事件推送变量
 String Event;
//自定义菜单项
 String EventKey;
  
 
 public String getEventKey() {
  return EventKey;
 }
 
 public void setEventKey(String eventKey) {
  EventKey = eventKey;
 }
 
 public XMLMsg(String str) throws DocumentException {
  Document doc = DocumentHelper.parseText(str);
  Element root = doc.getRootElement();
  this.ToUserName = root.elementText("ToUserName");
  this.FromUserName = root.elementText("FromUserName");
  this.CreateTime = root.elementText("CreateTime");
  this.MsgType = root.elementText("MsgType");
  this.Content = root.elementText("Content");
  this.MsgId = root.elementText("MsgId");
   
  this.Event = root.elementText("Event");
  this.EventKey = root.elementText("EventKey");
 }
 
 public String getEvent() {
  return Event;
 }
 
 public void setEvent(String event) {
  Event = event;
 }
 
 public String getToUserName() {
  return ToUserName;
 }
 
 public void setToUserName(String toUserName) {
  ToUserName = toUserName;
 }
 
 public String getFromUserName() {
  return FromUserName;
 }
 
 public void setFromUserName(String fromUserName) {
  FromUserName = fromUserName;
 }
 
 public String getCreateTime() {
  return CreateTime;
 }
 
 public void setCreateTime(String createTime) {
  CreateTime = createTime;
 }
 
 public String getMsgType() {
  return MsgType;
 }
 
 public void setMsgType(String msgType) {
  MsgType = msgType;
 }
 
 public String getContent() {
  return Content;
 }
 
 public void setContent(String content) {
  Content = content;
 }
 
 public String getMsgId() {
  return MsgId;
 }
 
 public void setMsgId(String msgId) {
  MsgId = msgId;
 }
}


JSON
这里我们使用json-lib,注意一下需要以下几个jar包的支持:

  • json-lib-2.4-jdk15.jar

  • commons-logging-1.1.3.jar

  • ezmorph-1.0.6.jar

  • commons-lang-2.4.jar

  • commons-collections.jar

  • commons-beanutils-1.8.0.jar

以下是简单的AccessToken类,返回String类型的access_token

package cn.wx.server;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
 
import net.sf.json.JSONObject;
 
public class AccessToken {
 
 /**
  * 根据注册信息,获得的参数,提交get请求,获得accessTkoen
  * @author lpe234
  * @time 2014-5-21 00:52:15
  */
 String appID = "XXXXXXXXXXXXXX";
 String appsecret = "XXXXXXXXXXXXXXXXX";//微信服务号或者申请测试账号的订阅号才有。。。
 String preUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
 String tempUrl = String.format(preUrl, appID, appsecret);
/** 测试
* public static void main(String[] args) {
*  AccessToken as = new AccessToken();
*  System.out.println(as.get());
* }
*/
 //返回String类型access_token
 public String get() {
  String temp = null;
  temp = getJSON();
  JSONObject j = JSONObject.fromObject(temp);
  temp = j.getString("access_token");
  //System.out.println(temp);
  return temp;
 }
 
 // 获取wx服务器返回JSON数据,private内部调用
 private String getJSON() {
  String temp = null;
  try {
   URL url = new URL(tempUrl);
   URLConnection conn = url.openConnection();
   InputStreamReader isr = new InputStreamReader(conn.getInputStream());
   BufferedReader br = new BufferedReader(isr);
   temp = br.readLine();
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  //System.out.println(temp);
  return temp;
 }
}

看完了这篇文章,相信你对“Java中使用XML格式和JSON格式数据的示例”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网站名称:Java中使用XML格式和JSON格式数据的示例
文章URL:http://bjjierui.cn/article/iesegi.html

其他资讯