Results 1 to 12 of 12

Thread: Remove node in XML file

  1. #1
    Join Date
    Feb 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Remove node in XML file

    Hello,

    I'm french, be indulgent please for my writing

    I have this XML file:

    Qt Code:
    1. <CallAccounting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CAPTicket_V001.001.xsd">
    2. <OmniPCXOffice>
    3. <SoftwareVersion>3Efefe1</SoftwareVersion>
    4. <CPUIPAddress>117.168.192.45</CPUIPAddress>
    5. </OmniPCXOffice>
    6. <Checksum>821551922</Checksum>
    7. <TicketType>Call</TicketType>
    8. <InitialUserType>G</InitialUserType>
    9. <InitialUserID>9</InitialUserID>
    10. <ChargedUserType>A</ChargedUserType>
    11. <ChargedUserID>13</ChargedUserID>
    12. <SubscriberName>BEBO</SubscriberName>
    13. <CommunicationType>Incoming</CommunicationType>
    14. <TrunkType>N</TrunkType>
    15. <TrunkID>001</TrunkID>
    16. <Date>2012-12-24</Date>
    17. <Time>14:48:00</Time>
    18. <CallDuration>00:02:12</CallDuration>
    19. <TaxesAmount>0</TaxesAmount>
    20. <Service>ST</Service>
    21. <DialledNumber>0559270461</DialledNumber>
    22. <DiallingMode>M</DiallingMode>
    23. <RingingDuration>00:00:04</RingingDuration>
    24. <Cost>0.00</Cost>
    25. <Currency>EUR</Currency>
    26. </CallAccounting>
    To copy to clipboard, switch view to plain text mode 

    I want to edit this file to get this:

    Qt Code:
    1. <CallAccounting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CAPTicket_V001.001.xsd">
    2. <TicketType>Call</TicketType>
    3. <ChargedUserID>13</ChargedUserID>
    4. <SubscriberName>BEATRICE</SubscriberName>
    5. <CommunicationType>Incoming</CommunicationType>
    6. <Date>2012-12-24</Date>
    7. <Time>14:48:00</Time>
    8. <CallDuration>00:02:12</CallDuration>
    9. <DialledNumber>0559270461</DialledNumber>
    10. </CallAccounting>
    To copy to clipboard, switch view to plain text mode 

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

    Help me please !!

    Pinjul

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Remove node in XML file

    Did you get past reading the xml file into QDomDocument?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Remove node in XML file

    Thank you to answer me.

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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Remove node in XML file

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Remove node in XML file

    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
    Qt Code:
    1. QDomNode n = docElem.firstChild();
    2. while(!n.isNull()) {
    3. QDomNode copy = n.nextSibling(); //have a copy
    4. QDomElement e = n.toElement();
    5. if(!e.isNull()) {
    6. if(//if tag matched)
    7. docElem.removeChild(n);
    8. }
    9. n = copy;
    10. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Remove node in XML file

    He is not looping through anything because he didn't manage to load the document yet.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Remove node in XML file

    Hello everybody,

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

    Qt Code:
    1. #include<iostream>
    2. #include<QtGui>
    3. #include<QtXML/QDomDocument>
    4. #include<QtXml/QDomElement>
    5. using namespace std;
    6.  
    7.  
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication app(argc, argv);
    12.  
    13. QString fileName = "Fichier_Dom.xml";
    14. QFile file(fileName);
    15. //Ouverture du fichier en lecture seule et en mode texte
    16. file.open(QFile::ReadOnly|QFile::Text);
    17.  
    18. //Ajoute le contenu du fichier XML dans un QDomDocument et dit au QDomDocument de ne pas tenir compte dse namespaces
    19. doc.setContent(&file, false);
    20.  
    21. //Ici, listeAppel pointe sur l'élément <CallAcountingList> de notre document
    22. QDomElement listeAppel = doc.documentElement();
    23.  
    24. //Ici, listeAppel pointe sur un fils de <CallAcountingList>, c'est à dire <CallAcounting>
    25. listeAppel = listeAppel.firstChildElement();
    26.  
    27. //Boucle permettant la navigation dans le fichier XML
    28. while(!listeAppel.isNull())
    29. {
    30. //Si on pointe sur un élément de type <CallAcounting>
    31. if(listeAppel.tagName() == "CallAcounting")
    32. {
    33. //On récupère le premier enfant de l'élément CallAcounting c'est à dire <OmniPCXOffice> ou <Checksum> ou ...
    34. QDomElement unElement = listeAppel.firstChildElement();
    35.  
    36. //On parcourt tous les enfants de l'élément <CallAcounting>
    37. while(!unElement.isNull())
    38. {
    39. //Si l'enfant de l'élément CallAcounting est l'élément <OmniPCXOffice>
    40. if (unElement.tagName() == "OmniPCXOffice")
    41. {
    42. //On récupère le texte contenu dans la balise <OmniPCXOffice>
    43. QString strOmniPCXOffice = unElement.text();
    44. }
    45. //Si l'enfant de l'élément CallAcounting est l'élément <Checksum>
    46. else if (unElement.tagName() == "CheckSum")
    47. {
    48. //On récupère le texte contenu dans la balise <Checksum>
    49. QString strChecksum = unElement.text();
    50. }
    51. //Si l'enfant de l'élément CallAcounting est l'élément <TicketType>
    52. else if (unElement.tagName() == "TicketType")
    53. {
    54. //On récupère le texte contenu dans la balise <TicketType>
    55. QString strTicketType = unElement.text();
    56. }
    57. //Si l'enfant de l'élément CallAcounting est l'élément <InitialUserType>
    58. else if (unElement.tagName() == "InitialUserType")
    59. {
    60. //On récupère le texte contenu dans la balise <InitialUserType>
    61. QString strInitialUserType = unElement.text();
    62. }
    63. //Si l'enfant de l'élément CallAcounting est l'élément <InitialUserID>
    64. else if (unElement.tagName() == "InitialUserID")
    65. {
    66. //On récupère le texte contenu dans la balise <InitialUserID>
    67. QString strInitialUserID = unElement.text();
    68. }
    69. //Si l'enfant de l'élément CallAcounting est l'élément <ChargedUserType>
    70. else if (unElement.tagName() == "ChargedUserType")
    71. {
    72. //On récupère le texte contenu dans la balise <ChargedUserType>
    73. QString strChargedUserType = unElement.text();
    74. }
    75. //Si l'enfant de l'élément CallAcounting est l'élément <ChargedUserID>
    76. else if (unElement.tagName() == "ChargedUserID")
    77. {
    78. //On récupère le texte contenu dans la balise <ChargedUserID>
    79. QString strChargedUserID = unElement.text();
    80. }
    81. //Si l'enfant de l'élément CallAcounting est l'élément <SuscriberName>
    82. else if (unElement.tagName() == "SuscriberName")
    83. {
    84. //On récupère le texte contenu dans la balise <SuscriberName>
    85. QString strSuscriberName = unElement.text();
    86. }
    87. //Si l'enfant de l'élément CallAcounting est l'élément <CommunicationType>
    88. else if (unElement.tagName() == "CommunicationType")
    89. {
    90. //On récupère le texte contenu dans la balise <CommunicationType>
    91. QString strCommunicationType = unElement.text();
    92. }
    93. //Si l'enfant de l'élément CallAcounting est l'élément <TrunkType>
    94. else if (unElement.tagName() == "TrunkType")
    95. {
    96. //On récupère le texte contenu dans la balise <TrunkType>
    97. QString strTrunkType = unElement.text();
    98. }
    99. //Si l'enfant de l'élément CallAcounting est l'élément <TrunkID>
    100. else if (unElement.tagName() == "TrunkID")
    101. {
    102. //On récupère le texte contenu dans la balise <TrunkID>
    103. QString strTrunkID = unElement.text();
    104. }
    105. //Si l'enfant de l'élément CallAcounting est l'élément <Date>
    106. else if (unElement.tagName() == "Date")
    107. {
    108. //On récupère le texte contenu dans la balise <Date>
    109. QString strDate = unElement.text();
    110. }
    111. //Si l'enfant de l'élément CallAcounting est l'élément <Time>
    112. else if (unElement.tagName() == "Time")
    113. {
    114. //On récupère le texte contenu dans la balise <Time>
    115. QString strTime = unElement.text();
    116. }
    117. //Si l'enfant de l'élément CallAcounting est l'élément <CallDuration>
    118. else if (unElement.tagName() == "CallDuration")
    119. {
    120. //On récupère le texte contenu dans la balise <CallDuration>
    121. QString strCallDuration = unElement.text();
    122. }
    123. //Si l'enfant de l'élément CallAcounting est l'élément <TaxesAmount>
    124. else if (unElement.tagName() == "TaxesAmount")
    125. {
    126. //On récupère le texte contenu dans la balise <TaxesAmount>
    127. QString strTaxesAmount = unElement.text();
    128. }
    129. //Si l'enfant de l'élément CallAcounting est l'élément <Service>
    130. else if (unElement.tagName() == "Service")
    131. {
    132. //On récupère le texte contenu dans la balise <Service>
    133. QString strService = unElement.text();
    134. }
    135. //Si l'enfant de l'élément CallAcounting est l'élément <DialledNumber>
    136. else if (unElement.tagName() == "DialledNumber")
    137. {
    138. //On récupère le texte contenu dans la balise <DialledNumber>
    139. QString strDialledNumber = unElement.text();
    140. }
    141. //Si l'enfant de l'élément CallAcounting est l'élément <DiallingMode>
    142. else if (unElement.tagName() == "DiallingMode")
    143. {
    144. //On récupère le texte contenu dans la balise <DiallingMode>
    145. QString strDiallingMode = unElement.text();
    146. }
    147. //Si l'enfant de l'élément CallAcounting est l'élément <RingingDuration>
    148. else if (unElement.tagName() == "RingingDuration")
    149. {
    150. //On récupère le texte contenu dans la balise <RingingDuration>
    151. QString strRingingDuration = unElement.text();
    152. }
    153. //Si l'enfant de l'élément CallAcounting est l'élément <Cost>
    154. else if (unElement.tagName() == "Cost")
    155. {
    156. //On récupère le texte contenu dans la balise <Cost>
    157. QString strCost = unElement.text();
    158. }
    159. //Si l'enfant de l'élément CallAcounting est l'élément <Currency>
    160. else if (unElement.tagName() == "Currency")
    161. {
    162. //On récupère le texte contenu dans la balise <Currency>
    163. QString strCurrency = unElement.text();
    164. }
    165. }
    166.  
    167.  
    168. }
    169.  
    170. }
    171.  
    172.  
    173.  
    174.  
    175.  
    176. return app.exec();
    177. }
    To copy to clipboard, switch view to plain text mode 

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

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

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Remove node in XML file

    make sure that <project>.pro file has Qt xml module included
    Qt Code:
    1. QT += core gui xml
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. #9
    Join Date
    Feb 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Remove node in XML file

    Thank you, how can i be sure the XML File was read correctly ?

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Remove node in XML file

    QDomDocument::setContent() returns a bool value and parameters return error number and text in case something went wrong.

  11. #11
    Join Date
    Feb 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Remove node in XML file

    I write this:

    Qt Code:
    1. doc.setContent(fileName);
    To copy to clipboard, switch view to plain text mode 

    but nothing happens.

    i don't use it well ?

  12. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Remove node in XML file

    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.

Similar Threads

  1. Designing a Node Editor (Hint: Blender Node Editor)
    By Mind Calamity in forum Qt Programming
    Replies: 4
    Last Post: 5th October 2011, 16:22
  2. how to remove .ini file
    By rahulgogoi in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2011, 14:27
  3. Inserting node to Xml file
    By vajindarladdad in forum Newbie
    Replies: 2
    Last Post: 13th April 2009, 11:34
  4. how to justfy a node is leaf node or not
    By weixj2003ld in forum Qt Programming
    Replies: 4
    Last Post: 9th April 2009, 07:40
  5. remove node in xml file
    By mattia in forum Newbie
    Replies: 1
    Last Post: 6th March 2008, 13:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.