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.
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.
With QDomDocument it is needed write the nodes, e. g.:
Qt Code:
doc.appendChild(root); root.appendChild(tag); tag.appendChild(t); To copy to clipboard, switch view to plain text mode
I want to generate a XML from the information of XML document read.
I'm sorry I don't understand what that means. How do you want to modify XML nodes without accessing the nodes?
I'm sorry. I will try to explain it better.
I want this:
1.- I parse a XML document. For example:
Qt Code:
<?xml version="1.0" encoding="UTF-8"?> <information> <node1>data1<node1> <node2>data2<node2> </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:
<?xml version="1.0" encoding="UTF-8"?> <information> <node1>myValue1<node1> <node2>myValue2<node2> </information>To copy to clipboard, switch view to plain text mode
Now my code is like this:
Qt Code:
"<?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.To copy to clipboard, switch view to plain text mode
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.
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.
I've used QDomDocument. Thank you.
Regards.
QDomDocument::setContent().
Cheers,
_
Bookmarks