在Qt中编写XML文件

September 21st, 2013 by JasonLe's Tech Leave a reply »

对于XML文件,大家应该都会太陌生,只要定义好了XML的格式,我们就可以用来传送数据了。
XML本身没有什么表示,他不像HTML那样表现数据,每个Node也没有什么具体的含义,全部靠编程者自己定义。

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
  <from>Jani</from>
  <to>Tove</to>
  <message>Remember me this weekend</message>
</note>

我们可以看到xml文件中每个root 与children <>中只是为了便于理解写出来的,另一方面就是作为Attribute出现,在read xml时候用的到。
其实写xml非常简单。
Key Code:

    QDomElement root = document.createElement("Books");
    document.appendChild(root);
    for(int i=0;i<10;i++)
    {
        QDomElement node = document.createElement("Book");
        node.setAttribute("Name","My Book "+QString::number(i));
        node.setAttribute("ID",QString::number(i));
        root.appendChild(node);
        for(int h=0;h<5;h++)
        {
            QDomElement chapter = document.createElement("Chapter");
            chapter.setAttribute("Name","Chapter "+QString::number(h));
            chapter.setAttribute("ID",QString::number(h));
            node.appendChild(chapter);
        }
    }

打印出来的效果就是:Books下面各种子节点。

<Books>
<Book Name="My Book 0" ID="0">
<Chapter Name="Chapter 0" ID="0"/>
<Chapter Name="Chapter 1" ID="1"/>
<Chapter Name="Chapter 2" ID="2"/>
<Chapter Name="Chapter 3" ID="3"/>
<Chapter Name="Chapter 4" ID="4"/>
</Book>
<Book Name="My Book 1" ID="1">
<Chapter Name="Chapter 0" ID="0"/>
<Chapter Name="Chapter 1" ID="1"/>
<Chapter Name="Chapter 2" ID="2"/>
<Chapter Name="Chapter 3" ID="3"/>
<Chapter Name="Chapter 4" ID="4"/>
</Book>
<Book Name="My Book 2" ID="2">
<Chapter Name="Chapter 0" ID="0"/>
<Chapter Name="Chapter 1" ID="1"/>
<Chapter Name="Chapter 2" ID="2"/>
<Chapter Name="Chapter 3" ID="3"/>
<Chapter Name="Chapter 4" ID="4"/>
</Book>
<Book Name="My Book 3" ID="3">
<Chapter Name="Chapter 0" ID="0"/>
<Chapter Name="Chapter 1" ID="1"/>
<Chapter Name="Chapter 2" ID="2"/>
<Chapter Name="Chapter 3" ID="3"/>
<Chapter Name="Chapter 4" ID="4"/>
</Book>
<Book Name="My Book 4" ID="4">
<Chapter Name="Chapter 0" ID="0"/>
<Chapter Name="Chapter 1" ID="1"/>
<Chapter Name="Chapter 2" ID="2"/>
<Chapter Name="Chapter 3" ID="3"/>
<Chapter Name="Chapter 4" ID="4"/>
</Book>
<Book Name="My Book 5" ID="5">
<Chapter Name="Chapter 0" ID="0"/>
<Chapter Name="Chapter 1" ID="1"/>
<Chapter Name="Chapter 2" ID="2"/>
<Chapter Name="Chapter 3" ID="3"/>
<Chapter Name="Chapter 4" ID="4"/>
</Book>
<Book Name="My Book 6" ID="6">
<Chapter Name="Chapter 0" ID="0"/>
<Chapter Name="Chapter 1" ID="1"/>
<Chapter Name="Chapter 2" ID="2"/>
<Chapter Name="Chapter 3" ID="3"/>
<Chapter Name="Chapter 4" ID="4"/>
</Book>
<Book Name="My Book 7" ID="7">
<Chapter Name="Chapter 0" ID="0"/>
<Chapter Name="Chapter 1" ID="1"/>
<Chapter Name="Chapter 2" ID="2"/>
<Chapter Name="Chapter 3" ID="3"/>
<Chapter Name="Chapter 4" ID="4"/>
</Book>
<Book Name="My Book 8" ID="8">
<Chapter Name="Chapter 0" ID="0"/>
<Chapter Name="Chapter 1" ID="1"/>
<Chapter Name="Chapter 2" ID="2"/>
<Chapter Name="Chapter 3" ID="3"/>
<Chapter Name="Chapter 4" ID="4"/>
</Book>
<Book Name="My Book 9" ID="9">
<Chapter Name="Chapter 0" ID="0"/>
<Chapter Name="Chapter 1" ID="1"/>
<Chapter Name="Chapter 2" ID="2"/>
<Chapter Name="Chapter 3" ID="3"/>
<Chapter Name="Chapter 4" ID="4"/>
</Book>
</Books>               

写XML,最主要就是分清层级关系,要分清root与children,这样才能写出对的XML文件。