Regarding QDomDocument



  • HI All

    I have one of the tag as shown below in my XML file

    <FIELD name = "Rajveer">Value</FIELD>

    Can anyone tell how to set the Value in above tag using QDomDocument.



  • QDomDocument will not do it directly. You will need to get the right QDomText node, and call setData on that node.



  • HI Andre

    I went through the documentation of QDomText & also QDomDocument
    but i didnt find any function which gives me the QDomText.

    can you tell me how to get required QDomText node.



  • 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 XML

    if (nodeList.count() > 0)
    {
    QString NodeText(nodeList.at(0).attributes().namedItem("AttributeName").nodeValue());
    }
    @

    Edit: please use @ tags around code sections; Andre



  • Hi DanSiddiqui

    Can you please see just refer once what my question was.



  • 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.



  • HI Andre

    I want to change the value of the particular tag.
    How to do that.



  • 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 nodes

    4.)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.



  • Yes. Find the node using elementsByTagName() and set the data using setData() - as DanSiddiqui and Andre said.



  • 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&#41;)
    {
        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.



  • 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.





  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.