我有一个xml对象。我可以通过这种方式循环来获取其属性的后续值。我需要将这些存储在父子关系中的数据库中。
foreach ($xml->entry as $status) {
echo $status->author->name.''$status->content.''.$status->id; }
树的深度不是有限的。此外,插入数据后按顺序检索数据(父子...)的示例查询也很棒。
修改 树的深度可以被认为是三个。 这是从Twitter返回的json的一个例子。
<script type="text/javascript"> var test_json = {\"id\":\"tag:search.twitter.com,2005:search\\/#DIYSe_F\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/search.twitter.com\\/search?q=%23DIYSe_F\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"application\\/atom+xml\",\"href\":\"http:\\/\\/search.twitter.com\\/search.atom?q=%23DIYSe_F&rpp=100\",\"rel\":\"self\"}},{\"@attributes\":{\"type\":\"application\\/opensearchdescription+xml\",\"href\":\"http:\\/\\/search.twitter.com\\/opensearch.xml\",\"rel\":\"search\"}},{\"@attributes\":{\"type\":\"application\\/atom+xml\",\"href\":\"http:\\/\\/search.twitter.com\\/search.atom?q=%23DIYSe_F&rpp=100&since_id=7750301532557312\",\"rel\":\"refresh\"}}],\"title\":\"#DIYSe_F - Twitter Search\",\"updated\":\"2010-11-24T22:20:44Z\",\"entry\":[{\"id\":\"tag:search.twitter.com,2005:7559269595488256\",\"published\":\"2010-11-24T22:20:44Z\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/twitter.com\\/_smir\\/statuses\\/7559269595488256\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"image\\/png\",\"href\":\"http:\\/\\/s.twimg.com\\/a\\/1289849896\\/images\\/default_profile_5_normal.png\",\"rel\":
\":\"image\"}}],\"title\":\"#DIYse_F HIE_STRUCT: HIERARCHICAL STRUCTURE: hierarchical structure to display \\nmessages of Functions and Qualities
types\",\"content\":\"<a href=\\\"http:\\/\\/search.twitter.com\\/search?q=%23DIYse_F\\\" onclick=\\\"pageTracker._setCustomVar(2, \'result_type\', \'recent\', 3);pageTracker._trackPageview(\'\\/intra\\/hashtag\\/#DIYse_F\');\\\"><b>#DIYse_F<\\/b><\\/a> HIE_STRUCT: HIERARCHICAL STRUCTURE: hierarchical structure to display \\nmessages of Functions and Qualities types\",\"updated\":\"2010-11-24T22:20:44Z\",\"author\":{\"name\":\"_smir (Smeer)\",\"uri\":\"http:\\/\\/twitter.com\\/_smir\"}},{\"id\":\"tag:search.twitter.com,2005:7552659368189952\",\"published\":\"2010-11-24T21:54:28Z\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/twitter.com\\/_smir\\/statuses\\/7552659368189952\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"image\\/png\",\"href\":\"http:\\/\\/s.twimg.com\\/a\\/1289849896\\/images\\/default_profile_5_normal.png\",\"rel\":\"image\"}}],\"title\":\"#DIYse_F SEND_MSG: users can send messages of four types i.e. \\n\\nFunction,Quality,Solution, and delivery\",\"content\":\"<a href=\\\"http:\\/\\/search.twitter.com\\/search?q=%23DIYse_F\\\" onclick=\\\"pageTracker._setCustomVar(2, \'result_type\', \'recent\', 3);pageTracker._trackPageview(\'\\/intra
\\/hashtag\\/#DIYse_F\');\\\"><b>#DIYse_F<\\/b><\\/a> SEND_MSG: users can send messages of four types i.e. \\n\\nFunction,Quality,Solution, and delivery\",\"updated\":\"2010-11-24T21:54:28Z\",\"author\":{\"name\":\"_smir
(Smeer)\",\"uri\":\"http:\\/\\/twitter.com\\/_smir\"}},{\"id\":\"tag:search.twitter.com,2005:7548895705956352\",\"published\":\"2010-11-24T21:39:31Z\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/twitter.com\\/Babar_Shahzad\\/statuses\\/7548895705956352\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"image\\/png\",\"href\":\"http:\\/\\/a1.twimg.com\\/profile_images\\/1090185625\\/29465_391454998679_533808679_3864564_6071800_n_normal.jpg\",\"rel\":\"image\"}}],\"title\":\"#DIYse_F READ_MSG: Users can read messages of all four types in \\n\\ndifferent windows\",\"content\":\"<a href=\\\"http:\\/\\/search.twitter.com\\/search?q=%23DIYse_F\\\" onclick=\\\"pageTracker._setCustomVar(2, \'result_type\', \'recent\', 3);pageTracker._trackPageview(\'\\/intra\\/hashtag\\/#DIYse_F\');\\\"><b>#DIYse_F<\\/b><\\/a> READ_MSG: Users can read messages of all four types in \\n\\ndifferent windows\",\"updated\":\"2010-11-24T21:39:31Z\",\"author\":{\"name\":\"Babar_Shahzad (Babar Shahzad Ch)\",\"uri\":\"http:\\/\\/twitter.com\\/Babar_Shahzad\"}}]};
答案 0 :(得分:1)
保存推文:
foreach ($xml->entry as $status) {
// REMEMBER SANITIZE THE XML VALUES!
$sql = "INSERT INTO
the_table (
msg
,msg_id
,parent_id
)
VALUES (
'{$status->content}'
,'{$status->id}'
,'{$status->in_reply_to_status_id}'
)";
}
此处提供了打印层次结构的递归函数:How can I convert a series of parent-child relationships into a hierarchical tree?使用msg_id
和parent_id
作为父ID的子项。
我只看到一个问题。如果您没有父推文,则不会显示此分支。