删除/替换 E4X 中的节点(AS3 - Flex)
我正在 Flex
应用程序中构建列表/网格控件,并在 .NET
Web 应用程序中使用它。 长话短说,我从序列化对象的 Web 服务中获取 XML。 我对一页上可以包含多少内容有页面限制。 我采用了数据网格并将其分页、跨页排序并处理一些基本过滤。
关于分页,我使用在页面上键入的字典并存储该页面的 XML。 这样,每当用户返回到我保存到此字典中的页面时,我就可以从本地内存中获取 XML,而不用访问 Web 服务。 基本上,我会缓存每次调用 Web 服务时检索到的数据以获取数据页。
有几种情况可能会使我的缓存过期。 过滤和排序是主要原因。 然而,用户可以通过打开编辑器来编辑网格中的一行数据。 他们编辑的数据可能会导致行中显示的数据过时。 我可以轻松地访问网络服务并获取整个数据页面,但由于页面大小是在运行时设置的,因此我可能会查看大量记录来检索。
现在让我谈谈我所遇到的问题的核心。 为了防止取回整个数据页,我调用网络服务,请求完全更新的记录(编辑器负责保存其数据)。
由于我使用的是自定义对象,因此我需要在服务器上将它们序列化为 XML(这已针对我们软件的其他部分进行了处理)。 所有数据均通过 e4x 中的 XML 进行处理。 Dictionary 中的缓存存储为 XMLList。
现在让我向您展示我的代码...
var idOfReplacee:String = this._WebService.GetSingleModelXml.lastResult.*[0].*[0].@Id;
var xmlToReplace:XMLList = this._DataPages[this._Options.PageIndex].Data.(@Id == idOfReplacee);
if(xmlToReplace.length() > 0)
{
delete (this._DataPages[this._Options.PageIndex].Data.(@Id == idOfReplacee)[0]);
this._DataPages[this._Options.PageIndex].Data += this._WebService.GetSingleModelXml.lastResult.*[0].*[0];
}
基本上,我获取了要替换的节点的 ID。 然后我在缓存的 Data 属性 (XMLList
) 中找到它。 我确保它存在,因为第二行的过滤器返回 XMLList
。
我遇到的问题是删除行。 我无法使该行从列表中删除该节点。 删除行后面的行有效。 我已将该节点添加到列表中。
如何替换或删除该节点(意味着我从缓存的 .Data 属性中的过滤器语句中找到的节点)???
希望我所有变量的下划线在发布时不会被转义! 否则 this._ == this
._
I'm building a listing/grid control in a Flex
application and using it in a .NET
web application. To make a really long story short I am getting XML from a webservice of serialized objects. I have a page limit of how many things can be on a page. I've taken a data grid and made it page, sort across pages, and handle some basic filtering.
In regards to paging I'm using a Dictionary keyed on the page and storing the XML for that page. This way whenever a user comes back to a page that I've saved into this dictionary I can grab the XML from local memory instead of hitting the webservice. Basically, I'm caching the data retrieved from each call to the webservice for a page of data.
There are several things that can expire my cache. Filtering and sorting are the main reason. However, a user may edit a row of data in the grid by opening an editor. The data they edit could cause the data displayed in the row to be stale. I could easily go to the webservice and get the whole page of data, but since the page size is set at runtime I could be looking at a large amount of records to retrieve.
So let me now get to the heart of the issue that I am experiencing. In order to prevent getting the whole page of data back I make a call to the webservice asking for the completely updated record (the editor handles saving its data).
Since I'm using custom objects I need to serialize them on the server to XML (this is handled already for other portions of our software). All data is handled through XML in e4x. The cache in the Dictionary is stored as an XMLList.
Now let me show you my code...
var idOfReplacee:String = this._WebService.GetSingleModelXml.lastResult.*[0].*[0].@Id;
var xmlToReplace:XMLList = this._DataPages[this._Options.PageIndex].Data.(@Id == idOfReplacee);
if(xmlToReplace.length() > 0)
{
delete (this._DataPages[this._Options.PageIndex].Data.(@Id == idOfReplacee)[0]);
this._DataPages[this._Options.PageIndex].Data += this._WebService.GetSingleModelXml.lastResult.*[0].*[0];
}
Basically, I get the id of the node I want to replace. Then I find it in the cache's Data property (XMLList
). I make sure it exists since the filter on the second line returns the XMLList
.
The problem I have is with the delete line. I cannot make that line delete that node from the list. The line following the delete line works. I've added the node to the list.
How do I replace or delete that node (meaning the node that I find from the filter statement out of the .Data property of the cache)???
Hopefully the underscores for all of my variables do not stay escaped when this is posted! otherwise this._ == this
._
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
谢谢你们的回答。
@西奥:
我尝试了几种不同的方式进行替换。 由于某种原因,它永远不会出错,但永远不会更新列表。
@马特:
我想出了一个解决办法。 问题不是来自您的建议,而是来自删除如何与列表一起工作(至少在本例中我是如何使用它的)。
_DataPages 字典对象的 Data 属性是定义节点的列表(通过先前过滤另一个 XML 文档得出)。
我最终做了这个小交易:
所以基本上我必须获取 XMLList 中节点的索引,即 Data 属性。 从那里我可以使用删除关键字将其从列表中删除。 += 将我的新节点添加到列表中。
我非常习惯使用 ActiveX 或 Mozilla XmlDocument 的东西,您可以在其中调用“SelectSingleNode”,然后使用“replaceChild”来执行此类操作。 哦,好吧,至少这是在某个论坛上,其他人可以找到它。 我不知道当我回答自己的问题时会发生什么。 也许这种见解会帮助其他人更好地回答这个问题!
Thanks for the answers guys.
@Theo:
I tried the replace several different ways. For some reason it would never error, but never update the list.
@Matt:
I figured out a solution. The issue wasn't coming from what you suggested, but from how the delete works with Lists (at least how I have it in this instance).
The Data property of the _DataPages dictionary object is list of the definition nodes (was arrived at by a previous filtering of another XML document).
I ended up doing this little deal:
So basically I had to get the index of the node in the XMLList that is the Data property. From there I could use the delete keyword to remove it from the list. The += adds my new node to the list.
I'm so used to using the ActiveX or Mozilla XmlDocument stuff where you call "SelectSingleNode" and then use "replaceChild" to do this kind of stuff. Oh well, at least this is in some forum where someone else can find it. I do not know the procedure for what happens when I answer my own question. Perhaps this insight will help someone else come along and help answer the question better!
也许您可以使用
replace
来代替?Perhaps you could use
replace
instead?我知道这是一个非常古老的问题,但我没有看到(我认为是)这个问题的最简单的解决方案。
Theo 在这里的方向是正确的,但是在使用替换的方式上存在许多错误(事实上,E4X 中几乎所有内容都是函数)。
我相信这会解决问题:
replace()
可以在第一个参数中采用多种不同的类型,但据我所知,XML 对象不是其中之一。I know this is an incredibly old question, but I don't see (what I think is) the simplest solution to this problem.
Theo had the right direction here, but there's a number of errors with the way replace was being used (and the fact that pretty much everything in E4X is a function).
I believe this will do the trick:
replace()
can take a number of different types in the first parameter, but AFAIK, XML objects are not one of them.我没有立即看到问题,所以我只能大胆猜测。 您获得的
delete
行正在查找列表顶层的第一个项目,该项目的属性“Id”的值等于idOfReplacee
。 确保您不需要深入挖掘 XML 结构来查找匹配的 ID。请尝试以下操作:(
请注意
Data
之后的额外“.”)。 您可以通过在发布的代码的第二行上设置断点来更轻松地调试它,并确保 XMLList 看起来像您期望的那样。I don't immediately see the problem, so I can only venture a guess. The
delete
line that you've got is looking for the first item at the top level of the list which has an attribute "Id" with a value equal toidOfReplacee
. Ensure that you don't need to dig deeper into the XML structure to find that matching id.Try this instead:
(Notice the extra '.' after
Data
). You could more easily debug this by setting a breakpoint on the second line of the code you posted, and ensure that the XMLList looks like you expect.