예제 #1
0
void node_dropped(DomNode* node)
{
    map<DomNode*, PyObject*>::iterator iter = dom_relation.find(node);
    if (iter != dom_relation.end())
    {
        PyObject* py_node = iter->second;
        python.invoke_object(py_node, "drop_tree");
    }
}
예제 #2
0
// build dom tree from python lxml dom tree.
DomNode* build_dom_tree(DomNode* parent, PyObject* node, map<DomNode*, PyObject*>& relation)
{
    const char* tag = python.get_attr_string(node, "tag");
    const char* inner_text = python.get_attr_string(node, "text");
    const char* inner_tail = python.get_attr_string(node, "tail");
    string text;
    if (inner_text != NULL)
    {
        text.append(inner_text);
    }

    if (inner_tail != NULL)
    {
        if (parent != NULL)
        {
            parent->append_text(inner_tail);
        }
    }

    DomNode* dom_node = new DomNode(tag, text);
    relation[dom_node] = node;

    //PyObject* parent = python.invoke_object(node, "getparent");
    FILE* fp = fopen("object.txt", "w+");
    PyObject_Print(node, fp, Py_PRINT_RAW);
    fclose(fp);
    PyObject* children = python.invoke_object(node, "getchildren");
    if (children != NULL)
    {
        int count = PyList_GET_SIZE(children);
        for (int i = 0; i < count; ++i)
        {
            PyObject* child = PyList_GET_ITEM(children, i);
            if (PyObject_IsInstance(child, tag_type) != 1)
            {
                continue;
            }

            DomNode* child_node = build_dom_tree(dom_node, child, relation);
            dom_node->append_child(child_node);
        }
    }

    dom_node->add_attribute("class", get_dom_attr(node, "class"));
    dom_node->add_attribute("id", get_dom_attr(node, "id"));

    return dom_node;
}