开发者

Using XML node names with hyphens in PHP [duplicate]

This question already has answers here: SimpleXML Reading node with a hyphenated name (2 answers) Closed 8 years ago.

I am trying to extract some data from开发者_开发百科 XML but when I execute the following I get a

Warning: Invalid argument supplied for foreach() in ...

Code Example:

foreach ($xml->custom-field-value as $milestone)
{
    ...     
}

It works fine for node names that are single words so I am guessing that it doesn't like the hyphens. Do I need to escape them and if so how?


From PHP manual:

Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

In your case you do:

$xml->{'custom-field-value'}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜