Results 1 to 9 of 9

Thread: Read XML parse and generate XML

  1. #1
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Read XML parse and generate XML

    Hi all,

    I want to read a xml file, parse it, modify some values and finally generate a file with the information read. Is it possible without write the xml in the code (hardcoded)?

    Best regards.

  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: Read XML parse and generate XML

    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
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read XML parse and generate XML

    With QDomDocument it is needed write the nodes, e. g.:

    Qt Code:
    1. QDomDocument doc("MyML");
    2. QDomElement root = doc.createElement("MyML");
    3. doc.appendChild(root);
    4.  
    5. QDomElement tag = doc.createElement("Greeting");
    6. root.appendChild(tag);
    7.  
    8. QDomText t = doc.createTextNode("Hello World");
    9. tag.appendChild(t);
    10.  
    11. QString xml = doc.toString();
    To copy to clipboard, switch view to plain text mode 

    I want to generate a XML from the information of XML document read.

  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: Read XML parse and generate XML

    I'm sorry I don't understand what that means. How do you want to modify XML nodes without accessing the nodes?
    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
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Read XML parse and generate XML

    Quote Originally Posted by qt_developer View Post
    I want to generate a XML from the information of XML document read.
    QDomDocument::setContent().

    Cheers,
    _

  6. #6
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Read XML parse and generate XML

    I'm sorry. I will try to explain it better.

    I want this:

    1.- I parse a XML document. For example:

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <information>
    3. <node1>data1<node1>
    4. <node2>data2<node2>
    5. </information>
    To copy to clipboard, switch view to plain text mode 

    The node1 and node2 values are put into QLineEdit's.

    2.- I update the information from GUI. data1 --> "myValue1" and data2 --> "myValue2".

    3.- I generate a XML with the new values. <-- How can I generate a new XML? I have a method with the XML hardcoded to generate the file. This is a problem if the XML changes or if the XML is very big.

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <information>
    3. <node1>myValue1<node1>
    4. <node2>myValue2<node2>
    5. </information>
    To copy to clipboard, switch view to plain text mode 

    Now my code is like this:

    Qt Code:
    1. QString myXML = QString(
    2.  
    3. "<?xml version="1.0" encoding="UTF-8"?>\n"\
    4. "<information>\n"\
    5. "\t\t<node1>%1<node1>\n"\
    6. "\t\t<node2>%2<node2>\n"\
    7. "</information>\n");
    8.  
    9. myXML = myXML.arg(data1).arg(data2); // data1 and data2 are QString with the information obtained from the GUI.
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Read XML parse and generate XML

    Quote Originally Posted by qt_developer View Post
    3.- I generate a XML with the new values. <-- How can I generate a new XML? I have a method with the XML hardcoded to generate the file. This is a problem if the XML changes or if the XML is very big.
    What exactly is the problem?

    If your XML file contains more information that the two strings, and you want to update those strings and leave the rest of the document unchanged, then use QDomDocument as others have already explained.

    If you only care about those two strings and just want to write a new XML file with only those two strings (thus losing any existing additional content), then you can simply write it with QXmlStreamWriter. It improves over your current solution because it properly escapes the two strings.

  8. #8
    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: Read XML parse and generate XML

    QDomDocument can both read existing and create new documents. So just read the existing xml document, modify it and save it again. I don't really see the problem.
    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.


  9. #9
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read XML parse and generate XML

    I've used QDomDocument. Thank you.

    Regards.

Similar Threads

  1. Using SAX to parse XML
    By sudha in forum Newbie
    Replies: 10
    Last Post: 20th January 2012, 00:12
  2. parse xml
    By vinayaka in forum Newbie
    Replies: 1
    Last Post: 3rd October 2011, 17:42
  3. Replies: 0
    Last Post: 11th April 2010, 22:26
  4. parse xml
    By rmagro in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2009, 23:27
  5. XML parse
    By arunredi in forum Newbie
    Replies: 2
    Last Post: 27th April 2008, 02:22

Tags for this Thread

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.