How to update child-data in XML file?
-
Hi.
I'd like to change "commonPrimaryUri" value in this XML file.
As follow.<?xml version="1.0" encoding="UTF-8"?> <tResource version="1.3.0"> <common> <commonPrimaryUri>http://192.168.39.91:8000/common-shop/</commonPrimaryUri> </common> </thincaResource>
I wrote this code.
It can works, but this result is different from what I expected.QDomDocument xmlBOM; QFile f("/home/root/d/resource/resource.xml"); if (f.open(QIODevice::ReadOnly)) { xmlBOM.setContent(&f); f.close(); QDomNodeList domList = xmlBOM.elementsByTagName("common"); QDomElement element = domList.at(0).toElement(); element.setAttribute("commonPrimaryUri", url); if (f.open(QIODevice::Truncate | QIODevice::WriteOnly)) { QByteArray xml = xmlBOM.toByteArray(); f.write(xml); f.close(); } }
this is result.
<?xml version='1.0' encoding='UTF-8'?> <thincaResource version="1.3.0"> <common commonPrimaryUri="http://1.2.3.4:8000/common-shop/"> <commonPrimaryUri>http://192.168.39.91:8000/common-shop/</commonPrimaryUri> ...
I don't want to change <common> data.
I'd like to change <commonPrimaryUri> data.
like this<common> <commonPrimaryUri>http://1.2.3.4:8000/common-shop/</commonPrimaryUri> </common>
Which part should I change?
Thanks. -
Hi.
I'd like to change "commonPrimaryUri" value in this XML file.
As follow.<?xml version="1.0" encoding="UTF-8"?> <tResource version="1.3.0"> <common> <commonPrimaryUri>http://192.168.39.91:8000/common-shop/</commonPrimaryUri> </common> </thincaResource>
I wrote this code.
It can works, but this result is different from what I expected.QDomDocument xmlBOM; QFile f("/home/root/d/resource/resource.xml"); if (f.open(QIODevice::ReadOnly)) { xmlBOM.setContent(&f); f.close(); QDomNodeList domList = xmlBOM.elementsByTagName("common"); QDomElement element = domList.at(0).toElement(); element.setAttribute("commonPrimaryUri", url); if (f.open(QIODevice::Truncate | QIODevice::WriteOnly)) { QByteArray xml = xmlBOM.toByteArray(); f.write(xml); f.close(); } }
this is result.
<?xml version='1.0' encoding='UTF-8'?> <thincaResource version="1.3.0"> <common commonPrimaryUri="http://1.2.3.4:8000/common-shop/"> <commonPrimaryUri>http://192.168.39.91:8000/common-shop/</commonPrimaryUri> ...
I don't want to change <common> data.
I'd like to change <commonPrimaryUri> data.
like this<common> <commonPrimaryUri>http://1.2.3.4:8000/common-shop/</commonPrimaryUri> </common>
Which part should I change?
Thanks.@nishiokas said in How to update child-data in XML file?:
element.setAttribute("commonPrimaryUri", url);
This sets an attribute on (the first)
xmlBOM.elementsByTagName("common");
, resulting in<common commonPrimaryUri="http://1.2.3.4:8000/common-shop/">
.You say you don't want that. You want to operate on the
<commonPrimaryUri>
child element of<common>
. So do that. (Visit<common>
children, orelement.elementsByTagName("commonPrimaryUri")
.) -
@JonB Thank you for your reply!
(Visit <common> children, or element.elementsByTagName("commonPrimaryUri").)
I used "element.elementsByTagName("commonPrimaryUri")" instead of "xmlBOM.elementsByTagName("common")".
But I couldn't get positive result.QDomNodeList domList = xmlBOM.elementsByTagName("commonPrimaryUri"); QDomElement element = domList.at(0).toElement(); element.setAttribute("", "http://1.2.3.4:8000/common-shop/"); // result XML // <commonPrimaryUri ="http://1.2.3.4:8000/common-shop/">http://192.168.39.91:8000/common-shop/</commonPrimaryUri> element.setNodeValue("http://1.2.3.4:8000/common-shop/"); //retult XML //nothing change element.setTagName("http://1.2.3.4:8000/common-shop/"); //retult XML // <http://1.2.3.4:8000/pos-api/close>http://192.168.39.91:8000/pos-api/close</http://1.2.3.4:8000/pos-api/close>
I'm afraid I don't understand how to visit "<common> children".
Should I use "appendChld" in QDomNodeList?How should I call this potision? "<tag>here!</tag>"
I think it's "Value" but setNodeValue couldn't change anything.thanks.
-
@JonB Thank you for your reply!
(Visit <common> children, or element.elementsByTagName("commonPrimaryUri").)
I used "element.elementsByTagName("commonPrimaryUri")" instead of "xmlBOM.elementsByTagName("common")".
But I couldn't get positive result.QDomNodeList domList = xmlBOM.elementsByTagName("commonPrimaryUri"); QDomElement element = domList.at(0).toElement(); element.setAttribute("", "http://1.2.3.4:8000/common-shop/"); // result XML // <commonPrimaryUri ="http://1.2.3.4:8000/common-shop/">http://192.168.39.91:8000/common-shop/</commonPrimaryUri> element.setNodeValue("http://1.2.3.4:8000/common-shop/"); //retult XML //nothing change element.setTagName("http://1.2.3.4:8000/common-shop/"); //retult XML // <http://1.2.3.4:8000/pos-api/close>http://192.168.39.91:8000/pos-api/close</http://1.2.3.4:8000/pos-api/close>
I'm afraid I don't understand how to visit "<common> children".
Should I use "appendChld" in QDomNodeList?How should I call this potision? "<tag>here!</tag>"
I think it's "Value" but setNodeValue couldn't change anything.thanks.
@nishiokas
Please don't introducesetAttribute()
orsetTagName()
, they are not what you want and just unnecessarily complicate the code.element.setNodeValue("http://1.2.3.4:8000/common-shop/");
If I am not mistaken,
<commonPrimaryUri>
does not directly have the value. Rather, it has a child node, of typeQDomText
, which has the text you want to alter. Try enumerating<commonPrimaryUri>
's children to see if I am right.