使用QDomDocument失败读取XML文件..

时间:2017-12-11 11:55:42

标签: xml file parsing qdomdocument

作为创建地址簿的项目的一部分,我必须在XML文件中进行备份。

我使用QDomDoc等,写入功能正常,但xml文件的读取/解析在QDomDoc的settingContent失败。

我看了很多带有例子的论坛,但没有结果..

我的写作准则:

void MyMainWindow::SavePhoneBookXML()
{
QDomImplementation domImpl = QDomDocument().implementation();

QString sName = QFileDialog::getSaveFileName(this, tr("Save PhoneBook in Xml File"), "C:/ ...", tr("Fichier Xml (*.xml)"));
QString sPublicId = "Public ID";
QString sSystemId = "System ID";

QDomDocument domDoc(domImpl.createDocumentType(sName, sPublicId, sSystemId));

//Ajout de commentaires
domDoc.appendChild(domDoc.createComment("Ce fichier contient les contacts du carnet d'adresse"));
domDoc.appendChild(domDoc.createTextNode("\n"));

//Noeud Racine Phonebook
QDomElement domElementPhoneBookNode = domDoc.createElement("PhoneBook");
domElementPhoneBookNode.setAttribute("Name", m_MyPhoneBook.getPhoneBookName());
domDoc.appendChild(domElementPhoneBookNode);

for(int i = 0 ; i < m_MyPhoneBook.m_pSheetList->size() ; ++i)
{
    //Node Contact
    QDomElement domContactNode = domDoc.createElement("Contact");
    //Contact Nom
    QDomElement domContactName = domDoc.createElement("ContactName");
    domContactName.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactName()));
    domContactNode.appendChild(domContactName);
    //Contact Prenom
    QDomElement domContactFirstName = domDoc.createElement("ContactFirstName");
    domContactFirstName.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactFirstName()));
    domContactNode.appendChild(domContactFirstName);
    //Contact Civility
    QDomElement domContactCivility = domDoc.createElement("ContactCivility");
    domContactCivility.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactCivility()));
    domContactNode.appendChild(domContactCivility);
    //Contact Fonction
    QDomElement domContactFunction = domDoc.createElement("ContactFunction");
    domContactFunction.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactFunction()));
    domContactNode.appendChild(domContactFunction);
    //Contact Department
    QDomElement domContactDepartment = domDoc.createElement("ContactDepartment");
    domContactDepartment.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactDepartment()));
    domContactNode.appendChild(domContactDepartment);
    //Contact Adress
    QDomElement domContactAdress = domDoc.createElement("ContactAdress");
    domContactAdress.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactAdress()));
    domContactNode.appendChild(domContactAdress);
    //Contact City
    QDomElement domContactCity = domDoc.createElement("ContactCity");
    domContactCity.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactCity()));
    domContactNode.appendChild(domContactCity);
    //Contact State
    QDomElement domContactState = domDoc.createElement("ContactState");
    domContactState.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactState()));
    domContactNode.appendChild(domContactState);
    //Contact ZipCode
    QDomElement domContactZipCode = domDoc.createElement("ContactZipCode");
    domContactZipCode.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactZipCode()));
    domContactNode.appendChild(domContactZipCode);
    //Contact Country
    QDomElement domContactCountry = domDoc.createElement("ContactCountry");
    domContactCountry.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactCountry()));
    domContactNode.appendChild(domContactCountry);
    //Contact Mail1
    QDomElement domContactMail1 = domDoc.createElement("ContactMail1");
    domContactMail1.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactMail1()));
    domContactNode.appendChild(domContactMail1);
    //Contact Mail2
    QDomElement domContactMail2 = domDoc.createElement("ContactMail2");
    domContactMail2.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactMail2()));
    domContactNode.appendChild(domContactMail2);
    //Contact Phone
    QDomElement domContactPhone = domDoc.createElement("ContactPhone");
    domContactPhone.appendChild(domDoc.createTextNode(QString::number(m_MyPhoneBook.m_pSheetList->at(i)->GetContactPhoneNumber())));
    domContactNode.appendChild(domContactPhone);
    //Contact CellPhone
    QDomElement domContactCellPhone = domDoc.createElement("ContactCellPhone");
    domContactCellPhone.appendChild(domDoc.createTextNode(QString::number(m_MyPhoneBook.m_pSheetList->at(i)->GetContactCellPhoneNumber())));
    domContactNode.appendChild(domContactCellPhone);
    //Contact Fax
    QDomElement domContactFax = domDoc.createElement("ContactFax");
    domContactFax.appendChild(domDoc.createTextNode(QString::number(m_MyPhoneBook.m_pSheetList->at(i)->GetContactFaxNumber())));
    domContactNode.appendChild(domContactFax);
    //Contact OtherPhone
    QDomElement domContactOtherPhone = domDoc.createElement("ContactOtherPhone");
    domContactOtherPhone.appendChild(domDoc.createTextNode(QString::number(m_MyPhoneBook.m_pSheetList->at(i)->GetContactOtherPhoneNumber())));
    domContactNode.appendChild(domContactOtherPhone);
    //Contact Divers
    QDomElement domContactVarious = domDoc.createElement("ContactVarious");
    domContactVarious.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetContactVarious()));
    domContactNode.appendChild(domContactVarious);

    //Node Corporation
    //QDomElement domCorpNode = domDoc.createElement("Corporation");
    //Corp Name
    QDomElement domCorpName = domDoc.createElement("CorpName");
    domCorpName.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpName()));
    domContactNode.appendChild(domCorpName);
    //Corp Adress
    QDomElement domCorpAdress = domDoc.createElement("CorpAdress");
    domCorpAdress.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpAdress()));
    domContactNode.appendChild(domCorpAdress);
    //Corp City
    QDomElement domCorpCity = domDoc.createElement("CorpCity");
    domCorpCity.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpCity()));
    domContactNode.appendChild(domCorpCity);
    //Corp ZipCode
    QDomElement domCorpZipCode = domDoc.createElement("CorpZipCode");
    domCorpZipCode.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpZipCode()));
    domContactNode.appendChild(domCorpZipCode);
    //Corp State
    QDomElement domCorpState = domDoc.createElement("CorpState");
    domCorpState.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpState()));
    domContactNode.appendChild(domCorpState);
    //Corp Country
    QDomElement domCorpCountry = domDoc.createElement("CorpCountry");
    domCorpCountry.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpCountry()));
    domContactNode.appendChild(domCorpCountry);
    //Corp Mail1
    QDomElement domCorpMail1 = domDoc.createElement("CorpMail1");
    domCorpMail1.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpMail1()));
    domContactNode.appendChild(domCorpMail1);
    //Corp Mail2
    QDomElement domCorpMail2 = domDoc.createElement("CorpMail2");
    domCorpMail2.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpMail2()));
    domContactNode.appendChild(domCorpMail2);
    //Corp WebAdress
    QDomElement domCorpWebAdress = domDoc.createElement("CorpWebAdress");
    domCorpWebAdress.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpWebAdress()));
    domContactNode.appendChild(domCorpWebAdress);
    //Corp Phone
    QDomElement domCorpPhone = domDoc.createElement("CorpPhone");
    domCorpPhone.appendChild(domDoc.createTextNode(QString::number(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpPhoneNumber())));
    domContactNode.appendChild(domCorpPhone);
    //Corp Fax
    QDomElement domCorpFax = domDoc.createElement("CorpFax");
    domCorpFax.appendChild(domDoc.createTextNode(QString::number(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpFaxNumber())));
    domContactNode.appendChild(domCorpFax);
    //Corp Siret
    QDomElement domCorpSiret = domDoc.createElement("CorpSiret");
    domCorpSiret.appendChild(domDoc.createTextNode(QString::number(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpSiretNumber())));
    domContactNode.appendChild(domCorpSiret);
    //Corp TVA
    QDomElement domCorpTVA = domDoc.createElement("CorpTVA");
    domCorpTVA.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpTVAIntra()));
    domContactNode.appendChild(domCorpTVA);
    //Corp PaymentCondition
    QDomElement domCorpPaymentCondition = domDoc.createElement("CorpPaymentCondition");
    domCorpPaymentCondition.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpPaymentCondition()));
    domContactNode.appendChild(domCorpPaymentCondition);
    //Corp Various
    QDomElement domCorpVarious = domDoc.createElement("CorpVarious");
    domCorpVarious.appendChild(domDoc.createTextNode(m_MyPhoneBook.m_pSheetList->at(i)->GetCorpVarious()));
    domContactNode.appendChild(domCorpVarious);

    //Ajout Node Contact
    domElementPhoneBookNode.appendChild(domContactNode);
    //Ajout Node Corporation
    //domElementPhoneBookNode.appendChild(domCorpNode);
}

QDomProcessingInstruction domInstructions = domDoc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");

domDoc.insertBefore(domInstructions, domElementPhoneBookNode);

//Creation du fichier XML
QFile fFichierXML(sName);

//Ouverture du fichier
if(!fFichierXML.open(QFile::WriteOnly))
{
    return;
}

QTextStream tsOut(&fFichierXML);

tsOut << domDoc.toString();

//Sauvegarde
//domDoc.save(tsOut, 4, QDomDocument::EncodingFromDocument);

//Fermeture du fichier
fFichierXML.close();
}

我的阅读代码:

void MyMainWindow::LoadPhoneBookXML()
{
QString sFilePath = QFileDialog::getOpenFileName(this, tr("Open XML File ..."), "E:/", tr("XMLFile (*.xml)"));
if(sFilePath.isEmpty())
{
    QMessageBox::critical(this, tr("Open File XML"), tr("Le fichier xml est vide !"));
    return;
}

QFile fFile(sFilePath);
if(!fFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
    QMessageBox::critical(this, tr("File Open"), tr("Failed to open file for reading."));
    return;
}

//QTextStream tsIn(&fFile);

QDomDocument domDoc;

if(!domDoc.setContent(&fFile))
{
    QMessageBox::critical(this, tr("Parse Error"), tr("Failed to parse the file into a DOM tree."));
    fFile.close();
    return;
}

fFile.close();

//Infos du fichier
QFileInfo fileInfo(sFilePath);
//Dossier du fichier
QDir dir = fileInfo.dir();
//Path du fichier
QString sDirPath = fileInfo.filePath();
//Nom du fichier
QString sFileXmlName = fileInfo.fileName();

//QDomElement PhoneBook = domDoc.firstChildElement("PhoneBook");
QDomElement PhoneBook = domDoc.documentElement();
QDomElement Contact = PhoneBook.firstChildElement("Contact");

for(; !Contact.isNull() ; Contact = Contact.nextSiblingElement("Contact"))
{
    DataSheet *MySheetContact = new DataSheet();




    MySheetContact->SetContactName(Contact.firstChildElement("ContactName").localName());
    MySheetContact->SetContactFirstName(Contact.firstChildElement("ContactFirstName").localName());
    MySheetContact->SetContactCivility(Contact.firstChildElement("ContactCivility").localName());
    MySheetContact->SetContactFunction(Contact.firstChildElement("ContactFunction").localName());
    MySheetContact->SetContactDepartment(Contact.firstChildElement("ContactDepartment").localName());
    MySheetContact->SetContactAdress(Contact.firstChildElement("ContactAdress").localName());
    MySheetContact->SetContactCity(Contact.firstChildElement("ContactCity").localName());
    MySheetContact->SetContactState(Contact.firstChildElement("ContactState").localName());
    MySheetContact->SetContactZipCode(Contact.firstChildElement("ContactZipCode").localName());
    MySheetContact->SetContactCountry(Contact.firstChildElement("ContactCountry").localName());
    MySheetContact->SetContactMail1(Contact.firstChildElement("ContactMail1").localName());
    MySheetContact->SetContactMail2(Contact.firstChildElement("ContactMail2").localName());
    MySheetContact->SetContactPhoneNumber(Contact.firstChildElement("ContactPhone").localName().toInt());
    MySheetContact->SetContactCellPhoneNumber(Contact.firstChildElement("ContactCellPhone").localName().toInt());
    MySheetContact->SetContactFaxNumber(Contact.firstChildElement("ContactFax").localName().toInt());
    MySheetContact->SetContactOtherPhoneNumber(Contact.firstChildElement("ContactOtherPhone").localName().toInt());
    MySheetContact->SetContactVarious(Contact.firstChildElement("ContactVarious").localName());

    MySheetContact->SetCorpName(Contact.firstChildElement("CorpName").localName());
    MySheetContact->SetCorpAdress(Contact.firstChildElement("CorpAdress").localName());
    MySheetContact->SetCorpCity(Contact.firstChildElement("CorpCity").localName());
    MySheetContact->SetCorpState(Contact.firstChildElement("CorpState").localName());
    MySheetContact->SetCorpZipCode(Contact.firstChildElement("CorpZipCode").localName());
    MySheetContact->SetCorpCountry(Contact.firstChildElement("CorpCountry").localName());
    MySheetContact->SetCorpMail1(Contact.firstChildElement("CorpMail1").localName());
    MySheetContact->SetCorpMail2(Contact.firstChildElement("CorpMail2").localName());
    MySheetContact->SetCorpWebAdress(Contact.firstChildElement("CorpWebAdress").localName());
    MySheetContact->SetCorpPhoneNumber(Contact.firstChildElement("CorpPhone").localName().toInt());
    MySheetContact->SetCorpFaxNumber(Contact.firstChildElement("CorpFax").localName().toInt());
    MySheetContact->SetCorpOtherPhoneNumber(Contact.firstChildElement("CorpOtherPhone").localName().toInt());
    MySheetContact->SetCorpSiretNumber(Contact.firstChildElement("CorpSiret").localName().toInt());
    MySheetContact->SetCorpTVAIntra(Contact.firstChildElement("CorpTVA").localName());
    MySheetContact->SetCorpPaymentCondition(Contact.firstChildElement("CorpPaymentCondition").localName());
    MySheetContact->SetCorpVarious(Contact.firstChildElement("CorpVarious").localName());

    m_MyPhoneBook.m_pSheetList->push_back(MySheetContact);
}

//========Traitement apres lecture du fichier XML pour l'affichage de la liste de contacts ds l'IHM========//
//Partie ajout des noms des contacts dans la ListView des contacts
//MAJ de l'affichage des contacts
m_slExploListContact.clear();

for (int i = 0 ; i < m_MyPhoneBook.m_pSheetList->size() ; ++i)
{
    m_slExploListContact << (m_MyPhoneBook.m_pSheetList->at(i)->GetContactName() + " " + m_MyPhoneBook.m_pSheetList->at(i)->GetContactFirstName());
}

m_pExploListModel->setStringList(m_slExploListContact);
m_pExploListView->setModel(m_pExploListModel);

//Partie ajout des noms d'entreprise dans la ListView des entreprises
//MAJ de l'affichage des entreprises
m_slExploListCorp.clear();

for (int i = 0 ; i < m_MyPhoneBook.m_pSheetList->size() ; ++i)
{
    m_slExploListCorp << (m_MyPhoneBook.m_pSheetList->at(i)->GetCorpName());
}

//Suppression des doublons
m_slExploListCorp.removeDuplicates();

//Attribution de la String List au model
m_pExploListCorpModel->setStringList(m_slExploListCorp);
//Attribution du modele à la vue
m_pExploListCorpView->setModel(m_pExploListCorpModel);

//MAJ de l'affichage du nombre de contacts
m_pLabelContactNumber->setText(tr("Contacts : ") + QString::number(m_MyPhoneBook.m_pSheetList->size()));
}

我希望我在解释中已经足够清楚.. :)

感谢您的阅读,也许还有答案:p

0 个答案:

没有答案