编辑QDomElement的值?

时间:2011-07-19 20:35:46

标签: c++ qt qt4 qtxml

我需要编辑QDomElement的文本 - 例如

我有一个XML文件,其内容为 -

<root>    
    <firstchild>Edit text here</firstchild>
</root>

如何编辑子元素<firstchild>的文本?

我没有在Qt 4.7中提供的QDomDocument类描述的QDomElement中看到任何函数

Edit1 - 我正在添加更多细节。

我需要阅读,修改和保存xml文件。格式化文件如下 -

<root>    
    <firstchild>Edit text here</firstchild>
</root>

需要编辑元素的值。读取xml文件的代码是 -

QFile xmlFile(".\\iWantToEdit.xml");
xmlFile.open(QIODevice::ReadWrite);

QByteArray xmlData(xmlFile.readAll());

QDomDocument doc;
doc.setContent(xmlData);
     

//读取必要的值

     

//回写修改后的值?

注意:我尝试将QDomElement强制转换为QDomNode并使用函数setNodeValue()。但它不适用于QDomElement。

我们非常欢迎任何建议,代码示例和链接。

6 个答案:

答案 0 :(得分:18)

这将做你想要的(你发布的代码将保持原样):

// Get element in question
QDomElement root = doc.documentElement();
QDomElement nodeTag = root.firstChildElement("firstchild");

// create a new node with a QDomText child
QDomElement newNodeTag = doc.createElement(QString("firstchild")); 
QDomText newNodeText = doc.createTextNode(QString("New Text"));
newNodeTag.appendChild(newNodeText);

// replace existing node with new node
root.replaceChild(newNodeTag, nodeTag);

// Write changes to same file
xmlFile.resize(0);
QTextStream stream;
stream.setDevice(&xmlFile);
doc.save(stream, 4);

xmlFile.close();

......你们都已经准备好了。你当然也可以写一个不同的文件。在这个例子中,我只是截断了现有文件并覆盖了它。

答案 1 :(得分:4)

当你想要更改节点内的文本时,只需用更好更简单的解决方案(类似于Lol4t0写的)来更新它。 &#39; firstchild&#39;内的文字节点实际上变成了一个文本节点,所以你想要做的是:

...
QDomDocument doc;
doc.setContent(xmlData);
doc.firstChildElement("firstchild").firstChild().setNodeValue(‌​"new text");

注意额外的firstChild()调用,它将实际访问文本节点并允许您更改值。与创建新节点和替换整个节点相比,这更简单,更快速,更少侵入。

答案 2 :(得分:2)

问题是什么。你想写什么样的价值观? 例如,暂停代码转换此xml

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <node attribute="value">
        <inner_node inner="true"/>
        text
    </node>
</document>

<?xml version='1.0' encoding='UTF-8'?>
<document>
    <new_amazing_tag_name attribute="foo">
        <bar inner="true"/>new amazing text</new_amazing_tag_name>
</document>

代码:

QFile file (":/xml/document");
file.open(QIODevice::ReadOnly);
QDomDocument document;
document.setContent(&file);
QDomElement documentTag = document.documentElement();
qDebug()<<documentTag.tagName();

QDomElement nodeTag = documentTag.firstChildElement();
qDebug()<<nodeTag.tagName();
nodeTag.setTagName("new_amazing_tag_name");
nodeTag.setAttribute("attribute","foo");
nodeTag.childNodes().at(1).setNodeValue("new amazing text");

QDomElement innerNode = nodeTag.firstChildElement();
innerNode.setTagName("bar");
file.close();

QFile outFile("xmlout.xml");
outFile.open(QIODevice::WriteOnly);
QTextStream stream;
stream.setDevice(&outFile);
stream.setCodec("UTF-8");
document.save(stream,4);
outFile.close();

答案 3 :(得分:1)

这是您的代码版本,可以满足您的需求。请注意,当spraff说,关键是找到文本类型的“firstchild”节点的子节点 - 这就是文本存在于DOM中的位置。

   QFile xmlFile(".\\iWantToEdit.xml");
    xmlFile.open(QIODevice::ReadWrite);

    QByteArray xmlData(xmlFile.readAll());

    QDomDocument doc;
    doc.setContent(xmlData);

    // Get the "Root" element
     QDomElement docElem = doc.documentElement();

    // Find elements with tag name "firstchild"
    QDomNodeList nodes = docElem.elementsByTagName("firstchild"); 

    // Iterate through all we found
    for(int i=0; i<nodes.count(); i++)
    {
        QDomNode node = nodes.item(i);

        // Check the node is a DOM element
        if(node.nodeType() == QDomNode::ElementNode)
        {
            // Access the DOM element
            QDomElement element = node.toElement(); 

            // Iterate through it's children
            for(QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
            {
                // Find the child that is of DOM type text
                 QDomText t = n.toText();
                 if (!t.isNull())
                 {
                    // Print out the original text
                    qDebug() << "Old text was " << t.data();
                    // Set the new text
                    t.setData("Here is the new text");
                 }
            }
        }
    }

    // Save the modified data
    QFile newFile("iEditedIt.xml");
    newFile.open(QIODevice::WriteOnly);
    newFile.write(doc.toByteArray());
    newFile.close();

答案 4 :(得分:0)

Here is Solution to update xml tag using QdomDocument,It's work for linux ubuntu 18.04  
 steps is open xmlfile in readwrite mode, setContent of file in object of qdomdocument,update node value, save xmlfile and close xmlfile.

        QString filepath= QDir::homePath() + "/Book.xml";
        QFile file (filepath);
        file.open(QIODevice::ReadWrite);
        QDomDocument doc;
        doc.setContent(&file); doc.documentElement().firstChildElement("BookPrice").firstChild().setNodeValue(QString("$7777"));
         file.resize(0);
         QTextStream stream;
         stream.setDevice(&file);
         doc.save(stream, 4);
         file.close();

答案 5 :(得分:-2)

将抽象级别上升到QDomNodefirstchild是一个QDomText元素,因此您可以value()setValue(x)使用文本本身。