Moving some code from a PHP 5.2.6 / Windows environment to a 5.2.0 / Linux environment, I somehow lost access to a plain text node within a SimpleXML Object. On a var_dump of $xml_node, a [0] element was shown as the string '12'. However, $xml_node[0] was evaluating NULL in 5.2.0. You can see below the code change I made, pulling my data out of the raw XML with a regular expression. Hope this is useful to someone.
//In some versions of PHP it seems we cannot access the [0] element of a SimpleXML Object. Doesn't work in 5.2.0:
//$count = $xml_node[0];
//grab the raw XML:
$count = ($xml_node->asXML());
//pull out the number between the closing and opening brace of the xml:
$count = preg_replace('/.*>(\d*)<.*/', '$1', $count);
SimpleXML
- 導入
- インストール/設定
- 定義済み定数
- 例
- SimpleXML 関数
- SimpleXMLElement->addAttribute — SimpleXML 要素に属性を追加する
- SimpleXMLElement->addChild — XML ノードに子要素を追加する
- SimpleXMLElement->asXML — SimpleXML 要素に基づき整形式の XML 文字列を返す
- SimpleXMLElement->attributes — 要素の属性を定義する
- SimpleXMLElement->children — 指定したノードの子ノードを見付ける
- SimpleXMLElement->__construct — 新しい SimpleXMLElement オブジェクトを作成する
- SimpleXMLElement->getDocNamespaces — ドキュメントで宣言されている名前空間を返す
- SimpleXMLElement->getName — XML 要素の名前を取得する
- SimpleXMLElement->getNamespaces — ドキュメントで使用している名前空間を返す
- SimpleXMLElement->registerXPathNamespace — 次の XPath クエリ用の prefix/ns コンテキストを作成する
- SimpleXMLElement->xpath — XML データに Xpath クエリを実行する
- simplexml_import_dom — DOM ノードから SimpleXMLElement オブジェクトを取得する
- simplexml_load_file — XMLファイルをパースし、オブジェクトに代入する
- simplexml_load_string — XML 文字列をオブジェクトに代入する
SimpleXML
gwhitescarver at yahoo dot com
12-Sep-2008 04:02
12-Sep-2008 04:02
charlie at blankcanvasstudios dot com
15-Aug-2008 02:21
15-Aug-2008 02:21
Optimizing aalaap at gmail dot com's php
<?php
function is_rss($feedxml) {
@$feed = simplexml_load_string($feedxml);
return ($feed->channel->item)?true:false;
}
function is_atom($feedxml) {
@$feed = new SimpleXMLElement($feedxml);
($feed->entry):true:false;
}
?>
aalaap at gmail dot com
30-Jun-2008 09:58
30-Jun-2008 09:58
Here are two quick and dirty functions that use SimpleXML to detect if a feed xml is RSS or ATOM:
<?php
function is_rss($feedxml) {
@$feed = new SimpleXMLElement($feedxml);
if ($feed->channel->item) {
return true;
} else {
return false;
}
}
function is_atom($feedxml) {
@$feed = new SimpleXMLElement($feedxml);
if ($feed->entry) {
return true;
} else {
return false;
}
}
?>
The functions take in the full text feed (retrieved via cURL, for example) and return a true or a false based on the result.
