PDA

View Full Version : Read XML parse and generate XML



qt_developer
23rd January 2015, 07:56
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.

wysota
23rd January 2015, 08:00
QDomDocument

qt_developer
23rd January 2015, 08:14
With QDomDocument it is needed write the nodes, e. g.:



QDomDocument doc("MyML");
QDomElement root = doc.createElement("MyML");
doc.appendChild(root);

QDomElement tag = doc.createElement("Greeting");
root.appendChild(tag);

QDomText t = doc.createTextNode("Hello World");
tag.appendChild(t);

QString xml = doc.toString();


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

wysota
23rd January 2015, 08:36
I'm sorry I don't understand what that means. How do you want to modify XML nodes without accessing the nodes?

anda_skoa
23rd January 2015, 09:00
I want to generate a XML from the information of XML document read.
QDomDocument::setContent().

Cheers,
_

qt_developer
23rd January 2015, 09:01
I'm sorry. I will try to explain it better.

I want this:

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



<?xml version="1.0" encoding="UTF-8"?>
<information>
<node1>data1<node1>
<node2>data2<node2>
</information>


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.



<?xml version="1.0" encoding="UTF-8"?>
<information>
<node1>myValue1<node1>
<node2>myValue2<node2>
</information>


Now my code is like this:



QString myXML = QString(

"<?xml version="1.0" encoding="UTF-8"?>\n"\
"<information>\n"\
"\t\t<node1>%1<node1>\n"\
"\t\t<node2>%2<node2>\n"\
"</information>\n");

myXML = myXML.arg(data1).arg(data2); // data1 and data2 are QString with the information obtained from the GUI.

yeye_olive
23rd January 2015, 10:44
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.

wysota
23rd January 2015, 11:09
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.

qt_developer
26th January 2015, 18:43
I've used QDomDocument. Thank you.

Regards.