Results 1 to 18 of 18

Thread: Remove element of xml

  1. #1
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Remove element of xml

    xml Code:
    1. <lista>
    2. <client>
    3. <dns>google.com.br</dns>
    4. <phone>(19)3632-9244</phone>
    5. <name>Google</name>
    6. </client>
    7. <client>
    8. <dns>uol.com.br</dns>
    9. <phone>(16)3307-5496</phone>
    10. <name>Uol</name>
    11. </client>
    12. <client>
    13. <dns>terra.com.br</dns>
    14. <phone>(21)3368-0422</phone>
    15. <name>Terra</name>
    16. </client>
    17. </list>
    To copy to clipboard, switch view to plain text mode 


    I wonder how I can delete everything between the tag <client> </client> choosing the name,
    eg:
    I would like to delete the customer information Google:
    xml Code:
    1. <client>
    2. <dns>google.com.br</dns>
    3. <phone>(19)3632-9244</phone>
    4. <name>Google</name>
    5. </client>
    To copy to clipboard, switch view to plain text mode 


    Thanks
    Last edited by Lykurg; 4th February 2011 at 06:42. Reason: added [HIGHLIGHT="xml"]

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Remove element of xml

    Quick and dirty solution could be to use regular expressions.
    Yours could look like this (pseudocode):
    Qt Code:
    1. <client>/*whatever*/<name>/*maybe whitespaces*/ClientToDelete/*maybe whitespaces*/</name>/*whatever*/</client>
    To copy to clipboard, switch view to plain text mode 
    Don't forget to set it for minimal match, it'll eat up all your xml otherwise (if it founds ClientToDelete).

  3. #3
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: Remove element of xml

    It would help if you explained a bit how you are parsing the document; i.e., if you are using SAX, DOM, or some other parser.

  4. #4
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Remove element of xml

    Quote Originally Posted by helloworld View Post
    It would help if you explained a bit how you are parsing the document; i.e., if you are using SAX, DOM, or some other parser.
    lol Sorry guy

    I'm using the DOM, see:

    Qt Code:
    1. QString arquivo = "List.xml";
    2.  
    3. QFile file(arquivo);
    4. QDomDocument doc( "List" );
    5.  
    6. //Checking if file exists
    7. if(!file.exists())
    8. {
    9. QMessageBox::information(0,tr("Critical Error"),tr("The file %1 does not exist! \ nPlease try to register an account!!").arg(arquivo));
    10. this->close();
    11. }
    12.  
    13. //Checking if the file can be read and written
    14. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    15. {
    16. QMessageBox::information(0,tr("Critical Error"),tr("Failed to open file %1!").arg(arquivo));
    17. file.close();
    18. this->close();
    19. }
    20.  
    21. //Getting the contents of the old xml
    22. doc.setContent(&file);
    23. file.close();
    To copy to clipboard, switch view to plain text mode 

    Thanks


    Added after 36 minutes:


    I changed the structure of my XML:

    Qt Code:
    1. <list>
    2. <client name="Google">
    3. <dns>google.com.br</dns>
    4. <phone>+5521XXXXXXXX</phone>
    5. </client>
    6. <client name="Hotmail">
    7. <dns>hotmail.com</dns>
    8. <phone>+5535XXXXXXXX</phone>
    9. </client>
    10. </list>
    To copy to clipboard, switch view to plain text mode 

    I created a method to delete, but is not working:

    Qt Code:
    1. void Remove::RemoveGroup(QDomDocument *twmDomDocument, QString theGroupName )
    2. {
    3. QDomNodeList domTwm = twmDomDocument->elementsByTagName( "client" );
    4.  
    5. if ( !domTwm.isEmpty() )
    6. {
    7. for ( uint i = 0; i < domTwm.length(); i++ )
    8. {
    9. if ( domTwm.at(i).toElement().attribute( "name" ) == theGroupName )
    10. {
    11. twmDomDocument->removeChild( domTwm.at(i) );
    12. }
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Remove::Remover()
    2. {
    3. RemoveGroup(&leXml(),"Google");
    4. }
    5.  
    6. QDomDocument Remove::leXml()
    7. {
    8. QString arquivo = "List.xml";
    9.  
    10. QFile file(arquivo);
    11. QDomDocument doc( "List" );
    12.  
    13. //Checking if file exists
    14. if(!file.exists())
    15. {
    16. QMessageBox::information(0,tr("Critical Error"),tr("The file %1 does not exist! \ nPlease try to register an account!!").arg(arquivo));
    17. this->close();
    18. }
    19.  
    20. //Checking if the file can be read and written
    21. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    22. {
    23. QMessageBox::information(0,tr("Critical Error"),tr("Failed to open file %1!").arg(arquivo));
    24. file.close();
    25. this->close();
    26. }
    27.  
    28. //Getting the contents of the old xml
    29. doc.setContent(&file);
    30. file.close();
    31. return doc;
    32. }
    To copy to clipboard, switch view to plain text mode 

    Why?
    Last edited by VitaliBR; 4th February 2011 at 10:56.

  5. #5
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Remove element of xml

    anyone?

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Remove element of xml

    A little suggestion:
    Qt Code:
    1. void Remove::RemoveGroup(QDomDocument *twmDomDocument, QString theGroupName )
    2. {
    3. QDomNodeList domTwm = twmDomDocument->elementsByTagName( "client" );
    4. qDebug() << "start remove: " << theGroupName;
    5. if ( !domTwm.isEmpty() )
    6. {
    7. qDebug() << "not empty... length: " << domTwm.length();
    8. for ( uint i = 0; i < domTwm.length(); i++ )
    9. {
    10. qDebug() << "processing " << i << " -> " << domTwm.at(i).toElement().attribute( "name" );
    11. if ( domTwm.at(i).toElement().attribute( "name" ) == theGroupName )
    12. {
    13. qDebug() << "removing";
    14. twmDomDocument->removeChild( domTwm.at(i) );
    15. }
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    Whats the output ?

    ------------------------------
    Wait a minute, now I've looked how you are calling your methods. This is not good:
    Qt Code:
    1. RemoveGroup(&leXml(),"Google");
    To copy to clipboard, switch view to plain text mode 
    Dont pass temporary values by pointer or reference, whats the point - leXml() returns a QDomDocument which may be modified by RemoveGroup(), but you cant access it anymore. This is a mess. Try this:
    Qt Code:
    1. QDomDocument doc = leXml();
    2. RemoveGroup(&doc,"Google");
    3. qDebug() << doc.toString();
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 4th February 2011 at 20:10.

  7. #7
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Remove element of xml

    It appeared in the Debug

    start remove: "Google"
    not empty... length: 1
    processing 0 -> "Google"
    removing
    "<!DOCTYPE List>
    <list>
    <client>
    <dns>google.com.br</dns>
    <phone>+55162223232</phone>
    <name>Google</name>
    </client>
    </list>"

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Remove element of xml

    Ok, now I see what's the problem here.
    In order to remove child element 'el' with "doc->removeChild( el )", 'el' should be direct child of the "doc" node. <client> is not direct child of your document, its a child of root element of your document, so this will work:
    Qt Code:
    1. twmDomDocument->documentElement().removeChild( domTwm.at(i) );
    To copy to clipboard, switch view to plain text mode 

    ---
    I have tested it on this document:
    Qt Code:
    1. <list>
    2. <client name="Google">
    3. <dns>google.com.br</dns>
    4. <phone>+5521XXXXXXXX</phone>
    5. </client>
    6. <client name="Hotmail">
    7. <dns>hotmail.com</dns>
    8. <phone>+5535XXXXXXXX</phone>
    9. </client>
    10. </list>
    To copy to clipboard, switch view to plain text mode 

    code:
    Qt Code:
    1. QFile f("test.xml");
    2. f.open( QIODevice::ReadOnly | QIODevice::Text );
    3. doc.setContent( &f );
    4. RemoveGroup(&doc,"Google");
    5. qDebug() << doc.toString();
    To copy to clipboard, switch view to plain text mode 

    output:
    <list>
    <client name="Hotmail" >
    <dns>hotmail.com</dns>
    <phone>+5535XXXXXXXX</phone>
    </client>
    </list>
    Last edited by stampede; 8th February 2011 at 10:58. Reason: removed unnecessary quotes

  9. #9
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Remove element of xml

    Ok

    Thanks for your help
    I need to save this change in the XML? If yes, how I do it?

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Remove element of xml

    I need to save this change in the XML? If yes, how I do it?
    The method you have changes QDomDocument object, which not implies change in file from which you get xml string. To save changes, simply overwrite previous content of the xml file using the updated QDomDocument object.

  11. #11
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Remove element of xml

    But why is not Google removing my XML?

    the button (action) remover
    Qt Code:
    1. void Remove::Remover()
    2. {
    3. QFile f("List.xml");
    4. f.open( QIODevice::ReadOnly | QIODevice::Text );
    5. doc.setContent( &f );
    6. RemoveGroup(&doc,"Google");
    7.  
    8. QTextStream out(&f);
    9. out << doc.toString();
    10.  
    11. f.close();
    12.  
    13. QMessageBox::information(0,tr("Warning!"),tr("The Client %1 has been removed!").arg("Google"));
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Remove element of xml

    But why is not Google removing my XML?
    What do you mean ? Remove method is not working ? Have you updated it with my previous suggestion ?
    Or do you mean that content of file is not updated ? If yes, then well...
    Qt Code:
    1. f.open( QIODevice::ReadOnly | QIODevice::Text );
    2. //...
    3. QTextStream out(&f);
    4. out << doc.toString();
    To copy to clipboard, switch view to plain text mode 
    'f' is still opened in ReadOnly mode when you try to write to it.

  13. The following user says thank you to stampede for this useful post:

    riarioriu3 (14th September 2012)

  14. #13
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Remove element of xml

    I did so:
    Qt Code:
    1. void Remove::Remover()
    2. {
    3. QFile f("List.xml");
    4. f.open(QIODevice::WriteOnly);
    5. doc.setContent( &f );
    6. RemoveGroup(&doc,"Google");
    7.  
    8. QTextStream out(&f);
    9. out << doc.toString();
    10.  
    11. f.close();
    12.  
    13. QMessageBox::information(0,tr("Warning!"),tr("The Client %1 has been removed!").arg("Google"));
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Remove::RemoveGroup(QDomDocument *twmDomDocument, QString theGroupName )
    2. {
    3. QDomNodeList domTwm = twmDomDocument->elementsByTagName( "client" );
    4.  
    5. if ( !domTwm.isEmpty() )
    6. {
    7. for ( uint i = 0; i < domTwm.length(); i++ )
    8. {
    9. if ( domTwm.at(i).toElement().attribute( "name" ) == theGroupName )
    10. {
    11. twmDomDocument->documentElement().removeChild( domTwm.at(i) );
    12. }
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 


    But instead of it just delete the Google of the XML, it leaves blank XML file (delete everything)

  15. #14
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Remove element of xml

    Qt Code:
    1. f.open(QIODevice::WriteOnly);
    2. doc.setContent( &f );
    To copy to clipboard, switch view to plain text mode 

    Now you are trying to read from file opened in WriteOnly mode ...
    Think, this "problem" is not really difficult. Don't just copy-paste, you're supposed to be a programmer, right ?
    In order to give you the next tip I'll need to write the code for you.

  16. The following user says thank you to stampede for this useful post:

    VitaliBR (15th February 2011)

  17. #15
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Remove element of xml

    Thanks

    now worked

    I did so:
    Qt Code:
    1. QFile f("List.xml");
    2. f.open(QIODevice::ReadWrite | QIODevice::Text);
    3. doc.setContent( &f );
    4. RemoveGroup(&doc,ui.comboBox->currentText());
    To copy to clipboard, switch view to plain text mode 

    A single doubt, I researched and tried but could not:
    When he saves the new list in XML, it does not erase the old one, ie it saves the new list after the old list
    Qt Code:
    1. <!DOCTYPE List><list>
    2. <client>
    3. <dns>google.com.br</dns>
    4. <phone>(19)3632-9244</phone>
    5. <name>Google</name>
    6. </client>
    7. <client>
    8. <dns>uol.com.br</dns>
    9. <phone>(16)3307-5496</phone>
    10. <name>Uol</name>
    11. </client>
    12. <client>
    13. <dns>terra.com.br</dns>
    14. <phone>(21)3368-0422</phone>
    15. <name>Terra</name>
    16. </client>
    17. </list>
    18. <!DOCTYPE List><list>
    19. <client>
    20. <dns>uol.com.br</dns>
    21. <phone>(16)3307-5496</phone>
    22. <name>Uol</name>
    23. </client>
    24. <client>
    25. <dns>terra.com.br</dns>
    26. <phone>(21)3368-0422</phone>
    27. <name>Terra</name>
    28. </client>
    29. </list>
    To copy to clipboard, switch view to plain text mode 

  18. #16
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Remove element of xml

    I tried using a doc.clear(), but did not work

  19. #17
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Remove element of xml

    Try it this way:
    1) open file in ReadOnly mode, create your QDomDocument from it
    2) close the file
    3) do the processing of xml
    4) open the same file again with WriteOnly, and Truncate flags set ( should clear previous content )
    5) save new xml to file and close it

  20. #18
    Join Date
    Dec 2010
    Posts
    24
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Remove element of xml

    Worked

    Thanks guy!!

Similar Threads

  1. [QML] Video element
    By grezly in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2011, 17:26
  2. Problem with translate one element
    By Hostel in forum Newbie
    Replies: 0
    Last Post: 19th November 2010, 00:00
  3. How to get focum element on QWebPage?
    By corrado in forum Qt Programming
    Replies: 0
    Last Post: 16th May 2010, 15:00
  4. Change a QList element
    By mcrahr in forum Newbie
    Replies: 1
    Last Post: 23rd August 2009, 11:16
  5. Fix element in risizing window
    By JoZCaVaLLo in forum Newbie
    Replies: 4
    Last Post: 26th June 2009, 10:35

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.