深层嵌套XML [英] Deep Nested XML

查看:222
本文介绍了深层嵌套XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <序列号=1 > 
< file>
< filenum> 1< / epnum>
< prodnum> 4V01< / prodnum>
< title>系列#1 - 文件#1< / title>
< / file>
< file>
< filenum> 2< / epnum>
< prodnum> 4V02< / prodnum>
< title>系列#1 - 文件#2< / title>
< / file>
< / Series>
< Series no =2>
< file>
< filenum> 1< / epnum>
< prodnum> 4V01< / prodnum>
< title>系列#2 - 文件#1< / title>
< / file>
< file>
< filenum> 2< / epnum>
< prodnum> 4V02< / prodnum>
< title>系列#2 - 文件#2< / title>
< / file>
< / Series>

我当前的代码允许我将每个系列检索到一个XMLList中,然后我有一个允许的nesteddatagrid类我要做这样的事情。

 < classes:NestedDataGrid width =100%height =100%id = gridFilesdataProvider ={filesList}> 
< classes:columns>
< mx:DataGridColumn headerText =SeasondataField =@ nowidth =60/>
< mx:DataGridColumn headerText =EpisodedataField =file.filenumwidth =60/>
< mx:DataGridColumn headerText =TitledataField =file.title/>
< / classes:columns>
< / classes:NestedDataGrid>

但是,这显示了两行的datagrid,第一行在Series列中有1个,两个文件挤在同一行的第二个单元格中。第二行是相同的,但在系列列中的数字2,两个系列#2文件挤入到它旁边的单元格。



如果我不使用嵌套数据类我可以使用Series.file来提取文件,并且所有4个文件都正确列出,但是我没有获得每个文件的序列号。

解决方案

使用xml的当前结构,使用两列网格 - 第一列作为序列号来表示它更容易,第二列是另一个2或3列DataGrid,显示文件细节。但是如果您不想更改结构,则需要以下代码。请注意,由于 dataField 属性未设置,您必须指定一个 sortCompareFunction ,用于根据序列号排序网格 - 尝试排序时可能会抛出异常。

 < classes:NestedDataGrid width =100%height =100% id =gridFiles
dataProvider ={filesList.Series.file}>
< classes:columns><! - 从OP的代码中复制粘贴的类。那是什么? - >
< mx:DataGridColumn headerText =SeasonlabelFunction =getSerieswidth =60/>
< mx:DataGridColumn headerText =EpisodedataField =filenumwidth =60/>
< mx:DataGridColumn headerText =TitledataField =title/>
< / classes:columns>
< / classes:NestedDataGrid>
private function getSeries(item:Object,col:DataGridColumn):String
{
返回XML(item).parent()。
}






更新:

 < mx:DataGrid width =100%height =100%id =gridFiles> 
< mx:columns>
< mx:DataGridColumn headerText =SeasonlabelFunction =getSerieswidth =60/>
< mx:DataGridColumn headerText =EpisodedataField =epnumwidth =60/>
< mx:DataGridColumn headerText =TitledataField =title/>
< / mx:columns>
< / mx:DataGrid>

gridFiles.dataProvider = XML(event.result).descendants('episode');
//使用与上述相同的getSeries函数


I am trying to display a list of items in a datagrid from an XMLList.

<Series no="1">
    <file>
        <filenum>1</epnum>
        <prodnum>4V01</prodnum>
        <title>Series #1 - File #1</title>
    </file>
    <file>
        <filenum>2</epnum>
        <prodnum>4V02</prodnum>
        <title>Series #1 - File #2</title>
    </file>
</Series>
<Series no="2">
    <file>
        <filenum>1</epnum>
        <prodnum>4V01</prodnum>
        <title>Series #2 - File #1</title>
    </file>
    <file>
        <filenum>2</epnum>
        <prodnum>4V02</prodnum>
        <title>Series #2 - File #2</title>
    </file>
</Series>

My current code allows me to retrieve every Series into an XMLList and then i have a nesteddatagrid class that allows me to do things like.

<classes:NestedDataGrid width="100%" height="100%" id="gridFiles" dataProvider="{filesList}" >
<classes:columns>
<mx:DataGridColumn headerText="Season" dataField="@no" width="60"/>
<mx:DataGridColumn headerText="Episode" dataField="file.filenum" width="60"/>
<mx:DataGridColumn headerText="Title" dataField="file.title"/>
</classes:columns>
</classes:NestedDataGrid>

However this displays the datagrid with two rows, the first row has 1 in the Series column and then the two files crammed into the second cell in the same row. The second row is the same but has the number 2 in the Series column and the two series #2 files crammed into the cell next to it.

If i do not use the nested data class i can pull the files using Series.file instead and all 4 of the files list correctly, however i do not get the Series number for each...

解决方案

With the current structure of the xml, it's easier to represent it with a two column grid - first column being the series number, and second column being another 2 or 3 column DataGrid that displays file details. But if you don't wanna change the structure, the following code is what you need. Note that since dataField property is not set, you have to specify a sortCompareFunction for sorting the grid based on series number - otherwise it might throw exceptions while trying to sort.

<classes:NestedDataGrid width="100%" height="100%" id="gridFiles" 
  dataProvider="{filesList.Series.file}" >
  <classes:columns><!-- classes copy pasted from OP's code. Whats that? -->
    <mx:DataGridColumn headerText="Season" labelFunction="getSeries" width="60"/>
    <mx:DataGridColumn headerText="Episode" dataField="filenum" width="60"/>
    <mx:DataGridColumn headerText="Title" dataField="title"/>
  </classes:columns>
</classes:NestedDataGrid>
private function getSeries(item:Object, col:DataGridColumn):String
{
  return XML(item).parent().@no;
}


UPDATE:

<mx:DataGrid width="100%" height="100%" id="gridFiles" > 
  <mx:columns>
    <mx:DataGridColumn headerText="Season" labelFunction="getSeries" width="60"/>
    <mx:DataGridColumn headerText="Episode" dataField="epnum" width="60"/>
    <mx:DataGridColumn headerText="Title" dataField="title"/>
  </mx:columns>
</mx:DataGrid>

gridFiles.dataProvider = XML(event.result).descendants('episode');
//use the same getSeries function as above

这篇关于深层嵌套XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆