PHP XML Parser能够定义期望多个值(PHP XML Parser with ability to define expect multiple values)

编程入门 行业动态 更新时间:2024-10-24 16:23:39
PHP XML Parser能够定义期望多个值(PHP XML Parser with ability to define expect multiple values)

我们必须编写读者来定期输入xml数据,我们得到的常见错误是,如果你有一个可以有多个子节点的节点,并且在特定实例中只有一个子节点,那么生成的数组或对象将返回单个孩子而不是孩子阵列。

例如,解析:

<parent> <child name="Bill"> </child> <child name="John"> </child> </parent>

将返回一个对象或数组,如:

[child] => array( [0] => array( [name] => "Bill" ) [1] => array( [name] => "John" ) )

而解析:

<parent> <child name="John"> </child> </parent>

将返回:

[child] => array( [name] => "John" )

我知道,根据具体情况,我们可以手动检查这些事情:

if(isset($ parent ['child'] ['name']))$ parent ['child'] = array($ parent ['child']);

但这是你必须记住要做的事情,它经常出现在bug报告中。 许多这些xml源已经有WSDL规范或DTD,它们确定了一个元素可能有多次出现,如下所示,所以我们需要的信息已经存在,但我们使用的解析器似乎都没有足够的智能来使用这些信息时构造结果。

<s:complexType name="parent"> <s:sequence> <s:element minOccurs="0" maxOccurs="unbounded" name="child" nillable="true" type="tns:child"/> </s:sequence> </s:complexType> <s:complexType name="child"> <s:complexContent mixed="false"> <s:sequence> <s:element minOccurs="1" maxOccurs="1" name="name" type="s:string"/> </s:sequence> </s:extension> </s:complexContent> </s:complexType>

是否有一个更聪明的xml解析器,其解释数据,知道一组元素何时是基于DTD,XSD,WSDL的列表,并始终返回值数组而不是使用最佳猜测来折叠单个值,或者是否可以将解析器设置为始终使每个子节点成为一个数组,虽然编码时有点笨重,但意味着无论元素数量多少,结构至少始终保持一致。

We have to write readers to input xml data on a regular basis, and a common bug we get is where, if you have a node that can have multiple children, and in a particular instance there's only 1 child, then the resulting array or object will return the single child rather than array of children.

For example, parsing:

<parent> <child name="Bill"> </child> <child name="John"> </child> </parent>

Will return an object or array like:

[child] => array( [0] => array( [name] => "Bill" ) [1] => array( [name] => "John" ) )

Whereas parsing:

<parent> <child name="John"> </child> </parent>

Will return:

[child] => array( [name] => "John" )

I know that, on a case by case basis, we can check these things manually like:

if( isset( $parent[ 'child' ][ 'name' ] ) ) $parent[ 'child' ] = array( $parent[ 'child' ] );

But it's something you have to remember to do all the time and it pops up often in bug reports. Many of these xml sources already have WSDL specifications or DTD's which do specify that an element may have multiple occurrences, as below, so the information we need is already there but none of the parsers we use seem to be smart enough to use that information when structuring the result.

<s:complexType name="parent"> <s:sequence> <s:element minOccurs="0" maxOccurs="unbounded" name="child" nillable="true" type="tns:child"/> </s:sequence> </s:complexType> <s:complexType name="child"> <s:complexContent mixed="false"> <s:sequence> <s:element minOccurs="1" maxOccurs="1" name="name" type="s:string"/> </s:sequence> </s:extension> </s:complexContent> </s:complexType>

Is there an xml parser available that is smarter with its interpretation of data that knows when a set of elements is a list based on the DTD, XSD, WSDL, and always returns an array of values instead of using best guesses to collapse single values, or is it possible to set the parser to always make every child node into an array which, while a bit unwieldy when coding, means the structure is at least always consistent regardless of the number of elements.

最满意答案

Soap扩展具有SOAP_SINGLE_ELEMENT_ARRAYS功能选项。

The Soap extension has a SOAP_SINGLE_ELEMENT_ARRAYS feature option.

更多推荐

本文发布于:2023-07-31 01:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   定义   XML   Parser   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!