XML HTTPService 结果和每个
XML 在 Flex 中很酷,但我有一个恼人的问题需要解决,这是由我能想象到的一个功能引起的。你看,当你只有一个东西的标签时,它会创建一个不是数组的对象。但是当找到多个时,它会将其放入数组结构中。
我想优雅地解决它。任何建议。
示例:这说明了这一点,这是 HTTP 请求的结果:
private function initXMLRes(event:ResultEvent):void
{
var resObj:Object = event.result;
//
for each(var i:Object in resObj.specifictag)
{
// to use specifictag attributes, etc. example:
Alert.show(specifictag.name);
}
}
之前的代码将适用于 2 个以上的项目。它将接受这个 xml:
<specifictag name="one" /><specifictag name="two" />
... 但不是这个:
<specifictag name="one" />
我可以检查 resObj.specifictag 的格式(检查它是否有一个数组),然后复制代码(针对每种情况)。但是 - 即使它调用一个函数 - 我认为这不是一个优雅的解决方案。
好吧,我希望有人对这个问题有一个好主意。 (我从经验中知道,SO 比 Flex 拥有更多的 C++ 专家,但是......)
完美的事情是 HTTPrequest 以一致的方式处理每个标签(始终使用数组......尽管我猜测这会也有其缺点)。
谢谢!
XML is cool in flex, but I have an annoying problem to solve, caused by what I can imagine is a feature. You see, when you have just one tag of something, it creates an object which is not an array. But when finds more than one, it puts it on an array structure.
I want to solve it elegantly. Any recommendation.
Example: this illustrates it, being the result of a HTTP request:
private function initXMLRes(event:ResultEvent):void
{
var resObj:Object = event.result;
//
for each(var i:Object in resObj.specifictag)
{
// to use specifictag attributes, etc. example:
Alert.show(specifictag.name);
}
}
The code before will work with 2+ items. It will accept this xml:
<specifictag name="one" /><specifictag name="two" />
... but not this one:
<specifictag name="one" />
I could check the format of resObj.specifictag (to check if it is has an array) and then duplicate the code (for each case). But -even if it's calling a function- I don't think it is an elegant solution.
Well, I hope someone has a good idea on this matter. (I know from experience that SO has much more C++ experts than flex, but well...)
The perfect thing would be that HTTPrequest handled every tag in a consistent way (using always arrays... though I'm guessing that that would also have its drawbacks).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这个问题实际上是一个错误,您可以在 HTTPService 结果处理程序中处理它并强制返回的对象成为数组集合。这是我的应用程序的一个片段。
所以它应该像这样适合你:
The problem I think is actually a bug, that you can handle in your HTTPService result handler and force your returned object into being an array collection. Here's a snippet from my app.
So it should work for you like this:
这是我当前的解决方案:
This is my current solution:
听起来您的
元素是顶级元素,而不是在其他根目录内?一个 XML 文档只能有一个根,因此在这种情况下,您实际上在某些情况下获得一个 XML 文档,在其他情况下获得一个 XML 文档的ArrayCollection
。如果您将服务更改为始终返回一个将specifictag
作为子项的 XML 容器,则不必担心此问题。以下代码演示了在处理 XML 对象时迭代一个元素与迭代多个元素的工作方式相同。
It sounds like your
<specifictag>
elements are top-level and not inside some other root? An XML document can only have one root, so in this case you're actually getting an XML document in some cases and anArrayCollection
of XML documents in other cases. If you change the service to always return a single XML container which hasspecifictag
as it's children, you won't have to worry about this problem.The following code demonstrates that iterating over one element works the same as iterating over multiple when dealing with XML objects.