qt使用QDomDocument读写xml文件

在使用QDomDocument读写xml之前需要在工程文件添加:
QT += xml

1.生成xml文件

void createXml(QString xmlName)
{
	QFile file(xmlName);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate |QIODevice::Text))
        return false;
    QDomDocument doc;
    QDomProcessingInstruction instruction; //添加处理命令
    instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instruction);
/*
<bookstore>
	<book category="c++">
		<price>98</price>
	</book>
	<book category="语文">
		<price>100</price>
	</book>
</bookstore>
*/
	QDomElement root = doc.createElement("bookstore");
    doc.appendChild(root);

	QDomElement book = doc.createElement("book");
    book.setAttribute("category", "C++");	//生成category节点
    root.appendChild(book);
    QDomElement price= doc.createElement("price");
    book.appendChild(price);
    QDomText text = doc.createTextNode("98");
    price.appendChild(text);
	
	book = doc.createElement("book");
	book.setAttribute("category", QString::fromLocal8Bit("语文"));
    root.appendChild(book);
    price= doc.createElement("price");
    book.appendChild(price);
    text = doc.createTextNode("100");
    price.appendChild(text);
    
	QTextStream stream(&file);
	stream.setCodec("UTF_8");
	doc.save(stream,4,QDomNode::EncodingFromTextStream);
	file.close();
}

在这里插入图片描述
2.读取xml文件

void loadXml(QString xmlName)
{
	QFile file(xmlName);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        return;
    }

    QString strError;
    int errorLine;
    int errorColumn;
    QDomDocument doc;
    if(!doc.setContent(&file, false, &strError, &errorLine, &errorColumn)){
        return;
    }
    QDomElement root = doc.documentElement();
    if(root.tagName() == "bookstore")
    {
		QDomNode book = root.firstChild();
		while(!book.isNull())
		{
			if(book.toElement().tagName() == "book")
			{
				QString str = book.toElement().attribute("category");	//获取category属性内容
				qDebug()<<str;
				QDomNode node = book.firstChild();
				while(!node.isNull())
				{
					if(node.toElement().tagName() == "price")
					{
						QString price = node.toElement().text();
						qDebug()<<price;
					}
					node = node.nextSibling();
				}
			}
			book = book.nextSibling();
		}
	}
}

void appendXml(QDomDocument &doc,QDomElement &root)
{

    QDomElement book = doc.createElement("book");
    book.setAttribute("category", "C++");	//生成category节点
    root.appendChild(book);

    QDomElement price= doc.createElement("price");
    book.appendChild(price);
    QDomText text = doc.createTextNode("98");
    price.appendChild(text);

    book = doc.createElement("book");
    book.setAttribute("category", QString::fromLocal8Bit("语文"));
    root.appendChild(book);

    price= doc.createElement("price");
        text = doc.createTextNode("100");
        price.appendChild(text);
    book.appendChild(price);
}


void openFileWriteXML(QString xmlPath)
{
    QFile file(xmlPath);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        return;
    }

    QString strError;
    int errorLine;
    int errorColumn;
    QDomDocument doc;
    if(!doc.setContent(&file, false, &strError, &errorLine, &errorColumn)){
        return;
    }
    QDomElement root = doc.documentElement();


}

在这里插入图片描述

好的,这是一个关于 Qt 的问题。QDomDocumentQt 中用于读写 XML 文件的类。其主要功能是将 XML 文件解析成 DOM 树表示,从而可以方便地进行修改、查询和输出操作。 在 Qt使用 QDomDocument 读写 XML 文件的步骤如下: 1. 创建 QDomDocument 对象,并设置 XML 文件的版本、编码和根节点。 2. 通过 QDomDocument 对象的 createElement() 方法创建节点,并通过 appendChild() 方法将其添加到 DOM 树中。 3. 通过 QDomDocument 对象的 createTextNode() 方法创建文本节点,并通过 appendChild() 方法将其添加到 DOM 树中。 4. 通过 QDomDocument 对象的 toByteArray() 方法将 DOM 树输出到字节数组中,或通过 save() 方法将 DOM 树保存到 XML 文件中。 以下是一个简单的示例代码,演示了如何使用 QDomDocument 读写 XML 文件: ```cpp #include <QFile> #include <QDomDocument> int main() { // 创建 QDomDocument 对象 QDomDocument doc; // 设置 XML 文件的版本和编码 doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"")); // 创建根节点 QDomElement root = doc.createElement("root"); doc.appendChild(root); // 创建子节点 QDomElement child = doc.createElement("child"); root.appendChild(child); // 创建文本节点 QDomText text = doc.createTextNode("Hello, world!"); child.appendChild(text); // 将 DOM 树输出到字节数组中 QByteArray xml = doc.toByteArray(); // 将 DOM 树保存到 XML 文件中 QFile file("test.xml"); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream out(&file); out << doc.toString(); file.close(); } return 0; } ``` 这段代码创建了一个名为 test.xmlXML 文件,其内容为: ```xml <?xml version="1.0" encoding="UTF-8"?> <root> <child>Hello, world!</child> </root> ``` 希望这个示例代码可以帮助你了解如何使用 QDomDocumentQt读写 XML 文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值