问题描述
如何在Flex中将平面/分层数据转换为XML格式.以下是我的分层数据:(表格格式)
How to convert a flat/hierarchical data to XML format in Flex.The following is my Hierarchical Data:(Table format)
Asia India Chennai TN Category1 Product1 100
Asia India Mumbai MH Category1 Product1 100
Asia India Calcutta CT Category1 Product1 100
Asia India Calcutta CT Category2 Product2 200
EMEA UK London LN Category3 Product1 123
EMEA UK London LN Category3 Product2 455
EMEA UK Reading RN Category1 Product1 500
EMEA UK Reading RN Category1 Product2 430
I need to format/convert this to XML format so that I can populate that resulting xml as dataprovider to a Tree control.
Asia
India
Chennai
TN
Category1
Product1
100
Mumbai
MH
Category1
Product1
100
such a tree structure.
推荐答案
我将创建一个简单的转换器以获取真实的XML结构.解决方案的复杂性取决于您拥有的源文本.
I would make a simple converter to get a real XML structure. The complexity of the solution depends on the source text you have.
这是我的实现方式.
假设您的层次结构数据结构良好,并且每个元素之前都具有一定数量的空格.就我而言,我使用4个空格作为一级转换.
Suppose your hierarchical data is good structured and each element has a certain amount of whitespaces before. In my case I use 4 whitespaces as one level shift.
我从文本文件加载数据,如下所示:
I load the data from a text file, which looks like this:
Asia
India
Chennai
TN
Category1
Product1
100
Mumbai
MH
Category2
Product2
200
Category3
Product3
300
Product4
400
然后,我遍历每个字符串并分析其级别.生成的树如下所示:
Then I go through each string and analyse its level.The resulting tree looks like this:
//应用程序
<?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"
minWidth="955" minHeight="600" creationComplete="init(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
private const SPACEDELIMITER:int = 4;
private var loader:URLLoader;
private var ar:Array = new Array();
[Bindable]private var data:XML = <Root/>;
private var inputStr:String;
protected function init(event:FlexEvent):void
{
loader = new URLLoader(new URLRequest("com/treexml/tree.txt"));
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, completeHandler);
}
private function completeHandler(event:Event):void
{
inputStr = URLLoader(event.target).data;
parseString();
}
private function parseString():void
{
var levels:Array = new Array();
ar = inputStr.split("\r\n");
var reg:RegExp = /[a-z0-9]/gi;
var globalShift:int = String(ar[0]).search(reg);
for (var i:int = 0; i < ar.length; i++)
{
var item:String = ar[i];
var shift:int = item.search(reg); //amount of witespaces before the text
var level:int = (shift - globalShift)/SPACEDELIMITER; //level of the node
var label:String = item.substring(shift, item.length);
levels[level] = i; //id of the last element for the given level
var node:XML = new XML();
node = <child id = {i} label = {label}/>;
if (level == 0)
data.appendChild(node);
else
data..child.(@id == levels[level - 1]).appendChild(node);
}
}
]]>
</fx:Script>
<mx:Tree width="250"
height="400"
dataProvider="{data.child}"
labelField="@label"/>
</s:Application>
//EDIT
如果您的数据没有通过空格对齐,请尝试使用另一版本的parseString函数.
If your data is not aligned by means of whitespaces, try to use another edition of the parseString function.
假设您的数据如下所示:
Suppose your data looks like:
Asia India Chennai TN Category1 Product1 100
Asia India Mumbai MH Category1 Product1 100
Asia India Calcutta CT Category1 Product1 100
Asia India Calcutta CT Category2 Product2 200
EMEA UK London LN Category3 Product1 123
EMEA UK London LN Category3 Product2 455
EMEA UK Reading RN Category1 Product1 500
EMEA UK Reading RN Category1 Product2 430
函数是:
private function parseString():void
{
ar = inputStr.split("\r\n");
var map:Dictionary = new Dictionary();
var delimiter:String = "***";
var id:int = 0;
for (var i:int = 0; i < ar.length; i++)
{
if(ar[i].length){//if it's not an empty string
var itemArray:Array = ar[i].replace(/\s{2,}/g, ' ').split(" ");//collapse multiple spaces as one using RegEx
var key:String = "";
var prevkey:String = "";
for (var j:int = 0; j< itemArray.length; j++)
{
prevkey = key;
key += itemArray[j] + delimiter;
if (map[key] == null)
{
map[key] = id;
var node:XML = <child id = {id} label = {itemArray[j]}/>;
if (j == 0)
data.appendChild(node);
else
data..child.(@id == map[prevkey]).appendChild(node);
id++;
}
}
}
}
}
这篇关于在Flex中将分层数据转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!