作者:Flyingis
XML已经成为一种数据传输标准,解析XML是现在任何一门语言的基本功能,解析XML需要先了解XML的组成结构、遍历方式,可以先在互联网搜索。使用ArcGIS Flex API去解析XML的一个主要场景是在"回调"阶段,Demo "Reading GeoRSS feeds"中的GeoRSSUtil.as包含了一些基本应用方法。
Code
package com.esri.ags.samples
{
import com.esri.ags.geometry.Geometry;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.geometry.Polygon;
import mx.utils.StringUtil;
public class GeoRSSUtil
{
private static const GEORSS : Namespace = Namespaces.GEORSS_NS;
private static const GEOWGS : Namespace = Namespaces.GEOWGS_NS;
private static const GEO : Namespace = Namespaces.GEO_NS;
private static const GML : Namespace = Namespaces.GML_NS;
public function GeoRSSUtil(singletonEnforcer:SingletonEnforcer)
{
}
public static function toGeometry( x : XML ) : Geometry
{
const geoLat : String = String(x.GEOWGS::lat );
const geoLon : String = String(x.GEOWGS::long );
if( geoLat && geoLon )
{
return new MapPoint( Number( geoLon ), Number( geoLat ));
}
const georssPoint : String = String(x.GEORSS::point);
if( georssPoint )
{
return parseGeoRSSPoint(georssPoint);
}
const pointList : XMLList = x.GEO::point;
if( pointList && pointList.length() > 0 )
{
const geoPoint : XML = pointList[0];
const geoPLat : Number = Number(geoPoint.GEO::lat);
const geoPLon : Number = Number(geoPoint.GEO::long);
return new MapPoint( geoPLon, geoPLat );
}
const georssPolygon : String = String(x.GEORSS::polygon);
if( georssPolygon )
{
return parseGeoRSSPolygon( georssPolygon);
}
const whereList : XMLList = x.GEORSS::where;
if( whereList && whereList.length() > 0 )
{
const pos : String = whereList[0].GML::Point[0].GML::pos[0];
const arr : Array = pos.split(" ");
const gmlLat : Number = Number(arr[0]);
const gmlLon : Number = Number(arr[1]);
return new MapPoint( gmlLon, gmlLat );
}
return null;
}
private static function parseGeoRSSWhere( x : XML ) : Geometry
{
return null;
}
private static function parseGeoRSSPoint( text : String ) : Geometry
{
const tokens : Array = StringUtil.trim(text).split(" ");
const lat : Number = Number(tokens[0]);
const lon : Number = Number(tokens[1]);
return new MapPoint( lon, lat);
}
private static function parseGeoRSSPolygon( text : String ) : Geometry
{
const path : Array = [];
const tokens : Array = StringUtil.trim(text).split(" ");
for( var i:int=0, j:int=1; j < tokens.length; i+=2, j+=2 )
{
var lat : Number = Number(tokens[i]);
var lon : Number = Number(tokens[j]);
path.push( new MapPoint( lon, lat) );
}
return new Polygon([path]);
}
}
}
class SingletonEnforcer
{
}
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
package com.esri.ags.samples
{
import com.esri.ags.geometry.Geometry;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.geometry.Polygon;
import mx.utils.StringUtil;
public class GeoRSSUtil
{
private static const GEORSS : Namespace = Namespaces.GEORSS_NS;
private static const GEOWGS : Namespace = Namespaces.GEOWGS_NS;
private static const GEO : Namespace = Namespaces.GEO_NS;
private static const GML : Namespace = Namespaces.GML_NS;
public function GeoRSSUtil(singletonEnforcer:SingletonEnforcer)
{
}
public static function toGeometry( x : XML ) : Geometry
{
const geoLat : String = String(x.GEOWGS::lat );
const geoLon : String = String(x.GEOWGS::long );
if( geoLat && geoLon )
{
return new MapPoint( Number( geoLon ), Number( geoLat ));
}
const georssPoint : String = String(x.GEORSS::point);
if( georssPoint )
{
return parseGeoRSSPoint(georssPoint);
}
const pointList : XMLList = x.GEO::point;
if( pointList && pointList.length() > 0 )
{
const geoPoint : XML = pointList[0];
const geoPLat : Number = Number(geoPoint.GEO::lat);
const geoPLon : Number = Number(geoPoint.GEO::long);
return new MapPoint( geoPLon, geoPLat );
}
const georssPolygon : String = String(x.GEORSS::polygon);
if( georssPolygon )
{
return parseGeoRSSPolygon( georssPolygon);
}
const whereList : XMLList = x.GEORSS::where;
if( whereList && whereList.length() > 0 )
{
const pos : String = whereList[0].GML::Point[0].GML::pos[0];
const arr : Array = pos.split(" ");
const gmlLat : Number = Number(arr[0]);
const gmlLon : Number = Number(arr[1]);
return new MapPoint( gmlLon, gmlLat );
}
return null;
}
private static function parseGeoRSSWhere( x : XML ) : Geometry
{
return null;
}
private static function parseGeoRSSPoint( text : String ) : Geometry
{
const tokens : Array = StringUtil.trim(text).split(" ");
const lat : Number = Number(tokens[0]);
const lon : Number = Number(tokens[1]);
return new MapPoint( lon, lat);
}
private static function parseGeoRSSPolygon( text : String ) : Geometry
{
const path : Array = [];
const tokens : Array = StringUtil.trim(text).split(" ");
for( var i:int=0, j:int=1; j < tokens.length; i+=2, j+=2 )
{
var lat : Number = Number(tokens[i]);
var lon : Number = Number(tokens[j]);
path.push( new MapPoint( lon, lat) );
}
return new Polygon([path]);
}
}
}
class SingletonEnforcer
{
}
XML解析中的中文编码是经常会碰到的一个问题,但万变不离其宗——UTF-8,请参考这两篇文章:
说到GeoRSS,GeoRSS是一种地理位置搜索与聚合的方法,在地图上显示当前消息的地理位置,标注出来,点击查询时可以访问该消息的具体内容,如在地图上以GeoRSS方式标识会议地点,点击之后可以继续挖掘信息,一月前我就是通过GeoRSS找到了Oracle开发者大会具体位置和时间。