Regarding QDomDocument
-
As long as you are not willing to put the effort to properly format your code (as I, I seem to remember, told you before), I am not willing to put in the effort to read it.
-
Andre i have formated the code.
-
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.
-
Hi Andre
I have updated my code please have a check in debug also even if i am getting the pointer I am not ale to update the value in the file.
-
No, you will need to do the debugging of your code. If you want me to do it, reference the second line in my signature.
I would suggest you read up on your XML knowledge of what an element is, and thus, what an element name is. Hint: it is not "Rajveer".
-
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.
-
HI volker
I edit window the code is appearing as nicely indented but when i see the preview it shows messed up indentation.
-
Why do you not construct the whole XML using QDom methods but open an XML template and try to modify and duplicate this? This is quite strange at best, as you have the structure of the XML hardcoded in your method anyways (by means of the huge if/else blocks).
-
Hi volker
I know it is redundant way,but is it possible to get the result withthe same.
-
"QDomNode::cloneNode() ":http://doc.qt.nokia.com/4.7/qdomnode.html#cloneNode should be your friend.
-
Rajveer, why do you keep opening topics on the same issue?
http://developer.qt.nokia.com/forums/viewthread/9240/
http://developer.qt.nokia.com/forums/viewthread/8947/
http://developer.qt.nokia.com/forums/viewthread/7905/
http://developer.qt.nokia.com/forums/viewthread/7704/You don't seem to be making much progress, to be honest.
-
Hi All
My XML tag format is as shown below
@
<INFO>
<FIELD name = "ONE"></FIELD>
.
.
.
600
</INFO>My final XML file should be like this
<?xml version="1.0"?>
<root>
<INFO>....</INFO>
<INFO>....</INFO>
<INFO>....</INFO>
<INFO>....</INFO>
.
.
.
50000
</root>
@
I want to create a xml file with 50000 tags as shown above.Below is my code
@
QDomDocument mainDoc;
QDomProcessingInstruction instr = mainDoc.createProcessingInstruction("xml","version="1.0"");
mainDoc.appendChild(instr);
QDomElement root = mainDoc.createElement("root");
mainDoc.appendChild(root);QDomDocument doc; QString errMsg; int errLine; int errCol;
QString strItemTemplateData;//In this i have my Tag INFO stored
if (!doc.setContent(strItemTemplateData,false,&errMsg,&errLine,&errCol))
{
qDebug() << "Error in XML File format" <<endl;
}
for(int i=0;i<50000;i++)
{
QDomNode clonedNode = doc.cloneNode(true);
root.appendChild(clonedNode);
}
QFile generatedFile(strXMLFileName);
if (!generatedFile.open(QIODevice::WriteOnly))
{
qDebug() << "Error in Filet" <<endl;
return;
}
QTextStream textStream(&generatedFile);
mainDoc.save(textStream,4);
generatedFile.close();
@My code above is performing very slow i.e it is taking lot of time & also some times it is crashing.
Can anybody tell me how to create such a big file with good performance.
And While creating the TAGS i have to set values for each TAG items for which i have to check each attribute value and than set it.
eg:
@
<FIELD name="ONE">USA</FIELD>
@[EDIT: three dots are enough, Volker]
-
QDomDocument is not the way how to handle big XML documents, but you have been told numerous times:
http://developer.qt.nokia.com/forums/viewthread/9483/
http://developer.qt.nokia.com/forums/viewthread/9240/
http://developer.qt.nokia.com/forums/viewthread/8947/
http://developer.qt.nokia.com/forums/viewthread/7905/
http://developer.qt.nokia.com/forums/viewthread/7704/So - please - stop creating topics on the same issue over and over again and stick to the ones you've already created.
-
Hi Lukas
Not in anyone of the links u pasted has mentioned that
QDomDocument is not the way how to handle big XML documents
So please see my question and purpose of my question. -
You may ask multiple questions in the same topic as long as they are related. There is no need to create a new one.
Your application is slow and/or crashes because either your code is broken or for the simple fact that QDomDocument represents the whole XML structure in memory. If your XML structure contains hundreds of thousands of nodes you simply run out of memory and your application crashes. This is why you've been shown how to use QXmlStreamReader / QXmlStreamWriter instead which does not suffer from this problem and/or how to restructure your XML design so it isn't that bloated.
-
Moderator's note:
I have merged the two threads "XML File generation using QDomDocument" (was http://developer.qt.nokia.com/forums/viewthread/9483/) from September and "Problem in creating XML file using QDomDocument" (http://developer.qt.nokia.com/forums/viewthread/10663/) from today into this thread. The all are on the same topic.
Please stop creating new threads on the same topic.
Also, please mind to format your code so that it can be read easily. This includes proper indenting. I doubt that many people take care have a look your code anymore if it's so hard to read.
Overall, please read and understand: http://www.catb.org/~esr/faqs/smart-questions.html