<?xml version="1.0" encoding="UTF-8"?> 
< sdkguid = " ##GUID" > < inmethod = " SetModbusParaInfo" > < Devicename = " Device1" > < modeupdateCycUnit = " ms" timeOutUnit = " ms" resendTimes = " 3" timeOut = " 0" updateCyc = " 100" deviceId = " 1" value = " 2" /> < ethnetmask = " 255.255.255.0" ip = " 192.168.90.245" port = " 10000" /> < serialserial = " COM1" dataBits = " 8" baudRate = " 9600" parity = " NONE" stopBit = " 1" /> < registeroffset = " 1" func = " 0" name = " Para0" dataFormat = " 0" /> < registeroffset = " 1" func = " 0" name = " Para1" dataFormat = " 0" /> </ Device> </ in> </ sdk> #ifndef MANAGETREEXML_H
#define MANAGETREEXML_H
#include <QTreeWidget>
#include <QDomDocument>typedef struct RegisterPara {RegisterPara() {func_ = "0";offset_ = "1";dataFormat_ = "0";}QString func_;QString offset_;QString dataFormat_;
}RegisterPara;
Q_DECLARE_METATYPE(RegisterPara)typedef struct DevicePara {DevicePara() {mode_ = "0";deviceId_ = "1";updateCyc_ = "100";updateCycUnit_ = "ms";timeOut_ = "0";timeOutUnit_ = "ms";resendTimes_ = "3";serial_ = "COM1";baudRate_ = "9600";dataBits_ = "8";stopBit_ = "1";parity_ = "NONE";ipAddress_ = "192.168.90.245";netmask_ = "255.255.255.0";port_ = "10000";}QString mode_;QString deviceId_;QString updateCyc_;QString updateCycUnit_;QString timeOut_;QString timeOutUnit_;QString resendTimes_;QString serial_;QString baudRate_;QString dataBits_;QString stopBit_;QString parity_;QString ipAddress_;QString netmask_;QString port_;QHash<QString, RegisterPara*> registerPara_;
}DevicePara;
Q_DECLARE_METATYPE(DevicePara)enum ItemType {DeviceEnum = QTreeWidgetItem::UserType,RegisterEnum
};class ManageTreeXml
{
public:ManageTreeXml();bool SaveTreeXml(QTreeWidget* pTree, QString xmlPath);bool LoadTreeXml(QTreeWidget* pTree, QString xmlPath);private:bool showXml(QTreeWidget* pTree, QDomDocument *pDoc, bool bEditable);bool showXmlTreeNode(QTreeWidget* pTree, QDomNode domParent, QTreeWidgetItem *treeParent, bool bEditable);void setTreeNodeText(QDomNode Node, QTreeWidgetItem *treeNode, DevicePara* devicePara);void SaveItem(QDomElement& root,QTreeWidgetItem* item,QDomDocument& doc);
};#endif // MANAGETREEXML_H# include  "manageTreexml.h" # include  <QStack> # include  <QFile> # include  <QDebug> ManageTreeXml :: ManageTreeXml ( ) 
{ } bool  ManageTreeXml :: SaveTreeXml ( QTreeWidget * pTree,  QString xmlPath) 
{ QDomDocument doc; QDomProcessingInstruction head =  doc. createProcessingInstruction ( "xml" ,  "version=\"1.0\" encoding=\"UTF-8\"" ) ; doc. appendChild ( head) ; QTreeWidgetItem * rootItem =  pTree-> invisibleRootItem ( ) ;  QDomElement sdk =  doc. createElement ( "sdk" ) ; sdk. setAttribute ( "guid" ,  "##GUID" ) ; doc. appendChild ( sdk) ; QDomElement root=  doc. createElement ( "in" ) ; root. setAttribute ( "method" ,  "SetModbusParaInfo" ) ; sdk. appendChild ( root) ; SaveItem ( root,  rootItem,  doc) ; QFile file ( xmlPath) ; if  ( ! file. open ( QIODevice:: WriteOnly) )  { return  false ; } QString xml =  doc. toString ( ) ; QTextStream txtOutput ( & file) ; txtOutput. setCodec ( "UTF-8" ) ; txtOutput<< xml; file. close ( ) ; return  true ; 
} bool  ManageTreeXml :: LoadTreeXml ( QTreeWidget * pTree,  QString xmlPath) 
{ pTree-> clear ( ) ; if ( xmlPath. isEmpty ( ) )  { return  false ; } QDomDocument*  doc =  new  QDomDocument; QFile fl ( xmlPath) ; if ( ! doc-> setContent ( & fl) )  { fl. close ( ) ; return  false ; } fl. close ( ) ; showXml ( pTree,  doc,  false ) ; return  false ; 
} bool  ManageTreeXml :: showXml ( QTreeWidget*  pTree,  QDomDocument*  pDoc,  bool  bEditable) 
{ if ( pDoc ==  NULL )  { return  false ; } QDomElement docElem =  pDoc-> documentElement ( ) ; QDomNode domIn =  docElem. firstChild ( ) ; if  ( ! domIn. isNull ( ) )  { QDomElement inElem =  domIn. toElement ( ) ; QDomNodeList list =  inElem. childNodes ( ) ; for  ( int  i =  0 ;  i<  list. count ( ) ; i++ ) { QDomNode node =  list. at ( i) ; QTreeWidgetItem*  it =  new  QTreeWidgetItem ( ItemType:: DeviceEnum) ; if ( bEditable)  { it-> setFlags ( Qt:: ItemIsEditable| Qt:: ItemIsEnabled| Qt:: ItemIsSelectable) ; } if  ( ( node. toElement ( ) . tagName ( )  ==  "Device" ) &&  ( node. toElement ( ) . attributes ( ) . contains ( "name" ) ) )  { it-> setText ( 0 ,  node. toElement ( ) . attribute ( "name" ) ) ; pTree-> addTopLevelItem ( it) ; showXmlTreeNode ( pTree,  node. firstChild ( )  , it,  bEditable) ; } } } pTree-> expandAll ( ) ; return  true ; 
} bool  ManageTreeXml :: showXmlTreeNode ( QTreeWidget*  pTree,  QDomNode domParent , QTreeWidgetItem*  treeParent,  bool  bEditable) 
{ DevicePara dp; while ( ! domParent. isNull ( ) )  { QTreeWidgetItem*  it =  new  QTreeWidgetItem ( ItemType:: RegisterEnum) ; if ( bEditable)  { it-> setFlags ( Qt:: ItemIsEditable| Qt:: ItemIsEnabled| Qt:: ItemIsSelectable) ; } setTreeNodeText ( domParent , it,  & dp) ; if  ( domParent. toElement ( ) . tagName ( )  ==  "register" &&  ( domParent. toElement ( ) . attributes ( ) . contains ( "name" ) ) )  { it-> setText ( 0 ,  domParent. toElement ( ) . attribute ( "name" ) ) ; treeParent-> addChild ( it) ; }  else  { delete  it; it =  NULL ; } domParent =  domParent. nextSibling ( ) ; } treeParent-> setData ( 0 ,  Qt:: UserRole,  QVariant :: fromValue ( dp) ) ; return  true ; 
} void  ManageTreeXml :: setTreeNodeText ( QDomNode Node , QTreeWidgetItem*  treeNode,  DevicePara*  devicePara) 
{ if ( treeNode ==  NULL )  { return ; } if  ( Node. toElement ( ) . tagName ( )  ==  "mode" )  { devicePara-> mode_ =  Node. toElement ( ) . attribute ( "value" ) ; devicePara-> deviceId_ =  Node. toElement ( ) . attribute ( "deviceId" ) ; devicePara-> updateCyc_ =  Node. toElement ( ) . attribute ( "updateCyc" ) ; devicePara-> updateCycUnit_ =  Node. toElement ( ) . attribute ( "updateCycUnit" ) ; devicePara-> timeOut_ =  Node. toElement ( ) . attribute ( "timeOut" ) ; devicePara-> timeOutUnit_ =  Node. toElement ( ) . attribute ( "timeOutUnit" ) ; devicePara-> resendTimes_ =  Node. toElement ( ) . attribute ( "resendTimes" ) ; }  else  if  ( Node. toElement ( ) . tagName ( )  ==  "eth" )  { devicePara-> ipAddress_ =  Node. toElement ( ) . attribute ( "ip" ) ; devicePara-> netmask_ =  Node. toElement ( ) . attribute ( "netmask" ) ; devicePara-> port_ =  Node. toElement ( ) . attribute ( "port" ) ; }  else  if  ( Node. toElement ( ) . tagName ( )  ==  "serial" )  { devicePara-> serial_ =  Node. toElement ( ) . attribute ( "serial" ) ; devicePara-> baudRate_ =  Node. toElement ( ) . attribute ( "baudRate" ) ; devicePara-> dataBits_ =  Node. toElement ( ) . attribute ( "dataBits" ) ; devicePara-> stopBit_ =  Node. toElement ( ) . attribute ( "stopBit" ) ; devicePara-> parity_ =  Node. toElement ( ) . attribute ( "parity" ) ; }  else  if  ( Node. toElement ( ) . tagName ( )  ==  "register" )  { RegisterPara tp; tp. func_ =  Node. toElement ( ) . attribute ( "func" ) ; tp. offset_ =  Node. toElement ( ) . attribute ( "offset" ) ; tp. dataFormat_ =  Node. toElement ( ) . attribute ( "dataFormat" ) ; treeNode-> setData ( 0 ,  Qt:: UserRole +  1 ,  QVariant :: fromValue ( tp) ) ; } 
} void  ManageTreeXml :: SaveItem ( QDomElement&  root,  QTreeWidgetItem * item,  QDomDocument&  doc) 
{ QDomElement ele; int  countChild =  item-> childCount ( ) ; for  ( int  i =  0 ;  i <  countChild;  i++ )  { QTreeWidgetItem * childItem =  item-> child ( i) ; if  ( childItem-> type ( )  ==  ItemType:: DeviceEnum)  { ele =  doc. createElement ( "Device" ) ; QString itemTxt =  childItem-> text ( 0 ) ; ele. setAttribute ( "name" ,  itemTxt) ; DevicePara dp =  childItem-> data ( 0 ,  Qt:: UserRole) . value < DevicePara> ( ) ; QDomElement mode =  doc. createElement ( "mode" ) ; mode. setAttribute ( "value" , dp. mode_) ; mode. setAttribute ( "deviceId" ,  dp. deviceId_) ; mode. setAttribute ( "updateCyc" ,  dp. updateCyc_) ; mode. setAttribute ( "updateCycUnit" ,  dp. updateCycUnit_) ; mode. setAttribute ( "timeOut" ,  dp. timeOut_) ; mode. setAttribute ( "timeOutUnit" ,  dp. timeOutUnit_) ; mode. setAttribute ( "resendTimes" ,  dp. resendTimes_) ; ele. appendChild ( mode) ; QDomElement eth =  doc. createElement ( "eth" ) ; eth. setAttribute ( "ip" ,  dp. ipAddress_) ; eth. setAttribute ( "netmask" ,  dp. netmask_) ; eth. setAttribute ( "port" ,  dp. port_) ; ele. appendChild ( eth) ; QDomElement serial =  doc. createElement ( "serial" ) ; serial. setAttribute ( "serial" ,  dp. serial_) ; serial. setAttribute ( "baudRate" ,  dp. baudRate_) ; serial. setAttribute ( "dataBits" ,  dp. dataBits_) ; serial. setAttribute ( "stopBit" ,  dp. stopBit_) ; serial. setAttribute ( "parity" ,  dp. parity_) ; ele. appendChild ( serial) ; }  else  if ( childItem-> type ( )  ==  ItemType:: RegisterEnum)  { ele =  doc. createElement ( "register" ) ; RegisterPara dp =  childItem-> data ( 0 ,  Qt:: UserRole +  1 ) . value < RegisterPara> ( ) ; QString itemTxt =  childItem-> text ( 0 ) ; ele. setAttribute ( "name" ,  itemTxt) ; ele. setAttribute ( "func" ,  dp. func_) ; ele. setAttribute ( "offset" ,  dp. offset_) ; ele. setAttribute ( "dataFormat" ,  dp. dataFormat_) ; } root. appendChild ( ele) ; SaveItem ( ele, childItem, doc) ; } 
}