Flex与java通信最简单例子(详细说明了各种需要注意的配置):http://blog.csdn.net/u010011052/article/details/9116869

Flex与java通信(java查询数据库返回List,Flex调用java展示数据):http://blog.csdn.net/u010011052/article/details/9116871

Flex与java通信(java解析xml返回List,Flex调用java展示数据):http://blog.csdn.net/u010011052/article/details/9127643

这个例子展示的意思是:

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<String[]> cities;
// key=proviceId_cityId value=[disId,disName]
private Map<String, List<String[]>> disMap;
// key=proviceId_cityId_disId value=[nodeId,nodeName]
private Map<String, List<String[]>> 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<String[]> getCities() {
if (cities == null) {
return new ArrayList<String[]>();
}
return cities;
} public void setCities(List<String[]> cities) {
this.cities = cities;
} public Map<String, List<String[]>> getDisMap() {
if (disMap == null) {
return new HashMap<String, List<String[]>>();
}
return disMap;
} public void setDisMap(Map<String, List<String[]>> disMap) {
this.disMap = disMap;
} public Map<String, List<String[]>> getNodeMap() {
if (nodeMap == null) {
return new HashMap<String, List<String[]>>();
}
return nodeMap;
} public void setNodeMap(Map<String, List<String[]>> 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<UserNode> 以实体对象为单位的数据集合
* @throws DocumentException
* 解析xml文件存储到集合中
*/
public List getAllNodes(String user,String pwd){
List<UserNode> list = new ArrayList<UserNode>();
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<Node> nodes = root.selectNodes("/root/pnode/province");
Iterator iter = nodes.iterator();
while (iter.hasNext()) {
UserNode provinceInfo = new UserNode(); Map<String, List<String[]>> disMap = new HashMap<String, List<String[]>>();
Map<String, List<String[]>> nodeMap = new HashMap<String, List<String[]>>(); Element proviceElement = (Element) iter.next();
String provinceId = proviceElement.elementText("pid");
String provinceName = proviceElement.elementText("pname");
provinceInfo.setProvinceId(provinceId);
provinceInfo.setProvinceName(provinceName); //获取城市下各节点
List<String[]> cityList = new ArrayList<String[]>();
List<Node> 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<String[]> disList = new ArrayList<String[]>();
String disKey = provinceId + "_" + cityId;
List<Node> 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<String[]> nodeList = new ArrayList<String[]>();
String nodeKey = provinceId + "_" + cityId + "_" + disNodeId;
List<Node> 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 配置,更对详细配置请参照:http://blog.csdn.net/u010011052/article/details/9116869

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service" class="flex.messaging.services.RemotingService"> <adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters> <default-channels>
<channel ref="my-amf"/>
</default-channels> <destination id="UserNodeImpl">
<properties>
<source>tobaccoTrans.dao.impl.UserNodeImpl</source>
</properties>
</destination> </service>

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

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="init()"
minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent; [Bindable]
protected function init():void
{
// TODO Auto-generated method stub
uni.getAllNodes("zhucheng","zhucheng");
} [Bindable]
var usernodeList:ArrayCollection = new ArrayCollection();
var cities:ArrayCollection = new ArrayCollection();
var newArray:ArrayCollection = new ArrayCollection(); //创建的一
protected function uni_resultHandler(event:ResultEvent):void
{
// TODO Auto-generated method stub
//获取省份
usernodeList = event.result as ArrayCollection;
var usernodeListLength:int = usernodeList.length;
for(var iusernode:int = 0; iusernode < usernodeListLength; iusernode++){
var pid:String = usernodeList[iusernode].provinceId; //要显示的:省份id
var pname:String = usernodeList[iusernode].provinceName; //要显示的:省份name
newArray.addItem({pid:pid});
newArray.addItem({pname:pname}); //获取城市
cities = usernodeList[iusernode].cities;//iusernode
var citiesLength:int = cities.length;
for(var icity:int = 0; icity < citiesLength; icity++){
//cities[i]下2个子节点
var cityid:String = cities[icity][0].toString(); //要显示的:城市id
var cityname:String = cities[icity][1].toString(); //要显示的:城市name
newArray.addItem({cityid:cityid});
newArray.addItem({cityname:cityname}); //获取区域
var disKey:String = pid + "_" + cityid;
var dismap:ArrayCollection = new ArrayCollection();
dismap = usernodeList[iusernode].disMap[disKey];//iusernode
var dismapLength:int = dismap.length;
for(var idis:int = 0; idis < dismapLength; idis++){
//dismap[i]下2个子节点
var disid:String = dismap[idis][0]; //要显示的:区域id
var disname:String = dismap[idis][1]; //要显示的:区域name
newArray.addItem({disid:disid});
newArray.addItem({disname:disname}); //获取子节点
var nodeKey:String = disKey + "_" + disid;
var nodemap:ArrayCollection = new ArrayCollection();
nodemap = usernodeList[iusernode].nodeMap[nodeKey];//iusernode
var nodemapLength:int = nodemap.length;
for(var inode:int = 0; inode < nodemapLength; inode++){
//nodemap[i]下3个子节点
var nodeid:String = nodemap[inode][0]; //要显示的:子节点id
var nodename:String = nodemap[inode][1]; //要显示的:子节点name
var userid:String = nodemap[inode][2]; //要显示的:userid
newArray.addItem({nodeid:nodeid});
newArray.addItem({nodename:nodename});
newArray.addItem({userid:userid}); } //获取子节点结束 } //获取区域结束 } //获取城市结束 } //获取省份结束 } //uni_resultHandler结束 [Bindable]
protected function uni_faultHandler(event:FaultEvent):void
{
// TODO Auto-generated method stub } ]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:RemoteObject id="uni" destination="UserNodeImpl" result="uni_resultHandler(event)" fault="uni_faultHandler(event)" />
</fx:Declarations>
<mx:Panel x="16" y="15" width="95%" height="95%" title="展示UserNode数据Panel">
<mx:DataGrid x="15" y="15" width="92%" height="92%" id="dataGrid" dataProvider="{newArray}">
<mx:columns>
<mx:DataGridColumn headerText="pid" dataField="pid" />
<mx:DataGridColumn headerText="pname" dataField="pname" />
<mx:DataGridColumn headerText="cid" dataField="cityid" />
<mx:DataGridColumn headerText="cname" dataField="cityname" />
<mx:DataGridColumn headerText="zid" dataField="disid" />
<mx:DataGridColumn headerText="zname" dataField="disname" />
<mx:DataGridColumn headerText="nid" dataField="nodeid" />
<mx:DataGridColumn headerText="nname" dataField="nodename" />
<mx:DataGridColumn headerText="userid" dataField="userid" />
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</s:Application>

注意:

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

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

Flex与Java交互(Flex调用java类展示数据)解析xml展示数据的更多相关文章

  1. Flex使用Blazeds与Java交互及自定义对象转换详解-DATAGRID读取ORACLE数据

    http://www.cnblogs.com/RocD-DuPeng/articles/1751040.html 一.建立Flex与Java交互的工程. 本文中讲到的交互是利用Blazeds的,因为这 ...

  2. java URL实现调用其他系统发送报文并获取返回数据

    模拟本系统通过Url方式发送报文到目标服务器,并获取返回数据:(实现类) import java.io.BufferedOutputStream; import java.io.BufferedRea ...

  3. 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml

    import java.io.IOException; import java.util.List; import javax.servlet.ServletInputStream; import j ...

  4. 通过JAVA反射,调用未知类的类方法

    下面是一个比较简单的通过JAVA的反射机制调用已知方法的例子 package com.togeek.mvntest; import java.lang.reflect.InvocationTarget ...

  5. 如何做好Flex与Java交互

    三种flex4与Java顺利通信的方式是: flex与普通java类通信RemoteObject; flex与服务器交互HTTPService; flex与webservice交互WebService ...

  6. java 通过反射机制调用某个类的方法

    package net.xsoftlab.baike; import java.lang.reflect.Method; public class TestReflect {     public s ...

  7. Oracle调用Java类开发的存储过程、函数的方法

    oracle调用java类的基本步骤 1. 编写java代码,后续可以直接使用java代码,class文件或者jar包 2. 将写好的java代码导入到oracle数据库中,有两种方法:一种是使用lo ...

  8. PHP通过php-java-bridge调用JAVA的jar包里class类

    正 文:   有的时候我们需要在PHP里调用JAVA平台封装好的jar包里的class类和方法,一般飘易推荐的做法是采用php-java-bridge做桥接,本文就来介绍一下大致的实现方法. 先简单说 ...

  9. 通过JNI实现java调用C代码和C代码调用java的代码

    一.java调用C代码 1)java中需要声明调用的函数,也就是native方法,并通过System.LoadLibrary来调用dll或者so(C代码).实例代码如下: public class H ...

随机推荐

  1. Lucene4.3入门

    辞职交接期间无聊看了一下搜索引擎,java社区比较火的当然是Lucene,想写一个简单的小例子,在网上找了些资料,不过都不是4.3的,自己看了一下. 下载地址:http://lucene.apache ...

  2. 动态字符串 Stringbuilder类

    StringBuilder动态字符串 string 类型的修改会浪费资源,如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类, stringbuild ...

  3. TPersistent的三个用途(读写DFM文件,Assign,RTTI),最主要还是第三个用途

    不是什么类对象都需要RTTI,如果把它放在TObject,除了增加可执行文件的大小以及运行内存空间以外,没什么好处.

  4. VC 为静态控件添加事件

    操作系统:Windows 7软件环境:Visual C++ 2008 SP1本次目的:为静态控件添加事件,如:STATIC.Picture Control等等 有时候我们找到一个图片,为对话框背景添加 ...

  5. 支付宝打造公共账号业务网关, RSA密钥对生成

    作者: 玉龙      版权全部,同意转载. 请注明出处(创建金融_玉龙  http://www.weibo.com/u/1872245125) 原文地址: http://blog.csdn.net/ ...

  6. Javascript关闭详细说明

    在我的博客:http://blog.csdn.net/u011043843/article/details/26148265中也有对闭包的解释 在javascript中闭包是一个非常不好理解的概念.可 ...

  7. MSSQL - 存储过程Return返回值

    1.存储过程中不使用外部参数. 存储过程: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ========================== ...

  8. Managing your Actor Systems

    今天偶然得机会,找到一篇不错得文章,现在分享给大家. 原文:http://www.kotancode.com/2013/02/15/managing-your-actor-systems/ If yo ...

  9. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  10. <转载>如何解决子级用float浮动父级div高度不能自适应的问题

    转载:http://www.kwstu.com/ArticleView/divcss_2013101582430202 解决子级对象使用css float浮动 而父级div不能自适应高度,不能被父级内 ...