• 6460阅读
  • 2回复

一个有关XML读取的问题 [复制链接]

上一主题 下一主题
离线梦在云霄
 
只看楼主 倒序阅读 楼主  发表于: 2007-10-19
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
一个关于XML的问题,希望大家帮帮忙
我已经用QDomDocument将xml文件读取到内存中,并懂得如果取得数据,但是现在我想把这个xml文件的内容进行修改然后在保存回文件比如:
原文件内容是:
<?xml version='1.0' encoding='UTF-8'?>
<root>
    <IP>127.0.0.1</IP>
    <port>2222</port>
</root>
现在我想把他改成:
<?xml version='1.0' encoding='UTF-8'?>
<root>
    <IP>127.0.0.1</IP>
    <port>2000</port>
</root>
然后保存会文件,请问用什么方法可以做到。目前我采用的方法不行

domDocument.documentElement().elementsByTagName("IP").at(0).toElement().setNodeValue(“127.0.0.1”);
domDocument.documentElement().elementsByTagName("port").at(0).toElement().setNodeValue(“2000”);
const int IndentSize = 4;
QFile file("test.xml");
if (!file.open(QFile::WriteOnly | QFile::Text)) {
    QMessageBox::warning(this, tr("XML File"),
                            tr("Cannot write file %1:\n%2.")
                            .arg("system.xml")
                            .arg(file.errorString()));
        return false;
    }
QTextStream out(&file);
domDocument.save(out, IndentSize);
离线guoyun_he

只看该作者 1楼 发表于: 2007-10-19
我前一段时间写了一个函数,该函数带有两个节点,你可以参考一下(红色部分):bool XmlManager::writeValue(QString masterNode, QString slaveNode, QString strValue)
{
    if( docRoot.isNull() )
    {
        cout << "The root node is NULL." << endl;
        return FALSE;
    }

    if("document" != docRoot.tagName())
    {
        cout << "Wrong XML format.The root node name is:" << docRoot.tagName() << endl;
        return FALSE;
    }

    QDomNode node=docRoot.firstChild();                  // Returns the first child of the node. If there is no child node, a null node is returned.
    while (FALSE == node.isNull())
    {
        if (masterNode == node.toElement().tagName())
        {
            QDomNode childNode=node.firstChild();        // Converts a QDomNode into a QDomElement. If the node is not an element the returned object will be null.
            while (FALSE == childNode.isNull())
            {
                if (slaveNode == childNode.nodeName())
                {
                    childNode.firstChild().toElement().setTagName(strValue);
                    file.reset();
                    QTextStream out(&file);
                    doc.save(out, 8);
                    refreshFile();

                    return TRUE;
                }
                childNode = childNode.nextSibling();
            }
        }
        node=node.nextSibling();
    }

    return FALSE;
}
离线梦在云霄
只看该作者 2楼 发表于: 2007-10-19
回 1楼(guoyun_he) 的帖子
多谢guoyun_he的回复,我的问题已经解决了,后来我发现是我的
domDocument.documentElement().elementsByTagName("IP").at(0).toElement().setNodeValue(“127.0.0.1”);
domDocument.documentElement().elementsByTagName("port").at(0).toElement().setNodeValue(“2000”);
这两句并不能对内存中的节点的内容进行修改,我现在采用的方法是
QDomText ip = domDocument.createTextNode(“127.0.0.1”);
QDomNode oldip = domDocument.documentElement().elementsByTagName("IP").at(0).childNodes().at(0);
domDocument.documentElement().elementsByTagName("IP").at(0).replaceChild(ip,oldip);
QDomText port = domDocument.createTextNode("2000");
QDomNode oldport = domDocument.documentElement().elementsByTagName("port").at(0).childNodes().at(0);
domDocument.documentElement().elementsByTagName("port").at(0).replaceChild(port,oldport);
其他的代码没有变
不知道有没有更好一点的方法。
快速回复
限100 字节
 
上一个 下一个