XMLからJSONへの変換
simplexml_load_stringを使ってXMLからJSONに変換
PHP
// XMLを読み込む
$xml = file_get_contents('foo.xml');
// XML文字列をオブジェクトに代入
$obj = simplexml_load_string($xml);
// 値をJSON形式にして返す
$json = json_encode($obj);
// JSON形式から配列にデコード
$arr = json_decode($json, TRUE);
$jsonの内容(元のXML)
JSON
{"comment":{},"generator":"Google Reader","id":"tag:google.com,2005:reader\/user\/07280577793685799651\/state\/com.google\/alerts\/14593991743914576000\n ","link":[{"@attributes":{"rel":"hub","href":"http:\/\/pubsubhubbub.appspot.com\/"}},{"@attributes":{"rel":"self","href":"http:\/\/www.google.com\/alerts\/feeds\/07280577793685799651\/14593991743914576000"}}],"title":"Google \u30a2\u30e9\u30fc\u30c8 - \u79c1\u7684\u96d1\u9332","updated":"2012-11-06T00:00:00Z","entry":{"id":"tag:google.com,2005:reader\/item\/e276e2a793830e88\n ","title":"Fiddler HTTP \u30c7\u30d0\u30c3\u30ac | <b>\u79c1\u7684\u96d1\u9332<\/b>","published":"2012-11-06T00:00:00Z","updated":"2012-11-06T00:00:00Z","link":{"@attributes":{"rel":"alternate","href":"http:\/\/www.google.com\/url?sa=X&q=http:\/\/php.o0o0.jp\/article\/2967174473163794&ct=ga&cad=CAcQARgBIAEoATAAOABAk9zdhAVIAlgAYgVqYS1VUw&cd=KRVMUHPLtFs&usg=AFQjCNH0tbwBUkwdniLlTEla6_VZjXkd2g","type":"text\/html"}},"content":"Fiddler HTTP \u30c7\u30d0\u30c3\u30ac:Fiddler\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3068\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9 -\n <b>\u79c1\u7684\u96d1\u9332<\/b> PHP\u3092\u3088\u304f \u66f8\u3044\u3066\u3044\u308b\u4eba\u306e\u5099\u5fd8\u9332.<br><a\n style=\"color:#228822\"\n href=\"http:\/\/www.google.com\/url?sa=X&q=http:\/\/php.o0o0.jp\/article\/2967174473163794&ct=ga&cad=CAcQARgBIAEoBDAAOABAk9zdhAVIAlgAYgVqYS1VUw&cd=KRVMUHPLtFs&usg=AFQjCNH0tbwBUkwdniLlTEla6_VZjXkd2g\"\n title=\"http:\/\/php.o0o0.jp\/article\/2967174473163794\">php.o0o0.jp\/article\/2967174473163794<\/a>\n ","author":{"name":"(\u6295\u7a3f\u8005\u4e0d\u660e)"},"source":{"id":"tag:google.com,2005:reader\/user\/07280577793685799651\/state\/com.google\/alerts\/14593991743914576000\n ","title":"Google \u30a2\u30e9\u30fc\u30c8 - \u79c1\u7684\u96d1\u9332"}}}Array
$arrの内容
Array
(
[comment] => Array
(
)
[generator] => Google Reader
[id] => tag:google.com,2005:reader/user/07280577793685799651/state/com.google/alerts/14593991743914576000
[link] => Array
(
[0] => Array
(
[@attributes] => Array
(
[rel] => hub
[href] => http://pubsubhubbub.appspot.com/
)
)
[1] => Array
(
[@attributes] => Array
(
[rel] => self
[href] => http://www.google.com/alerts/feeds/07280577793685799651/14593991743914576000
)
)
)
[title] => Google アラート - 私的雑録
[updated] => 2012-11-06T00:00:00Z
[entry] => Array
(
[id] => tag:google.com,2005:reader/item/e276e2a793830e88
[title] => Fiddler HTTP デバッガ | <b>私的雑録</b>
[published] => 2012-11-06T00:00:00Z
[updated] => 2012-11-06T00:00:00Z
[link] => Array
(
[@attributes] => Array
(
[rel] => alternate
[href] => http://www.google.com/url?sa=X&q=http://php.o0o0.jp/article/2967174473163794&ct=ga&cad=CAcQARgBIAEoATAAOABAk9zdhAVIAlgAYgVqYS1VUw&cd=KRVMUHPLtFs&usg=AFQjCNH0tbwBUkwdniLlTEla6_VZjXkd2g
[type] => text/html
)
)
[content] => Fiddler HTTP デバッガ:Fiddlerのインストールと基本的な使い方 -
<b>私的雑録</b> PHPをよく 書いている人の備忘録.<br><a
style="color:#228822"
href="http://www.google.com/url?sa=X&q=http://php.o0o0.jp/article/2967174473163794&ct=ga&cad=CAcQARgBIAEoBDAAOABAk9zdhAVIAlgAYgVqYS1VUw&cd=KRVMUHPLtFs&usg=AFQjCNH0tbwBUkwdniLlTEla6_VZjXkd2g"
title="http://php.o0o0.jp/article/2967174473163794">php.o0o0.jp/article/2967174473163794</a>
[author] => Array
(
[name] => (投稿者不明)
)
[source] => Array
(
[id] => tag:google.com,2005:reader/user/07280577793685799651/state/com.google/alerts/14593991743914576000
[title] => Google アラート - 私的雑録
)
)
)