PDA

View Full Version : Need help for QDomDocument



vajindarladdad
22nd April 2009, 08:11
void Ribbon::WriteXml(int id ,QString location)
{
QFile file("xml/node.xml");
QDomDocument document ;

if(id==1) //creating ui , ribbon , tab , pabel , button node for the first time
{
QDomElement uiElementNode , documentElement , ribbonElementNode , tabElementNode , panelElementNode , buttonElementNode;
if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
qDebug("Error... in opening a file");
return ;
}

uiElementNode = document.createElement("ui");
uiElementNode.setAttribute("name", "UI EDITOR");
document.appendChild(uiElementNode);

ribbonElementNode = document.createElement("ribbon");
uiElementNode.appendChild(ribbonElementNode);

tabElementNode = document.createElement("tab");
ribbonElementNode.appendChild(tabElementNode);

panelElementNode = document.createElement("panel");
tabElementNode.appendChild(panelElementNode);

buttonElementNode = document.createElement("button");
panelElementNode.appendChild(buttonElementNode);

}
else // here we need to insert button node to the panel
{
if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
QMessageBox::warning(this, tr("Error"), "Error... in opening a file");
return ;
}

if( !document.setContent( &file ) )
{
QMessageBox::warning(this, tr("Error"), "Failed to parse the file into a DOM tree.");
return ;
}

QDomElement documentElement = document.documentElement(); //returns the root element
QDomNodeList elements = documentElement.elementsByTagName( "panel" ); //searching for the panel node
QDomElement panel = elements.at(0).toElement();
QDomElement newButton = document.createElement("button");//creating new button
panel.appendChild(newButton); //appending to the panel node

}
QTextStream out(&file);
out << document.toString(); //writting to a file
file.close();
}

Hello All experts this is my code to create the xml file.

I want result in following format
<ui name="UI EDITOR" >
<ribbon>
<tab/>
<panel>
<button/>
<button/>
</panel>
</ribbon>
</ui>

But i am getting result in this format
<ui name="UI EDITOR" >
<ribbon>
<tab/>
<panel>
<button/>
</panel>
</ribbon>
</ui>
<ui name="UI EDITOR" >
<ribbon>
<tab/>
<panel>
<button/>
<button/>
</panel>
</ribbon>
</ui>

Please tell me where i am commiting mistake

I WANT TO INSERT THE button NODE DYNAMICALLY

spirit
22nd April 2009, 08:20
why you can't just append a child?

vajindarladdad
22nd April 2009, 08:40
panel.appendChild(newButton); //appending to the panel node

Sir , i am appending a child .. but not getting the desired output, repetation of ui , ribbon tab...etc does occure

vajindarladdad
22nd April 2009, 08:46
In what other way i can do the same

spirit
22nd April 2009, 08:51
as I understand you need to add data between these tags


<ui name="UI EDITOR" >
<ribbon>
....
</ribbon>
<ui name="UI EDITOR" >

right?

why you can't create a method wich will generate whole xml at all?

vajindarladdad
22nd April 2009, 11:37
Sir if you could provide me an example code to do so ..
I would be very grateful.

vajindarladdad
22nd April 2009, 12:18
Thanks ... here is a code to do so......

[CODE ]
void Ribbon::WriteXml(int id ,QString location)
{
QFile file("xml/node.xml");
QDomDocument document ;

if(id==1)
{
QDomElement uiElementNode , documentElement , ribbonElementNode , tabElementNode , panelElementNode , buttonElementNode;
if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
qDebug("Error... in opening a file");
return ;
}

uiElementNode = document.createElement("ui");
uiElementNode.setAttribute("name", "UI EDITOR");
document.appendChild(uiElementNode);

ribbonElementNode = document.createElement("ribbon");
uiElementNode.appendChild(ribbonElementNode);

tabElementNode = document.createElement("tab");
ribbonElementNode.appendChild(tabElementNode);

panelElementNode = document.createElement("panel");
tabElementNode.appendChild(panelElementNode);

buttonElementNode = document.createElement("button");
buttonElementNode.setAttribute("id",QString::number(id));
buttonElementNode.setAttribute("tooltip",location);
panelElementNode.appendChild(buttonElementNode);

QTextStream out(&file);
out << document.toString(); //writting to a file
file.close();

}
else // here we need to insert button node to the panel
{
if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
QMessageBox::warning(this, tr("Error"), "Error... in opening a file");
return ;
}

if( !document.setContent( &file ) )
{
QMessageBox::warning(this, tr("Error"), "Failed to parse the file into a DOM tree.");
return ;
}
QDomElement documentElement = document.documentElement(); //returns the root element
QDomNodeList elements = documentElement.elementsByTagName( "panel" ); //searching for the panel node
QDomElement panel = elements.at(0).toElement();
QDomElement newButton = document.createElement("button");//creating new button
newButton.setAttribute("id",QString::number(id));
newButton.setAttribute("tooltip",location);
panel.appendChild(newButton); //appending to the panel node
file.close();

if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) //opening a file again to write the new content
{
QMessageBox::warning(this, tr("Error"), "Error... in opening a file");
return ;
}
QTextStream out(&file);
out << document.toString(); //writting to a file
file.close();
}
}

[ /CODE ]