从 PHP 中的 SimpleXMLElement 获取 iso8601.time 标签
我有以下功能。我想添加对 iso8601 时间格式的支持,但我无法让它工作。因为我在 php 中无法执行 (string)$tag->iso8601.time
。有没有办法获取 iso8601.time 元素? 标签
是一个SimpleXMLElement
。
private function _tagToPhpType($tag) {
/*
* <i4> or <int> four-byte signed integer -12
* <boolean> 0 (false) or 1 (true) 1
* <string> string hello world
* <double> double-precision signed floating point number -12.214
* <dateTime.iso8601> date/time 19980717T14:08:55
* <base64> base64-encoded binary eW91IGNhbid0IHJlYWQgdGhpcyE=
*
* Source: http://www.xmlrpc.com/spec
*/
if(!empty($tag->string)) {
return (string)$tag->string;
}
elseif(!empty($tag->int)) {
return (int)$tag->int;
}
elseif(!empty($tag->i4)) {
return (int)$tag->i4;
}
elseif(!empty($tag->boolean)) {
return (bool)$tag->boolean;
}
elseif(!empty($tag->double)) {
return (double)$tag->double;
}
elseif(!empty($tag->base64)) {
// @todo: Decode BASE64
return (int)$tag->base64;
} // @todo: Add iso8601 time type
else {
return (string)$tag;
}
}
I have the function bellow. I want to add support for iso8601 time format but I just can't get it to work. Since I in php can't do (string)$tag->iso8601.time
. Is there a way to get the iso8601.time element? The tag
is a SimpleXMLElement
.
private function _tagToPhpType($tag) {
/*
* <i4> or <int> four-byte signed integer -12
* <boolean> 0 (false) or 1 (true) 1
* <string> string hello world
* <double> double-precision signed floating point number -12.214
* <dateTime.iso8601> date/time 19980717T14:08:55
* <base64> base64-encoded binary eW91IGNhbid0IHJlYWQgdGhpcyE=
*
* Source: http://www.xmlrpc.com/spec
*/
if(!empty($tag->string)) {
return (string)$tag->string;
}
elseif(!empty($tag->int)) {
return (int)$tag->int;
}
elseif(!empty($tag->i4)) {
return (int)$tag->i4;
}
elseif(!empty($tag->boolean)) {
return (bool)$tag->boolean;
}
elseif(!empty($tag->double)) {
return (double)$tag->double;
}
elseif(!empty($tag->base64)) {
// @todo: Decode BASE64
return (int)$tag->base64;
} // @todo: Add iso8601 time type
else {
return (string)$tag;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如手册 (SimpleXML 基本用法) 和相关示例 (#3) 中所述:
因此,在您的情况下
$tag->{'dateTime.iso8601'}
将获得
元素。(您的
iso8601.time
与代码中的注释不匹配,但如果您需要获取该标签,那么从上面的答案应该很容易算出。)As noted in the manual (SimpleXML basic usage) and the associated example (#3):
So in your case
$tag->{'dateTime.iso8601'}
will get an<dateTime.iso8601>
element.(Your
iso8601.time
doesn't match the comment in the code, though if you need to get that tag then it should be easy to work out from the answer above.)