处理此 XML feed 的最佳方法是什么?

发布于 2024-10-05 17:24:54 字数 5746 浏览 0 评论 0原文

我有一个为我提供地址的 XML 文档。摘录如下:

<ezi:orderaddresses>
        <ezi:orderaddress>
            <ezi:addresstype>billing</ezi:addresstype>
            <ezi:name>Jason Fonseca</ezi:name>
            <ezi:companyname>Cyber</ezi:companyname>
            <ezi:address1>8 springstein</ezi:address1>
            <ezi:division>NT</ezi:division>
            <ezi:postalcode>34245</ezi:postalcode>
            <ezi:countrycode>AU</ezi:countrycode>
            <ezi:email>[email protected]</ezi:email>
            <ezi:phone>89549854</ezi:phone>
            <ezi:mobilephone>984590598</ezi:mobilephone>
        </ezi:orderaddress>
        <ezi:orderaddress>
            <ezi:addresstype>shipping</ezi:addresstype>
            <ezi:name>Jason Fonseca</ezi:name>
            <ezi:companyname>Cyber</ezi:companyname>
            <ezi:address1>8 springstein</ezi:address1>
            <ezi:division>NT</ezi:division>
            <ezi:postalcode>34245</ezi:postalcode>
            <ezi:countrycode>AU</ezi:countrycode>
            <ezi:email>[email protected]</ezi:email>
            <ezi:phone>89549854</ezi:phone>
        </ezi:orderaddress>
        </ezi:orderaddresses>

除了上述两个“orderaddress”标签的格式外,还可以有一个,例如:

      <ezi:orderaddresses>
        <ezi:orderaddress>
            <ezi:addresstype>billing</ezi:addresstype>
            <ezi:name>Jason Fonseca</ezi:name>
            <ezi:companyname>Cyber</ezi:companyname>
            <ezi:address1>8 springstein</ezi:address1>
            <ezi:division>NT</ezi:division>
            <ezi:postalcode>34245</ezi:postalcode>
            <ezi:countrycode>AU</ezi:countrycode>
            <ezi:email>[email protected]</ezi:email>
            <ezi:phone>89549854</ezi:phone>
            <ezi:mobilephone>984590598</ezi:mobilephone>
        </ezi:orderaddress>
        </ezi:orderaddresses>

我发现,当使用简单的 xml 来解释这一点时,在第一个实例中,我得到以下内容:

 [orderaddresses] => SimpleXMLElement Object
    (
        [orderaddress] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [addresstype] => billing
                        [name] => Jason Fonseca
                        [companyname] => Cyber
                        [address1] => 8 springstein
                        [division] => NT
                        [postalcode] => 34245
                        [countrycode] => AU
                        [email] => [email protected]
                        [phone] => 89549854
                        [mobilephone] => 984590598
                    )

                [1] => SimpleXMLElement Object
                    (
                        [addresstype] => shipping
                        [name] => Jason Fonseca
                        [companyname] => Cyber
                        [address1] => 8 springstein
                        [division] => NT
                        [postalcode] => 34245
                        [countrycode] => AU
                        [email] => [email protected]
                        [phone] => 89549854
                    )

            )

    )

并且在第二个例子中,我得到了这个:

   [orderaddresses] => SimpleXMLElement Object
        (
            [orderaddress] => SimpleXMLElement Object
                (
                    [addresstype] => billing
                    [name] => Jason Fonseca
                    [companyname] => Cyber
                    [address1] => 8 springstein
                    [division] => NT
                    [postalcode] => 34245
                    [countrycode] => AU
                    [email] => [email protected]
                    [phone] => 89549854
                    [mobilephone] => 984590598
                )

        )

敏锐的观察者会注意到,现在如果我尝试访问 orderaddresses->orderaddress 这将具有不同的结构,具体取决于是否有两个(或更多)地址或只有一个地址。 示例 2 它是一个数字索引数组,示例二它是一个关联对象。

为了标准化这一点,我使用了一些如下所示的代码:(

if(!isset($content['orderlines']['orderline'][0]))
    {
        $temp = $content['orderlines']['orderline'];
        unset($content['orderlines']['orderline']);
        $content['orderlines']['orderline'][0] =$temp;
    }

您可以忽略我在这里使用关联数组的事实,我有一个执行转换的例程,但我已经检查过并且该例程没有改变结果)。

我的问题是,我该如何正确解释这些数据?每次我尝试访问订单地址和订单行时都有那段代码真的很混乱。难道就没有更好的办法吗?

我突然想到要编辑我的转换例程来处理这个问题。如果您认为这是正确的方法,请告诉我,但我心里想,这个问题肯定会经常出现,并且一定有人有一个聪明而简短的解决方案?

谢谢

I've got an XML document that gives me addresses. Here's an excerpt:

<ezi:orderaddresses>
        <ezi:orderaddress>
            <ezi:addresstype>billing</ezi:addresstype>
            <ezi:name>Jason Fonseca</ezi:name>
            <ezi:companyname>Cyber</ezi:companyname>
            <ezi:address1>8 springstein</ezi:address1>
            <ezi:division>NT</ezi:division>
            <ezi:postalcode>34245</ezi:postalcode>
            <ezi:countrycode>AU</ezi:countrycode>
            <ezi:email>[email protected]</ezi:email>
            <ezi:phone>89549854</ezi:phone>
            <ezi:mobilephone>984590598</ezi:mobilephone>
        </ezi:orderaddress>
        <ezi:orderaddress>
            <ezi:addresstype>shipping</ezi:addresstype>
            <ezi:name>Jason Fonseca</ezi:name>
            <ezi:companyname>Cyber</ezi:companyname>
            <ezi:address1>8 springstein</ezi:address1>
            <ezi:division>NT</ezi:division>
            <ezi:postalcode>34245</ezi:postalcode>
            <ezi:countrycode>AU</ezi:countrycode>
            <ezi:email>[email protected]</ezi:email>
            <ezi:phone>89549854</ezi:phone>
        </ezi:orderaddress>
        </ezi:orderaddresses>

In addition to the above format of two "orderaddress" tags, there can also be one, for example:

      <ezi:orderaddresses>
        <ezi:orderaddress>
            <ezi:addresstype>billing</ezi:addresstype>
            <ezi:name>Jason Fonseca</ezi:name>
            <ezi:companyname>Cyber</ezi:companyname>
            <ezi:address1>8 springstein</ezi:address1>
            <ezi:division>NT</ezi:division>
            <ezi:postalcode>34245</ezi:postalcode>
            <ezi:countrycode>AU</ezi:countrycode>
            <ezi:email>[email protected]</ezi:email>
            <ezi:phone>89549854</ezi:phone>
            <ezi:mobilephone>984590598</ezi:mobilephone>
        </ezi:orderaddress>
        </ezi:orderaddresses>

What I find is that when using simple xml to interpret this, in the first instance, I get the following:

 [orderaddresses] => SimpleXMLElement Object
    (
        [orderaddress] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [addresstype] => billing
                        [name] => Jason Fonseca
                        [companyname] => Cyber
                        [address1] => 8 springstein
                        [division] => NT
                        [postalcode] => 34245
                        [countrycode] => AU
                        [email] => [email protected]
                        [phone] => 89549854
                        [mobilephone] => 984590598
                    )

                [1] => SimpleXMLElement Object
                    (
                        [addresstype] => shipping
                        [name] => Jason Fonseca
                        [companyname] => Cyber
                        [address1] => 8 springstein
                        [division] => NT
                        [postalcode] => 34245
                        [countrycode] => AU
                        [email] => [email protected]
                        [phone] => 89549854
                    )

            )

    )

And in the second instance, I get this:

   [orderaddresses] => SimpleXMLElement Object
        (
            [orderaddress] => SimpleXMLElement Object
                (
                    [addresstype] => billing
                    [name] => Jason Fonseca
                    [companyname] => Cyber
                    [address1] => 8 springstein
                    [division] => NT
                    [postalcode] => 34245
                    [countrycode] => AU
                    [email] => [email protected]
                    [phone] => 89549854
                    [mobilephone] => 984590598
                )

        )

The Keen observer will notice that now if I try to access
orderaddresses->orderaddress that this would have a different structure depending on whether there are two addresses (or more) or just one address.
Example 2 it is a numerically index array, example two it is a an associative object.

To standardise this, I've used some code that looks like this :

if(!isset($content['orderlines']['orderline'][0]))
    {
        $temp = $content['orderlines']['orderline'];
        unset($content['orderlines']['orderline']);
        $content['orderlines']['orderline'][0] =$temp;
    }

(you can ignore the fact that i'm using associative arrays here, i've got a routine that performs a conversion but I've checked and this routine does not change the result).

My question is, how am I supposed to correctly inteperet this data? Having that slice of code everytime I try to access the orderaddress and order lines is really messy. Isn't there a better way?

The thought of editing my conversion routine to handle this has crossed my mind. If you think that is the way to go, then let me know, but I thought to myself surely this problem must come up a lot and someone must have a smart, and brief solution to it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

日久见人心 2024-10-12 17:24:54

根据 simplexml 文档,您应该始终能够迭代over 和 subscript 属性,即使只有其中之一,这可能是实现您想要的效果的“最佳”方式。例如:

for($xml->thingThereMayBeOneOrMoreOf as $item) {
  //handle $item
}

或者,如果您只需要处理第一项

$item=$xml->thingThereMayBeOneOrMoreOf[0];
//handle $item

另外,根据文档,这些属性实际上不是数组,而是可访问(可下标)和可迭代的属性,只是 var_dump 等会将它们转换为/将它们视为数组倾销的目的。

我还没有真正测试过这个,ymmv。

According to the documentation for simplexml you should always be able to iterate over and subscript properties even if there is only one of them, this may be the 'best' way of achieving what you want. e.g. :

for($xml->thingThereMayBeOneOrMoreOf as $item) {
  //handle $item
}

or, if you only need to handle the first item

$item=$xml->thingThereMayBeOneOrMoreOf[0];
//handle $item

Also, according to the documentation these properties are not in fact arrays, but accessible (subscriptable) and iterable properties, just that var_dump etc will convert them to / treat them as arrays for the purposes of dumping.

I haven't actually tested this, ymmv.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文