Re: Element creation



Elements and other nodes are supposed to belong to a document. I don't know if you can create "free-floating" elements, but even if it's possible, it's probably not a good idea.

My recommendation is that you let getAllNodes() build a document that contains the elements you want. It shall return xmlpp::Document*. Then use xmlpp::Node::import_node() to copy the elements to where you want them.

The example program http://git.gnome.org/browse/libxml++/tree/examples/import_node shows how to use import_node(), and http://git.gnome.org/browse/libxml++/tree/examples/dom_build shows how to build a document from scratch. The document it builds contains a lot of stuff that you won't need in your document.

Kjell

2012-06-12 22:58, Jayashree skrev:
Hi,

I need to create a xml document and save it to a file.
I create the xml using the below code.

xmlpp::Document doc;
xmlpp::Element* pRootEle = doc.create_root_node("root");

xmlpp::Element* pNode = pRootEle->add_child("node1");
pNode->add_child_text("node1 data");

But i need to create a list of Elements in a function and directly add it
as a child to the root node. some thing like below.

list<Element *> eleList = getAllNodes();
foreach(Element ele in eleList)
{
    pRootEle -> add_child(ele);
}

How can i achieve it. I don't want to pass the rootnode to the function.

regards,
Jaya



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]