Regarding QDomDocument
-
Following code may be helpful for you for getting attribute's value
@
QDomDocument doc("mydocument");
doc.setContent(stringXML); //string having xml
QDomElement docElem = doc.documentElement();
QDomNodeList nodeList = docElem.elementsByTagName("1stTagName").at(0).toElement().elementsByTagName("2ndTagName").at(0).toElement().elementsByTagName("3rdTagName"); //Number of Tags in Ur XMLif (nodeList.count() > 0)
{
QString NodeText(nodeList.at(0).attributes().namedItem("AttributeName").nodeValue());
}
@Edit: please use @ tags around code sections; Andre
-
Rajveer, notice that QDomText is a QDomNode. QDomDocument does give you methods to get those. Then, you can use isCharacterData() and toCharacterData() to get the corresponding node of the right type, and then you can call data() or nodeValue() to get the string.
-
Your question has already been answered.
[quote author="Andre" date="1314607643"]QDomDocument will not do it directly. You will need to get the right QDomText node, and call setData on that node. [/quote].. get the right QDomText node ... has already been answered by DanSiddiqui.
-
Hi lukas
What I am trying to do is i have an xml file,I am performing following steps
1.)Open in ReadWrite mode
2.)Read that file using QDomDocument
3.)Get all the child nodes4.)Now as i have read all tha nodes and are in memory i want to change the value of particular chaild node and update the file with these changes.
-
Hi
Below is my code i tried by not yet success please help
@
QFile file("ABC.xml");
if (!file.open(QIODevice::ReadWrite))
{
qDebug() << "Not able to open the file";
return 1;
}QString errMsg; int errLine; int errCol; QDomDocument doc; if (!doc.setContent(&file,false,&errMsg,&errLine,&errCol)) { qDebug() << "Error in XML File format" <<endl; return 1; } QDomElement root = doc.documentElement(); if (root.tagName() != "TAG") { qDebug() << "Error in TAG" << endl; } QDomNodeList list = root.childNodes(); QDomAttr attr; int iSize = list.size(); QDomNodeList list1 = doc.elementsByTagName("Rajveer"); for (int i=0; i < iSize;i++) { QDomElement elem = list.item(i).toElement(); attr = elem.attributeNode("name"); if(!attr.isNull()) { if(attr.value() == "Rajveer") { QDomText text2 = doc.createTextNode("ITPL-Park2"); elem.appendChild(text2);* } }
}
file.close();
@My file looks something like this
@
<root>
<TAG>
<FIELD name= "Rajveer">value</FIELD>
.
.
.
</TAG>
</root>
@
With above code I am still not able to update the value of required tag in my file please help. -
Did you try to debug the code? Does it find your node? I doubt it, as you do not have INDEX elements in your document. That would mean that line 28 returns an empty list, thus your loop (including the code to set a value) is not executed.
Please, run your code through a debugger, and step through it. It is really enlightening. If you can not figure out how to do that, at least insert qDebug() << LINE; statements at every code junction. That will give you insight in how your code is actually executed.
-
-
If you are interested in a real advice: Stop botching code together and learn how to develop software instead. This includes a basic understanding of how computers work, a basic understanding of software design, programming language and toolkit comprehension and the ability to read, understand and adopt documentation and examples given. This will take time but every hour spent will save another ten later on.
-
Your XML:
@
<FIELD name = “Rajveer”>Value</FIELD>
@Your code:
@
QDomNodeList list1 = doc.elementsByTagName("Rajveer");
@How should it ever find a tag named "Rajveer", if you only have tag named "FIELD".
Are you sure, you did understand the minimal basics of XML?
Did you ever try to debug the code yourself? Either with debug output like qDebug() or with stepping through the code with a debuggger? You would immediately have spottet that you never get into the for loop.
Pleas do yourself and everyone else a favor and learn the basic principles of programming.
Also, make yourself comfortable with the most basic XML stuff, like the differences between tags, attribute names and attribute values.
Else you will fail miserably with your project.
-
Hi All
I have a fixed TAG template which i have saved in Template.xml
Template.xml
@
<TAG>
<ITEM name = "A"></ITEM>
<ITEM name = "B"></ITEM>
<ITEM name = "C"></ITEM>
<ITEM name = "D"></ITEM>
<ITEM name = "E"></ITEM>
</TAG>
@
I read the contents of above file & set the values.
Below is the code for that
@
QDomDocument doc;
QFile file(strFilename);
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "Not able to open the file";
return doc;
}QString errMsg; int errLine; int errCol; if (!doc.setContent(&file,false,&errMsg,&errLine,&errCol)) { qDebug() << "Error in XML File format" <<endl; return doc; } file.close(); QDomElement root = doc.documentElement(); if (root.tagName() != "TAG") { qDebug() << "Error in TAG" << endl; } QDomNodeList list = root.childNodes(); QDomAttr attr; int iSize = list.size(); for (int i=0; i < iSize;i++) { QDomElement elem = list.item(i).toElement(); attr = elem.attributeNode("name"); if(!attr.isNull()) { if(attr.value() == "A") { qDebug() << " Tagname = " << elem.tagName() << " Text Value " << elem.text() << endl; QDomText text2 = doc.createTextNode("ONE"); elem.appendChild(text2); } else if(attr.value() == "B") { qDebug() << " Tagname = " << elem.tagName() << " Text Value " << elem.text() << endl; QDomText text2 = doc.createTextNode("Two"); elem.appendChild(text2); } else if(attr.value() == "C") { qDebug() << " Tagname = " << elem.tagName() << " Text Value " << elem.text() << endl; QDomText text2 = doc.createTextNode("Three"); elem.appendChild(text2); } else if(attr.value() == "D") { qDebug() << " Tagname = " << elem.tagName() << " Text Value " << elem.text() << endl; QDomText text2 = doc.createTextNode("Four"); elem.appendChild(text2); } else if(attr.value() == "E") { qDebug() << " Tagname = " << elem.tagName() << " Text Value " << elem.text() << endl; QDomText text2 = doc.createTextNode("Five"); elem.appendChild(text2); } }
}
@After updating values now I am saving this document in other file.
Below is the code for that
@
QFile generatedFile("Main.xml");
if (!generatedFile.open(QIODevice::WriteOnly))
{
qDebug() << "Not able to open the file";
return 1;
}
QTextStream textStream(&generatedFile);
doc.save(textStream,4);
generatedFile.close();
@Now I want to add header & root element to this Main.xml file & also i want to clone or append as many Tags as above in this file how to do that?
My Main.xml should look like this
@
<!DOCTYPE xml>
<Root>
<TAG>
<ITEM name = "A">1</ITEM>
<ITEM name = "B">2</ITEM>
<ITEM name = "C">3</ITEM>
<ITEM name = "D">5</ITEM>
<ITEM name = "E">4</ITEM>
</TAG>
<TAG>
<ITEM name = "A">11</ITEM>
<ITEM name = "B">12</ITEM>
<ITEM name = "C">11</ITEM>
<ITEM name = "D">41</ITEM>
<ITEM name = "E">44</ITEM>
</TAG>
.
.
.</Root>
@ -
With that badly formatted code there is no wonder that you do not find your errors. Do you really expect us to play the "where is the corresponding brace game" in that big mess?
Please format the code nicely, first of all indent the blocks in a reasonable way, and edit your original post to update the code sections. We might come back and look at it then. Please leave a short comment in the comment box to notify of the update, so that the post shows up again in the listings.
You might want to read http://www.catb.org/~esr/faqs/smart-questions.html before you continue.