Reading is easy (this example, I should probably close the file.... oops)
{
qDebug
(qPrintable
(QString("Error: Cannot read file %1 %2").
arg(filename
).
arg(file.
errorString())));
return;
}
doc.setContent(&file);
QString filename = "C:/test.xml";
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
qDebug(qPrintable(QString("Error: Cannot read file %1 %2").arg(filename).arg(file.errorString())));
return;
}
QDomDocument doc;
doc.setContent(&file);
To copy to clipboard, switch view to plain text mode
Is your real concern related to processing? Consider this small example that traverses a DOM tree
{
while (!node.isNull())
{
if (node.isElement())
{
// Ignore attributes for now.
//qDebug(qPrintable(QString("DEBUG: <%1>").arg(node.toElement().tagName())));
os << "<" << node.toElement().tagName() << ">";
if (node.hasChildNodes())
{
pp(os, childNode);
}
// Print all children
//qDebug(qPrintable(QString("DEBUG: </%1>").arg(node.toElement().tagName())));
os << "</" << node.toElement().tagName() << ">";
}
else if (node.isText())
{
os << node.toText().data();
}
else
{
qDebug
(qPrintable
(QString("Unexpected node type:")));
//qDebug(qPrintable(QString("Unexpected node type: %1").arg((int)(node.NodeType()))));
}
node = node.nextSibling();
}
}
void pp(QTextStream& os, QDomNode node)
{
while (!node.isNull())
{
if (node.isElement())
{
// Ignore attributes for now.
//qDebug(qPrintable(QString("DEBUG: <%1>").arg(node.toElement().tagName())));
os << "<" << node.toElement().tagName() << ">";
if (node.hasChildNodes())
{
QDomNode childNode = node.firstChild();
pp(os, childNode);
}
// Print all children
//qDebug(qPrintable(QString("DEBUG: </%1>").arg(node.toElement().tagName())));
os << "</" << node.toElement().tagName() << ">";
}
else if (node.isText())
{
os << node.toText().data();
}
else
{
qDebug(qPrintable(QString("Unexpected node type:")));
//qDebug(qPrintable(QString("Unexpected node type: %1").arg((int)(node.NodeType()))));
}
node = node.nextSibling();
}
}
To copy to clipboard, switch view to plain text mode
The example ignores attributes. The head node that I would pass as an argument is found using doc.documentElement().
This snippet copies a node out of a DOM tree and removes the node. I then insert the node in a different location.
QString xmlOriginal
= "<xml><item><a>1</a></item><item><a>2</a></item></xml>";
doc.setContent(xmlOriginal);
QDomNode firstChild
= docElement.
firstChild();
QDomNode secondChild
= firstChild.
nextSibling();
QDomNode clonedNode
= secondChild.
cloneNode(true);
doc.documentElement().removeChild(secondChild);
doc.documentElement().insertBefore(clonedNode, firstChild);
os2 << "\nStream *****\n" << doc;
qDebug(qPrintable(s2));
QString xmlOriginal = "<xml><item><a>1</a></item><item><a>2</a></item></xml>";
QDomDocument doc;
doc.setContent(xmlOriginal);
QDomElement docElement = doc.documentElement();
QDomNode firstChild = docElement.firstChild();
QDomNode secondChild = firstChild.nextSibling();
QDomNode clonedNode = secondChild.cloneNode(true);
doc.documentElement().removeChild(secondChild);
doc.documentElement().insertBefore(clonedNode, firstChild);
QString s2;
QTextStream os2(&s2);
os2 << "\nStream *****\n" << doc;
qDebug(qPrintable(s2));
To copy to clipboard, switch view to plain text mode
Bookmarks