How to update a node in xml using QStreamWritter class?
-
I have an xml file in which i want to change the the attribute values of a node node.
how can i do it using QXmlStreamWritter class,ex:if i want to change the value of <heading> from reminder to something without being appending the new node node to the xml file.
<note>
<to>Tove</to>
<from>kishore</from>
<heading>Reminder</heading>
<body>hi!</body>
</note> -
I have an xml file in which i want to change the the attribute values of a node node.
how can i do it using QXmlStreamWritter class,ex:if i want to change the value of <heading> from reminder to something without being appending the new node node to the xml file.
<note>
<to>Tove</to>
<from>kishore</from>
<heading>Reminder</heading>
<body>hi!</body>
</note>@Sherlin-N-G I guess you mean QXmlStreamWritter?
Like shown here: http://doc.qt.io/qt-5/qxmlstreamwriter.html ?QXmlStreamWriter stream(&output); stream.setAutoFormatting(true); stream.writeStartDocument(); ... stream.writeStartElement("bookmark"); stream.writeAttribute("href", "http://qt-project.org/"); // <----- HERE stream.writeTextElement("title", "Qt Project"); stream.writeEndElement(); // bookmark ... stream.writeEndDocument();
-
hi @jsulm
i think this will write the node again to the xml file,
i want to change the values of existing node attributes.thank you for the reply
-
hi @jsulm
i think this will write the node again to the xml file,
i want to change the values of existing node attributes.thank you for the reply
@Sherlin-N-G You can simply overwrite the whole XML document.
-
@Sherlin-N-G You can simply overwrite the whole XML document.
@jsulm
hi
i have multiple note node inside my XML file , how can i add the set attribute value to particular node node. -
@jsulm
hi
i have multiple note node inside my XML file , how can i add the set attribute value to particular node node.@Sherlin-N-G
You have to (search the document to) select the particular node you want to alter. You can sit inserting/deleting/updating nodes in an XML document for as long as you like. Then you write the complete XML document back to file when you're finished. That's the normal/simplest way of doing things in a whole XML document. Do you have a particular reason for wanting to go down to theQXmlStreamWriter
level? -
hi @JonB i tried with code in the document , i am confused with these 2 line of code
stream.writeAttribute("href", "http://qt-project.org/"); // <----- HERE
stream.writeTextElement("title", "Qt Project");No, i can use any class for over writing but i feel QXmlStreamWriter is easy to implement.
-
@Sherlin-N-G
You have to (search the document to) select the particular node you want to alter. You can sit inserting/deleting/updating nodes in an XML document for as long as you like. Then you write the complete XML document back to file when you're finished. That's the normal/simplest way of doing things in a whole XML document. Do you have a particular reason for wanting to go down to theQXmlStreamWriter
level?@JonB Hi
I dont think so you can read a node you want to alter and write simultaneously using DOM. -
this is my xml file in which i need to change the value of X_value from car to bus by the below code
<Settings>
<insideSetting>
<note>
<X_value>car</X_value>
<Y_value>bike</Y_value>
</note>
</insideSetting>
</Settings>this is my snippet to update a node attribute X_value
void MainWindow::on_pushButton_2_clicked()
{QString output("/something/newXml1.xml"); QXmlStreamWriter stream(&output); stream.setAutoFormatting(true); stream.writeStartDocument(); stream.writeStartElement("Note"); stream.writeAttribute("href", "http://qt-project.org/"); // <----- HERE stream.writeTextElement("X_value", "Qt Project"); stream.writeEndElement(); // Zone // stream.writeEndDocument();
}
please help in this.
-
@JonB Hi
I dont think so you can read a node you want to alter and write simultaneously using DOM.@kishore_hemmady said in How to update a node in xml using QStreamWritter class?:
@JonB Hi
I dont think so you can read a node you want to alter and write simultaneously using DOM.I meant, read the whole doc into DOM, alter, write whole DOM back. For simplicity, personally I wouldn't be doing anything at the whole
QXmlStreamWriter
level. That's why I was asking whether the OP is wedded to that. -
this is my xml file in which i need to change the value of X_value from car to bus by the below code
<Settings>
<insideSetting>
<note>
<X_value>car</X_value>
<Y_value>bike</Y_value>
</note>
</insideSetting>
</Settings>this is my snippet to update a node attribute X_value
void MainWindow::on_pushButton_2_clicked()
{QString output("/something/newXml1.xml"); QXmlStreamWriter stream(&output); stream.setAutoFormatting(true); stream.writeStartDocument(); stream.writeStartElement("Note"); stream.writeAttribute("href", "http://qt-project.org/"); // <----- HERE stream.writeTextElement("X_value", "Qt Project"); stream.writeEndElement(); // Zone // stream.writeEndDocument();
}
please help in this.
@Sherlin-N-G You can use http://doc.qt.io/qt-5/qdomdocument.html
Simply read your whole document, then alter it and write it back to the file. -
above code will not work for updating??
do i need to read from QDom and then write? -
above code will not work for updating??
do i need to read from QDom and then write?