开发者

How we insert 'lft' and 'rgt' values for nested-set model for a data that we are receiving from other source?

I have to manage some hierarchical data for my web application. The data consist of messages of four types. I decided to use nested-list model because there can be arbitrary number of child nodes in the data. The data that I need to store is coming from an external source, how do I add values to the 'lft' & 'rgt' fields for it to be inserted into mySQL.

EDITED This is how I display messages(timeline) that my application fetches from Twitter using curl.

foreach ($xml->entry as $status) {
       echo'<li>'.$status->content.'</li>';
   }
开发者_开发知识库

Now in spite of direct printing, I need to store the contents of '$status->content' into database so that I can manipulate those i.e print in a parent-child hierachy.


One possibility is to write a recursive function or a procedure that goes through the data set to be imported.

Everything depends on the structure of the other source. If it's a classical parent id hierarchy, then in the Java world you could do something in the spirit of:

public class Importer {

    public static db = new Database();

    public static void processOtherSource(ArrayList<Object> data, int lft) {
        int siblings = 0;
        for (Object element : data) {
            int children = 0;
            if (element.getClass().getName().equals("ArrayList")) {
                children = Importer.processOtherSource(element, lft+1, rgt);
            }
            int rgt = lft + children * 2 + 1;
            Importer.db.insert(element, lft, rgt);
            siblings++;
        }
        return siblings;
    }

    public void main(String[] args) {
        Importer.processOtherSource(dataToImport, 1);
    }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜