XML HTTPService 结果和每个

发布于 2024-08-23 20:50:43 字数 901 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉目亦如画i 2024-08-30 20:50:43

我认为这个问题实际上是一个错误,您可以在 HTTPService 结果处理程序中处理它并强制返回的对象成为数组集合。这是我的应用程序的一个片段。

if (evt.result.rows.row is ArrayCollection) {
    MyAC= evt.result.rows.row;
}else{
    MyAC= new ArrayCollection(ArrayUtil.toArray(evt.result.rows.row));
}

所以它应该像这样适合你:

var resObj:Object = new Object();

if (event.result.rows.row is ArrayCollection) {
    resObj= event.result
}else{
    resObj= new ArrayCollection(ArrayUtil.toArray(event.result));
}

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.

if (evt.result.rows.row is ArrayCollection) {
    MyAC= evt.result.rows.row;
}else{
    MyAC= new ArrayCollection(ArrayUtil.toArray(evt.result.rows.row));
}

So it should work for you like this:

var resObj:Object = new Object();

if (event.result.rows.row is ArrayCollection) {
    resObj= event.result
}else{
    resObj= new ArrayCollection(ArrayUtil.toArray(event.result));
}
哀由 2024-08-30 20:50:43

这是我当前的解决方案:

// historial
var hist:ArrayCollection = new ArrayCollection();
if(resObj.hist[0])
  hist = resObj.hist;
else
  hist.addItem(resObj.hist);

This is my current solution:

// historial
var hist:ArrayCollection = new ArrayCollection();
if(resObj.hist[0])
  hist = resObj.hist;
else
  hist.addItem(resObj.hist);
深居我梦 2024-08-30 20:50:43

听起来您的 元素是顶级元素,而不是在其他根目录内?一个 XML 文档只能有一个根,因此在这种情况下,您实际上在某些情况下获得一个 XML 文档,在其他情况下获得一个 XML 文档的 ArrayCollection。如果您将服务更改为始终返回一个将 specifictag 作为子项的 XML 容器,则不必担心此问题。

以下代码演示了在处理 XML 对象时迭代一个元素与迭代多个元素的工作方式相同。

private function init():void {
    trace("Iterating two children");
    iterate(<root><child /><child /></root>);
    trace("Iterating one child:");
    iterate(<root><child /></root>);
}

private function iterate(x:XML):void {

    for each(var element:XML in x.child) {
        trace(element.name());
    }
}

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 an ArrayCollection of XML documents in other cases. If you change the service to always return a single XML container which has specifictag 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.

private function init():void {
    trace("Iterating two children");
    iterate(<root><child /><child /></root>);
    trace("Iterating one child:");
    iterate(<root><child /></root>);
}

private function iterate(x:XML):void {

    for each(var element:XML in x.child) {
        trace(element.name());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文