博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flex与Java交互(Flex调用java类展示数据)解析xml展示数据
阅读量:4921 次
发布时间:2019-06-11

本文共 9667 字,大约阅读时间需要 32 分钟。

Flex与java通信最简单例子(详细说明了各种需要注意的配置):

Flex与java通信(java查询数据库返回List,Flex调用java展示数据):

Flex与java通信(java解析xml返回List,Flex调用java展示数据):

 

这个例子展示的意思是:

java解析xml 并返回一个比较复杂的实体对象集合;

然后Flex调用java类拿到对象集合进行遍历,

并将遍历出来的数据塞到一个新的ArrayCollection中,用DataGrid进行数据的展示。

这里的重点不在java,而是Flex的显示。

 

1、java项目中(为树状xml设计的实体类)

这个实体类稍显复杂,这样写是为了根节点能对应子节点,可能有更方便的写法,希望大家可以提出好的建议

package tobaccoTrans.entity;import java.io.Serializable;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * @author DWJ    */public class UserNode implements Serializable {	private String provinceId;	private String provinceName;	// key=proviceId  value=[cityid,cityname]	private List
cities; // key=proviceId_cityId value=[disId,disName] private Map
> disMap; // key=proviceId_cityId_disId value=[nodeId,nodeName] private Map
> nodeMap; public String getProvinceId() { return provinceId; } public void setProvinceId(String provinceId) { this.provinceId = provinceId; } public String getProvinceName() { return provinceName; } public void setProvinceName(String provinceName) { this.provinceName = provinceName; } public List
getCities() { if (cities == null) { return new ArrayList
(); } return cities; } public void setCities(List
cities) { this.cities = cities; } public Map
> getDisMap() { if (disMap == null) { return new HashMap
>(); } return disMap; } public void setDisMap(Map
> disMap) { this.disMap = disMap; } public Map
> getNodeMap() { if (nodeMap == null) { return new HashMap
>(); } return nodeMap; } public void setNodeMap(Map
> nodeMap) { this.nodeMap = nodeMap; }}

2、java中的xml解析类

package tobaccoTrans.dao.impl;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.Node;import tobaccoTrans.entity.UserNode;import tobaccoTrans.util.ReadStream;/** * @author DWJ */public class UserNodeImpl extends ReadStream{	private Document document = null;	/**	 * @param user 用户  	 * @param pwd 密码	 * @return List
以实体对象为单位的数据集合 * @throws DocumentException * 解析xml文件存储到集合中 */ public List getAllNodes(String user,String pwd){ List
list = new ArrayList
(); try { String path = "http://10.60.250.116/server/getnode.php?"; //内网地址// String path = "http://www.yancao18.com/server/getnode.php?"; //外网地址 path += "user="+user+"&pwd="+pwd; document = this.Read(path.trim()); Element root = document.getRootElement(); //获取省份下各节点 List
nodes = root.selectNodes("/root/pnode/province"); Iterator iter = nodes.iterator(); while (iter.hasNext()) { UserNode provinceInfo = new UserNode(); Map
> disMap = new HashMap
>(); Map
> nodeMap = new HashMap
>(); Element proviceElement = (Element) iter.next(); String provinceId = proviceElement.elementText("pid"); String provinceName = proviceElement.elementText("pname"); provinceInfo.setProvinceId(provinceId); provinceInfo.setProvinceName(provinceName); //获取城市下各节点 List
cityList = new ArrayList
(); List
cityNodes = proviceElement.selectNodes("cnode/city"); Iterator cityIter = cityNodes.iterator(); while (cityIter.hasNext()) { String[] cityInfo = new String[2]; Element cityElement = (Element) cityIter.next(); String cityId = cityElement.elementText("cid"); String cityName = cityElement.elementText("cname"); cityInfo[0] = cityId; cityInfo[1] = cityName; //获取区域下各节点 List
disList = new ArrayList
(); String disKey = provinceId + "_" + cityId; List
disNodes = cityElement.selectNodes("znode/zone"); Iterator disIter = disNodes.iterator(); while (disIter.hasNext()) { String[] disInfo = new String[2]; Element disElement = (Element) disIter.next(); String disNodeId = disElement.elementText("zid"); String disNodeName = disElement.elementText("zname"); disInfo[0] = disNodeId; disInfo[1] = disNodeName; //获取各子节点 List
nodeList = new ArrayList
(); String nodeKey = provinceId + "_" + cityId + "_" + disNodeId; List
nodeNodes = disElement.selectNodes("nnode/node"); Iterator nodeIter = nodeNodes.iterator(); while (nodeIter.hasNext()) { String[] nodeInfo = new String[3]; Element nodeElement = (Element) nodeIter.next(); String nodeNodeId = nodeElement.elementText("nid"); String nodeNodeName = nodeElement.elementText("nname"); String userId = nodeElement.elementText("userid"); nodeInfo[0] = nodeNodeId; nodeInfo[1] = nodeNodeName; nodeInfo[2] = userId; nodeList.add(nodeInfo); } nodeMap.put(nodeKey, nodeList); disList.add(disInfo); } disMap.put(disKey, disList); cityList.add(cityInfo); } provinceInfo.setCities(cityList); provinceInfo.setDisMap(disMap); provinceInfo.setNodeMap(nodeMap); list.add(provinceInfo); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }

 

下面是xml解析类继承的一个读取数据流类

package tobaccoTrans.util;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.dom4j.Document;import org.dom4j.io.DOMReader;/** * @author DWJ */public class ReadStream {	public static Document Read(String str) {		URL url;		Document document = null;		try {			url = new URL(str);			HttpURLConnection conn = (HttpURLConnection) url.openConnection();			conn.setConnectTimeout(6 * 1000);// 设置连接超时			if (conn.getResponseCode() != 200)throw new RuntimeException("请求rul失败!");			InputStream inputStream = (InputStream) conn.getInputStream();			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();			DocumentBuilder db = dbf.newDocumentBuilder();			org.w3c.dom.Document doc = db.parse(inputStream);//			org.w3c.dom.Document doc = db.parse(new InputSource(new ByteArrayInputStream(inputStream.getBytes("utf-8"))));			DOMReader domReader = new DOMReader();			document = domReader.read((org.w3c.dom.Document) doc);		} catch (Exception e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		return document;	}}

 

3、remoting-config.xml 配置,更对详细配置请参照:

tobaccoTrans.dao.impl.UserNodeImpl

 

4、Flex项目中mxml应用程序的写法

注意:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

5、绑定好,运行mxml应用程序即可看到数据。

 

 

转载于:https://www.cnblogs.com/snake-hand/p/3144816.html

你可能感兴趣的文章
第二次作业(homework-02)成绩公布
查看>>
KVM&Libvirt基本概念及开发杂谈
查看>>
flv视频格式详细解析
查看>>
Python装饰器
查看>>
美剧推荐之《越狱》
查看>>
网络相关知识
查看>>
Chapter 1 Securing Your Server and Network(5):使用SSL加密会话
查看>>
19、路由和拓扑图和lan
查看>>
编写高性能 Web 应用程序的 10 个技巧
查看>>
a 锚点跳转滑动效果
查看>>
iOS9.0 LaunchScreen.StroyBoard自定义启动图片
查看>>
14、求出最大元素的下标及地址值——数组
查看>>
rm 删除不掉文件,报错解决 以及 chattr的介绍
查看>>
《需求工程——软件建模与分析》读后感
查看>>
ovs-vsctl 命令详解
查看>>
超级账本Fabric教程(一):超级账本入门
查看>>
.net core 使用阿里云短信发送SMS
查看>>
Unity5.1 新的网络引擎UNET(四) UNET Remote Actions
查看>>
How to get service execuable path
查看>>
39岁了,我依旧要谈梦想
查看>>