📄 xml.cpp
字号:
#include "xml.h"#include <qfile.h> #include <qtextcodec.h> #include <qtextstream.h> CXMLReader::CXMLReader(){ m_FileName = "";}CXMLReader::CXMLReader(QString filename){ m_FileName = filename;}CXMLReader::~CXMLReader(){}//打开XML文件bool CXMLReader::OpenDomXMLFile(){ QFile opmlFile(m_FileName); if (!opmlFile.open(IO_ReadOnly)) return false; QString szError = NULL; int iErrorLine = 0, iErrorColumn = 0; if (!m_DomTree.setContent(&opmlFile, &szError, &iErrorLine, &iErrorColumn)) { opmlFile.close(); printf("--@@Open XML File:%s Error row:%d col:%d Data:%s @@--\n", m_FileName.data(), iErrorLine, iErrorColumn, szError.data()); return false; } opmlFile.close(); return true;}//保存XML文件bool CXMLReader::SaveXmlFile(){ m_Mutex.lock(); QFile opmlFile(m_FileName); if (!opmlFile.open(IO_ReadWrite)) { m_Mutex.unlock(); return false; } QTextStream ts(&opmlFile); ts << m_DomTree.toString(4); opmlFile.close(); m_Mutex.unlock(); return true;}//取得父节点szDomName下的一个子节点szNodeName,匹配在第一个找到的节点QDomNode CXMLReader::GetDomNode(QString szDomName, QString szNodeName){ QDomElement root = m_DomTree.documentElement(); QDomNode node, childnode, ret; node = root.firstChild(); while (!node.isNull()) { if (node.isElement() && node.nodeName().lower() == szDomName.lower()) { QDomElement childelement = node.toElement(); childnode = childelement.firstChild(); while (!childnode.isNull()) { if (childnode.isElement() && childnode.nodeName().lower() == szNodeName.lower()) { return childnode; } childnode = childnode.nextSibling(); } } node = node.nextSibling(); } return ret;}//取得父节点szDomName下的一个子节点,匹配在第一个找到的节点的属性szAttrName的值相等szAttrValueQDomNode CXMLReader::GetDomNode(QString szDomName, QString szAttrName, QString szAttrValue){ QDomElement root = m_DomTree.documentElement(); QDomNode node, childnode, ret; node = root.firstChild(); while (!node.isNull()) { if (node.isElement() && node.nodeName().lower() == szDomName.lower()) { QDomElement childelement = node.toElement(); childnode = childelement.firstChild(); while (!childnode.isNull()) { QDomNamedNodeMap dm = childnode.toElement().attributes(); for (int i=0; i<(int)dm.count(); i++) { if (dm.item(i).nodeName().lower() == szAttrName.lower()) { if (dm.item(i).nodeValue() == szAttrValue) { return childnode; } } } childnode = childnode.nextSibling(); } } node = node.nextSibling(); } return ret;}//同上,只是一次查找时利用两个属性来共同查找QDomNode CXMLReader::GetDomNode(QString szDomName, QString szAttr1Name, QString szAttr1Value, QString szAttr2Name, QString szAttr2Value){ QDomElement root = m_DomTree.documentElement(); QDomNode node, childnode, ret; node = root.firstChild(); while (!node.isNull()) { if (node.isElement() && node.nodeName().lower() == szDomName.lower()) { QDomElement childelement = node.toElement(); childnode = childelement.firstChild(); while (!childnode.isNull()) { QDomNamedNodeMap dm = childnode.toElement().attributes(); int iIndex1 = -1, iIndex2 = -1; for (int i=0; i<(int)dm.count(); i++) { if (dm.item(i).nodeName().lower() == szAttr1Name.lower()) iIndex1 = i; else if (dm.item(i).nodeName().lower() == szAttr2Name.lower()) iIndex2 = i; } if (iIndex1 >= 0 && iIndex2 >= 0) { if (dm.item(iIndex1).nodeValue() == szAttr1Value && dm.item(iIndex2).nodeValue() == szAttr2Value) { return childnode; } } childnode = childnode.nextSibling(); } } node = node.nextSibling(); } return ret;}//取得父节点szDomName下的一个子节点szNodeName,匹配在第一个找到的节点的属性szAttrName的值相等szAttrValueQDomNode CXMLReader::GetDomNode(QString szDomName, QString szNodeName, QString szAttrName, QString szAttrValue){ QDomElement root = m_DomTree.documentElement(); QDomNode node, childnode, ret; node = root.firstChild(); while (!node.isNull()) { if (node.isElement() && node.nodeName().lower() == szDomName.lower()) { QDomElement childelement = node.toElement(); childnode = childelement.firstChild(); while (!childnode.isNull()) { if (childnode.isElement() && childnode.nodeName().lower() == szNodeName.lower()) { QDomNamedNodeMap dm = childnode.toElement().attributes(); for (int i=0; i<(int)dm.count(); i++) { if (dm.item(i).nodeName().lower() == szAttrName.lower()) { if (dm.item(i).nodeValue() == szAttrValue) { return childnode; } } } } childnode = childnode.nextSibling(); } } node = node.nextSibling(); } return ret;}//同上,只是一次查找时利用两个属性来共同查找QDomNode CXMLReader::GetDomNode(QString szDomName, QString szNodeName, QString szAttr1Name, QString szAttr1Value, QString szAttr2Name, QString szAttr2Value){ QDomElement root = m_DomTree.documentElement(); QDomNode node, childnode, ret; node = root.firstChild(); while (!node.isNull()) { if (node.isElement() && node.nodeName().lower() == szDomName.lower()) { QDomElement childelement = node.toElement(); childnode = childelement.firstChild(); while (!childnode.isNull()) { if (childnode.isElement() && childnode.nodeName().lower() == szNodeName.lower()) { QDomNamedNodeMap dm = childnode.toElement().attributes(); int iIndex1 = -1, iIndex2 = -1; for (int i=0; i<(int)dm.count(); i++) { if (dm.item(i).nodeName().lower() == szAttr1Name.lower()) iIndex1 = i; else if (dm.item(i).nodeName().lower() == szAttr2Name.lower()) iIndex2 = i; } if (iIndex1 == -1 || iIndex2 == -1) return ret; if (dm.item(iIndex1).nodeValue() == szAttr1Value && dm.item(iIndex2).nodeValue() == szAttr2Value) { return childnode; } } childnode = childnode.nextSibling(); } } node = node.nextSibling(); } return ret;}//根据子节点名称szNodeName,子节点的属性名szAttrName,属性值szAttrValue,在节点DomNode中查找子节点QDomNode CXMLReader::GetChildNode(QDomNode& DomNode, QString szNodeName, QString szAttrName, QString szAttrValue){ QDomNode childnode, ret; if (DomNode.isNull()) return ret; QDomElement childelement = DomNode.toElement(); childnode = childelement.firstChild(); while (!childnode.isNull()) { if (childnode.isElement() && childnode.nodeName().lower() == szNodeName.lower()) { if (szAttrName == "") { return childnode; } QDomNamedNodeMap dm; dm = childnode.toElement().attributes(); for (int i=0; i<(int)dm.count(); i++) { if (dm.item(i).nodeName().lower() == szAttrName.lower()) { if (dm.item(i).nodeValue() == szAttrValue) { return childnode; } } } } childnode = childnode.nextSibling(); } return ret;}//同上,只是一次查找时利用两个属性来共同查找QDomNode CXMLReader::GetChildNode(QDomNode& DomNode, QString szNodeName, QString szAttr1Name, QString szAttr1Value, QString szAttr2Name, QString szAttr2Value){ QDomNode childnode, ret; if (DomNode.isNull()) return ret; QDomElement childelement = DomNode.toElement(); childnode = childelement.firstChild(); while (!childnode.isNull()) { if (childnode.isElement() && childnode.nodeName().lower() == szNodeName.lower()) { QDomNamedNodeMap dm; dm = childnode.toElement().attributes(); int iIndex1 = -1, iIndex2 = -1; for (int i=0; i<(int)dm.count(); i++) { if (dm.item(i).nodeName().lower() == szAttr1Name.lower()) iIndex1 = i; else if (dm.item(i).nodeName().lower() == szAttr2Name.lower()) iIndex2 = i; } if (iIndex1 == -1 || iIndex2 == -1) return ret; if (dm.item(iIndex1).nodeValue() == szAttr1Value && dm.item(iIndex2).nodeValue() == szAttr2Value) { return childnode; } } childnode = childnode.nextSibling(); } return ret;}//取得Node的属性szAttrName的值szAttrValue(用来返回值)bool CXMLReader::GetNodeAttr(QDomNode& Node, QString szAttrName, QString& szAttrValue){ szAttrValue = ""; if (Node.isNull()) return false; QDomNamedNodeMap dm = Node.toElement().attributes(); for (int i=0; i<(int)dm.count(); i++) { if (dm.item(i).nodeName().lower() == szAttrName.lower()) { QString sztmp = dm.item(i).nodeValue(); QTextCodec* codec = QTextCodec::codecForName("GB2312"); QCString szutf = codec->fromUnicode(sztmp); szAttrValue = szutf.data(); return true; } } return false;}//取得Node下子节点的个数int CXMLReader::GetChildNodeCount(QDomNode& Node){ if (Node.isNull()) return 0; if (!Node.hasChildNodes()) return 0; QDomNodeList nodelist = Node.childNodes(); return nodelist.count();}//在szDomName节点的最后插入子节点szNodeNameQDomNode CXMLReader::InsertDomNode(QString szDomName, QString szNodeName){ m_Mutex.lock(); QDomElement root = m_DomTree.documentElement(); QDomNode node, DomNode, ret; node = root.firstChild(); while (!node.isNull()) { if (node.isElement() && node.nodeName().lower() == szDomName.lower()) { QDomElement childelement = m_DomTree.createElement(szNodeName); node.appendChild(childelement); DomNode = node.lastChild(); m_Mutex.unlock(); return DomNode; } node = node.nextSibling(); } m_Mutex.unlock(); return ret;}//在DomNode节点的最后插入子节点szChildName,返回新插入子节点的指针QDomNode CXMLReader::InsertChildNode(QDomNode& DomNode, QString szChildName){ m_Mutex.lock(); QDomElement childelement = m_DomTree.createElement(szChildName); DomNode.appendChild(childelement); m_Mutex.unlock(); return DomNode.lastChild();}//在Node节点中添加或修改属性szAttrName,值为szAttrValuebool CXMLReader::InsertNodeAttr(QDomNode& Node, QString szAttrName, QString szAttrValue){ m_Mutex.lock(); QDomElement childelement = Node.toElement(); childelement.setAttribute(szAttrName, szAttrValue); m_Mutex.unlock(); return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -