MyXmlReader::MyXmlReader(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.openBtn, SIGNAL(clicked()), this, SLOT(openFile()));
ui.treeWid->setColumnCount(2);
ui.treeWid->setColumnWidth(0,400);
}
MyXmlReader::~MyXmlReader()
{
}
void MyXmlReader::openXml(QString fileName)
{
QFile file(fileName);
if (file.open(QIODevice::ReadOnly))
{
QDomDocument dom("WCM");
if (dom.setContent(&file))
{
ui.treeWid->clear();
QDomElement docElem = dom.documentElement();
listDom(docElem, NULL);
}
}
file.close();
}
void MyXmlReader::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open XML File"), "C:/", tr("XML Files (*.xml)"));
if (!fileName.isEmpty())
{
openXml(fileName);
}
}
void MyXmlReader::listDom(QDomElement &docElem, QTreeWidgetItem *pItem)
{
QDomNode node = docElem.firstChild();
if (node.toElement().isNull())
{
pItem->setText(1, docElem.text());
}
while (!node.isNull())
{
QDomElement element = node.toElement();
if (!element.isNull())
{
QTreeWidgetItem *item;
if (pItem)
item = new QTreeWidgetItem(pItem);
else
item = new QTreeWidgetItem(ui.treeWid);
item->setText(0, element.tagName());
listDom(element, item);
if (pItem)
pItem->addChild(item);
else
ui.treeWid->addTopLevelItem(item);
}
node = node.nextSibling();
}
}
版权归属:
三味线
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区