bool QDomNodeProto:: isEntity() const { QDomNode *item = qscriptvalue_cast<QDomNode*>(thisObject()); if (item) return item->isEntity(); return false; }
void MainWindow::on_btn_all_clicked() { ui->listWidget->clear(); QFile file("my.xml"); if(!file.open(QIODevice::ReadOnly)) { return; } QDomDocument doc; QString errstr; int errline; int errrow; if(!doc.setContent(&file, true, &errstr, &errline, &errrow)) { qDebug() << errstr<< errline<< errrow; file.close(); return; } file.close(); QDomElement domelem = doc.documentElement(); QDomNode n = domelem.firstChild(); if(!n.isNull()) { if(n.isEntity()) { QDomElement e = n.toElement(); ui->listWidget->addItem(e.tagName() +e.attribute(tr("编号"))); QDomNodeList list = e.childNodes(); for(int i=0; i<list.count(); i++) { QDomNode node = list.at(i); if(node.isElement()) { ui->listWidget->addItem(" " + node.toElement().tagName() + " :" + node.toElement().text()); } } } n = n.nextSibling(); } }
void ReadAndWriteXML::parseEntry(const QDomElement &element, QTreeWidgetItem *parent, int parentLevel) { // item->setText(0, element.attribute("term")); QDomNode node = element.firstChild(); while (!node.isNull()) { //qDebug() << "DomParser::parseEntry node " << node.toElement().tagName(); // qDebug() << "DomParser::parseEntry parsing node " << node.nodeName(); if (node.isElement()) { //qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is element "; QTreeWidgetItem *childitem=new QTreeWidgetItem(parent, QStringList(QString("Type: %1").arg(node.nodeName()))); parseEntry(node.toElement(), childitem,++parentLevel); QString _attr=""; if (node.hasAttributes()) { childitem=new QTreeWidgetItem(childitem, QStringList(QString("Attributes node %1").arg(node.nodeName()))); parseAttribute(node.attributes(), childitem, parentLevel); } node = node.nextSibling(); continue; } if (node.isCDATASection()) { //qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is CDATASection --" << node.toText().data() << "--"; new QTreeWidgetItem(parent, QStringList(QString("Type: %1 Value: %2").arg(node.nodeName(),node.toText().data()))); node = node.nextSibling(); continue; } if (node.isComment()) { //qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is comment --" << node.toText().data() << "--"; new QTreeWidgetItem(parent, QStringList(QString("Type: %1 Value: %2").arg(node.nodeName(),node.toText().data()))); node = node.nextSibling(); continue; } if (node.isText()) { //qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " text --" << node.toText().data() << "--"; new QTreeWidgetItem(parent, QStringList(QString("Type: %1 Value: %2").arg(node.nodeName(),node.toText().data()))); node = node.nextSibling(); continue; } if (node.isDocument()) { //qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is document "; QTreeWidgetItem *childitem=new QTreeWidgetItem(parent, QStringList(QString("Type: %1").arg(node.nodeName()))); parseEntry(node.toElement(), childitem,++parentLevel); node = node.nextSibling(); continue; } if (node.isDocumentType()) { qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is document type"; QTreeWidgetItem *childitem=new QTreeWidgetItem(parent, QStringList(QString("Type: %1").arg(node.nodeName()))); parseEntry(node.toElement(), childitem,++parentLevel); node = node.nextSibling(); continue; } if (node.isDocumentFragment()) { qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is document fragment"; QTreeWidgetItem *childitem=new QTreeWidgetItem(parent, QStringList(QString("Type: %1").arg(node.nodeName()))); parseEntry(node.toElement(), childitem,++parentLevel); node = node.nextSibling(); continue; } if (node.isEntity() || node.isEntityReference () || node.isNotation () || node.isProcessingInstruction ()) { qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is not supported"; node = node.nextSibling(); continue; } node = node.nextSibling(); } }