QT XML 读写
首先工程的.pro文件中需要添加 QT += xml
官方解释QDomNode如下
The QDomNode class is the base class for all the nodes in a DOM tree.
Many functions in the DOM return a QDomNode.
You can find out the type of a node using isAttr(), isCDATASection(), isDocumentFragment(), isDocument(), isDocumentType(), isElement(), isEntityReference(), isText(), isEntity(), isNotation(), isProcessingInstruction(), isCharacterData() and isComment().
A QDomNode can be converted into one of its subclasses using toAttr(), toCDATASection(), toDocumentFragment(), toDocument(), toDocumentType(), toElement(), toEntityReference(), toText(), toEntity(), toNotation(), toProcessingInstruction(), toCharacterData() or toComment(). You can convert a node to a null node with clear().
Copies of the QDomNode class share their data using explicit sharing. This means that modifying one node will change all copies. This is especially useful in combination with functions which return a QDomNode, e.g. firstChild(). You can make an independent (deep) copy of the node with cloneNode().
A QDomNode can be null, much like a null pointer. Creating a copy of a null node results in another null node. It is not possible to modify a null node, but it is possible to assign another, possibly non-null node to it. In this case, the copy of the null node will remain null. You can check if a QDomNode is null by calling isNull(). The empty constructor of a QDomNode (or any of the derived classes) creates a null node.
Nodes are inserted with insertBefore(), insertAfter() or appendChild(). You can replace one node with another using replaceChild() and remove a node with removeChild().
To traverse nodes use firstChild() to get a node’s first child (if any), and nextSibling() to traverse. QDomNode also provides lastChild(), previousSibling() and parentNode(). To find the first child node with a particular node name use namedItem().
To find out if a node has children use hasChildNodes() and to get a list of all of a node’s children use childNodes().
The node’s name and value (the meaning of which varies depending on its type) is returned by nodeName() and nodeValue() respectively. The node’s type is returned by nodeType(). The node’s value can be set with setNodeValue().
The document to which the node belongs is returned by ownerDocument().
Adjacent QDomText nodes can be merged into a single node with normalize().
QDomElement nodes have attributes which can be retrieved with attributes().
QDomElement and QDomAttr nodes can have namespaces which can be retrieved with namespaceURI(). Their local name is retrieved with localName(), and their prefix with prefix(). The prefix can be set with setPrefix().
You can write the XML representation of the node to a text stream with save().
The following example looks for the first element in an XML document and prints the names of all the elements that are its direct children.
QDomDocument d;
d.setContent(someXML);
QDomNode n = d.firstChild();
while (!n.isNull()) {
if (n.isElement()) {
QDomElement e = n.toElement();
cout << "Element name: " << e.tagName() << endl;
break;
}
n = n.nextSibling();
}
读XML文件
bool MXmlParser::readXML()
{
QFile file( m_XMLPath );
if( !file.open( QFile::ReadOnly | QFile::Text ) )
{
qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->file.open->%s\n") << XMLPath();
return false;
}
QDomDocument t_doc;
if( !t_doc.setContent( &file ) )
{
qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->doc.setContent\n") << XMLPath();
file.close();
return false;
}
file.close();
QDomElement root = t_doc.documentElement();
QDomNode n = root.firstChild();
while ( !n.isNull() )
{
QDomElement e = n.toElement();
if( !e.isNull())
{
if( e.nodeName() == "context" )
{
QDomNodeList list = e.childNodes(); //获得元素e的所有子节点的列表
for(int a=0; a<list.count(); a++) //遍历该列表
{
QDomNode node = list.at(a);
if(node.isElement())
{
if( node.nodeName() == "message" )
{
QDomNodeList list2 = node.childNodes(); //获得元素node的所有子节点的列表
for(int i=0; i<list2.count(); i++) //遍历该列表
{
QDomNode node2 = list2.at(i);
if(node2.isElement())
{
if( node2.nodeName() == "source" )
{
qDebug() << node2.firstChild().toText().data();
}
}
}
}
}
}
}
}
n = n.nextSibling();
}
return true;
}
修改XML
QDomElement root = t_doc.documentElement();
QDomNode n = root.firstChild();
while ( !n.isNull() )
{
QDomElement e = n.toElement();
if( !e.isNull())
{
if( e.nodeName() == "context" )
{
QDomNodeList list = e.childNodes(); //获得元素e的所有子节点的列表
for(int a=0; a<list.count(); a++) //遍历该列表
{
QDomNode node = list.at(a);
if(node.isElement())
{
if( node.nodeName() == "message" )
{
QDomNodeList list2 = node.childNodes(); //获得元素node的所有子节点的列表
for(int i=0; i<list2.count(); i++) //遍历该列表
{
QDomNode node2 = list2.at(i);
if(node2.isElement())
{
if( node2.nodeName() == "translation" )
{
if (node2.hasChildNodes())
{
QDomNode oldNode = node2.firstChild();
node2.firstChild().setNodeValue("new value");
QDomNode newNode = node2.firstChild();
node2.replaceChild(newNode, oldNode);
}
else
{
QDomElement e = node2.toElement();
e.removeAttribute("type"); //remode an attribute
QDomText newNode;
newNode = t_doc.createTextNode("new node");
node2.appendChild(newNode);
}
}
}
}
}
}
}
}
}
n = n.nextSibling();
}
写XML
if(!file.open(QFile::WriteOnly|QFile::Truncate))
{
return ;
}
QTextStream ts(&file);
ts.reset();
ts.setCodec("utf-8");
t_doc.save(ts, 4, QDomNode::EncodingFromTextStream);
file.close();
```