PDA

View Full Version : Remove node in XML file



pinjul
6th February 2013, 16:15
Hello,

I'm french, be indulgent please for my writing :)

I have this XML file:



<CallAccounting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CAPTicket_V001.001.xsd">
<OmniPCXOffice>
<SoftwareVersion>3Efefe1</SoftwareVersion>
<CPUIPAddress>117.168.192.45</CPUIPAddress>
</OmniPCXOffice>
<Checksum>821551922</Checksum>
<TicketType>Call</TicketType>
<InitialUserType>G</InitialUserType>
<InitialUserID>9</InitialUserID>
<ChargedUserType>A</ChargedUserType>
<ChargedUserID>13</ChargedUserID>
<SubscriberName>BEBO</SubscriberName>
<CommunicationType>Incoming</CommunicationType>
<TrunkType>N</TrunkType>
<TrunkID>001</TrunkID>
<Date>2012-12-24</Date>
<Time>14:48:00</Time>
<CallDuration>00:02:12</CallDuration>
<TaxesAmount>0</TaxesAmount>
<Service>ST</Service>
<DialledNumber>0559270461</DialledNumber>
<DiallingMode>M</DiallingMode>
<RingingDuration>00:00:04</RingingDuration>
<Cost>0.00</Cost>
<Currency>EUR</Currency>
</CallAccounting>


I want to edit this file to get this:




<CallAccounting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CAPTicket_V001.001.xsd">
<TicketType>Call</TicketType>
<ChargedUserID>13</ChargedUserID>
<SubscriberName>BEATRICE</SubscriberName>
<CommunicationType>Incoming</CommunicationType>
<Date>2012-12-24</Date>
<Time>14:48:00</Time>
<CallDuration>00:02:12</CallDuration>
<DialledNumber>0559270461</DialledNumber>
</CallAccounting>



I have try QDomNode::removeChild() but i don't know to use it :(

Help me please !!

Pinjul

wysota
6th February 2013, 16:22
Did you get past reading the xml file into QDomDocument?

pinjul
6th February 2013, 16:27
Thank you to answer me.

No i haven't.
I have read the doc but i don't understand all..

wysota
6th February 2013, 17:13
The example in the docs is pretty much self explainatory. If you ask a specific question, we'll try to answer it but we won't do your work for you.

Santosh Reddy
6th February 2013, 17:44
I have try QDomNode::removeChild() but i don't know to use it
I guess you are looping though the children and then removing them from the node which doe not match. Note that once the node is removed from the parent, you cannot get referenc to next sibling, so have to make provision to have a copy of nextsibling (node)

somthing like this


QDomNode n = docElem.firstChild();
while(!n.isNull()) {
QDomNode copy = n.nextSibling(); //have a copy
QDomElement e = n.toElement();
if(!e.isNull()) {
if(//if tag matched)
docElem.removeChild(n);
}
n = copy;
}

wysota
6th February 2013, 17:47
He is not looping through anything because he didn't manage to load the document yet.

pinjul
7th February 2013, 09:07
Hello everybody,

I have tried to read the XML file with DOM
My code:




#include<iostream>
#include<QtGui>
#include<QtXML/QDomDocument>
#include<QtXml/QDomElement>
using namespace std;



int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QString fileName = "Fichier_Dom.xml";
QFile file(fileName);
//Ouverture du fichier en lecture seule et en mode texte
file.open(QFile::ReadOnly|QFile::Text);

QDomDocument doc;
//Ajoute le contenu du fichier XML dans un QDomDocument et dit au QDomDocument de ne pas tenir compte dse namespaces
doc.setContent(&file, false);

//Ici, listeAppel pointe sur l'élément <CallAcountingList> de notre document
QDomElement listeAppel = doc.documentElement();

//Ici, listeAppel pointe sur un fils de <CallAcountingList>, c'est à dire <CallAcounting>
listeAppel = listeAppel.firstChildElement();

//Boucle permettant la navigation dans le fichier XML
while(!listeAppel.isNull())
{
//Si on pointe sur un élément de type <CallAcounting>
if(listeAppel.tagName() == "CallAcounting")
{
//On récupère le premier enfant de l'élément CallAcounting c'est à dire <OmniPCXOffice> ou <Checksum> ou ...
QDomElement unElement = listeAppel.firstChildElement();

//On parcourt tous les enfants de l'élément <CallAcounting>
while(!unElement.isNull())
{
//Si l'enfant de l'élément CallAcounting est l'élément <OmniPCXOffice>
if (unElement.tagName() == "OmniPCXOffice")
{
//On récupère le texte contenu dans la balise <OmniPCXOffice>
QString strOmniPCXOffice = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <Checksum>
else if (unElement.tagName() == "CheckSum")
{
//On récupère le texte contenu dans la balise <Checksum>
QString strChecksum = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <TicketType>
else if (unElement.tagName() == "TicketType")
{
//On récupère le texte contenu dans la balise <TicketType>
QString strTicketType = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <InitialUserType>
else if (unElement.tagName() == "InitialUserType")
{
//On récupère le texte contenu dans la balise <InitialUserType>
QString strInitialUserType = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <InitialUserID>
else if (unElement.tagName() == "InitialUserID")
{
//On récupère le texte contenu dans la balise <InitialUserID>
QString strInitialUserID = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <ChargedUserType>
else if (unElement.tagName() == "ChargedUserType")
{
//On récupère le texte contenu dans la balise <ChargedUserType>
QString strChargedUserType = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <ChargedUserID>
else if (unElement.tagName() == "ChargedUserID")
{
//On récupère le texte contenu dans la balise <ChargedUserID>
QString strChargedUserID = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <SuscriberName>
else if (unElement.tagName() == "SuscriberName")
{
//On récupère le texte contenu dans la balise <SuscriberName>
QString strSuscriberName = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <CommunicationType>
else if (unElement.tagName() == "CommunicationType")
{
//On récupère le texte contenu dans la balise <CommunicationType>
QString strCommunicationType = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <TrunkType>
else if (unElement.tagName() == "TrunkType")
{
//On récupère le texte contenu dans la balise <TrunkType>
QString strTrunkType = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <TrunkID>
else if (unElement.tagName() == "TrunkID")
{
//On récupère le texte contenu dans la balise <TrunkID>
QString strTrunkID = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <Date>
else if (unElement.tagName() == "Date")
{
//On récupère le texte contenu dans la balise <Date>
QString strDate = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <Time>
else if (unElement.tagName() == "Time")
{
//On récupère le texte contenu dans la balise <Time>
QString strTime = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <CallDuration>
else if (unElement.tagName() == "CallDuration")
{
//On récupère le texte contenu dans la balise <CallDuration>
QString strCallDuration = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <TaxesAmount>
else if (unElement.tagName() == "TaxesAmount")
{
//On récupère le texte contenu dans la balise <TaxesAmount>
QString strTaxesAmount = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <Service>
else if (unElement.tagName() == "Service")
{
//On récupère le texte contenu dans la balise <Service>
QString strService = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <DialledNumber>
else if (unElement.tagName() == "DialledNumber")
{
//On récupère le texte contenu dans la balise <DialledNumber>
QString strDialledNumber = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <DiallingMode>
else if (unElement.tagName() == "DiallingMode")
{
//On récupère le texte contenu dans la balise <DiallingMode>
QString strDiallingMode = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <RingingDuration>
else if (unElement.tagName() == "RingingDuration")
{
//On récupère le texte contenu dans la balise <RingingDuration>
QString strRingingDuration = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <Cost>
else if (unElement.tagName() == "Cost")
{
//On récupère le texte contenu dans la balise <Cost>
QString strCost = unElement.text();
}
//Si l'enfant de l'élément CallAcounting est l'élément <Currency>
else if (unElement.tagName() == "Currency")
{
//On récupère le texte contenu dans la balise <Currency>
QString strCurrency = unElement.text();
}
}


}

}





return app.exec();
}



When I execute the programm, i have 55 errors. " Undefined reference to .. "

Where is the problem please, i don't understand .

Santosh Reddy
7th February 2013, 09:13
make sure that <project>.pro file has Qt xml module included


QT += core gui xml

pinjul
7th February 2013, 09:16
Thank you, how can i be sure the XML File was read correctly ?

Lykurg
7th February 2013, 09:33
QDomDocument::setContent() returns a bool value and parameters return error number and text in case something went wrong.

pinjul
7th February 2013, 09:55
I write this:




QDomDocument doc;
doc.setContent(fileName);



but nothing happens.

i don't use it well ?

Lykurg
7th February 2013, 10:14
May be you want check what doc.setContent return as I said. Also setContent has some more arguments. You might want use it?!?
Also my crystal ball says: Your path to the file is wrong. It is relative and points to the wrong path.