PDA

View Full Version : Need QDomDocument Example



vajindarladdad
21st April 2009, 12:26
Hello all experts ,
Can somebody provide me an example to create a xml file like this?
I am able to create a single node , but getting problem while inserting other node to it.

<ui name="UI EDITOR" >
<ribbon name="ribbon" >
<tab title="simulation" >
<panel>
<button tooltip="scFileOpen('/home/vajindar/Project/auto1.tin')" id="1" />
<button tooltip="scFileOpen('/home/vajindar/Project/auto2.tin')" id="2" />
<button tooltip="scFileOpen('/home/vajindar/Project/auto3.tin')" id="3" />
.
.
.
.
.
.
</panel>
</tab>
</ribbon>
</ui>

spirit
21st April 2009, 13:05
try this


#include <QtXml>

int main(int argc, char **argv)
{
Q_UNUSED(argc);
Q_UNUSED(argv);

QFile file("text.xml");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return 0;

QDomDocument doc;
QDomElement ui = doc.createElement("ui");
ui.setAttribute("name", "UI EDITOR");

QDomElement ribbon = doc.createElement("ribbon");
ribbon.setAttribute("name", "ribbon");
ui.appendChild(ribbon);

QDomElement tab = doc.createElement("tab");
tab.setAttribute("title", "simulation");
ribbon.appendChild(tab);

QDomElement panel = doc.createElement("panel");
tab.appendChild(panel);

for (int i = 0; i < 10; ++i) {
QDomElement button = doc.createElement("button");
button.setAttribute("tooltip", QString("scFileOpen('/home/vajindar/Project/auto%1.tin')").arg(i + 1));
button.setAttribute("id", i + 1);
panel.appendChild(button);
}


doc.appendChild(ui);
QTextStream out(&file);
out << doc.toString();

return 0;
}

vajindarladdad
21st April 2009, 13:20
Thank your Sir for your reply ..
But i don't want to create a file at one time ..
i want to insert the button node dynamically..
please give me a Hint

spirit
21st April 2009, 13:29
ok, add a method which will create QDomElement for given object
something like this


QDomElement Serializator::toXml(const MyObject &obj, QDomDocument &doc) const
{
QDomElement element = doc.createElement("...");
element.setAttribute(..., ...);
...
return element;
}


then


QDomDocument Serializator::generateDocument() const
{
QDomDocument doc;
QDomElement ui = doc.createElement("ui");
QList<MyObject> objects = this->objects();//method which return a list of objects
foreach(const MyObject &object, objects)
ui.appendChild(toXml(object, doc));

doc.appendChild(ui);
return doc;
}


or even better it's create toXml method in all needed classe,
of course if you implement them by yourself in this case a code should be like this


QDomDocument Serializator::generateDocument() const
{
QDomDocument doc;
QDomElement ui = doc.createElement("ui");
QList<MyObject> objects = this->objects();//method which return a list of objects
foreach(const MyObject &object, objects)
ui.appendChild(object.toXml(doc));

doc.appendChild(ui);
return doc;
}

vajindarladdad
21st April 2009, 13:54
Sir , cann't we do it using insertBefore() and using insertAfter() functions.
Because i am trying to implement this using these fuctions.

Sir , could you post the simple example.
Using insertBefore() and insertAfter();