📄 easexml.cpp
字号:
/*************************************************************************** easexml.cpp - description ------------------- begin : ? 2? 6 2005 copyright : (C) 2005 by LiuZhong email : easeliu@vip.sina.com ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#include "easexml.h"EaseXml::EaseXml(char* XmlStr){ SetXml(XmlStr);}EaseXml::EaseXml(string XmlStr){ SetXml(XmlStr);}EaseXml::~EaseXml(){}void EaseXml::SetXml(char* XmlStr){ if(strlen(XmlStr)>0){ m_Xml = XmlStr; m_Doc.Clear(); m_Doc.SetCondenseWhiteSpace(false); m_Doc.SetTabSize(1); m_Doc.Parse(XmlStr,0,TIXML_ENCODING_LEGACY); }}void EaseXml::SetXml(string XmlStr){ if(XmlStr.size()>0){ m_Xml = XmlStr; m_Doc.Clear(); m_Doc.SetCondenseWhiteSpace(false); m_Doc.SetTabSize(1); m_Doc.Parse(m_Xml.c_str(),0,TIXML_ENCODING_LEGACY); }}string EaseXml::Xml(){ return m_Xml;}TiXmlHandle EaseXml::Handle(){ return TiXmlHandle(&m_Doc);}string EaseXml::GetRootEleName(){ string RootName=""; TiXmlElement* Root = m_Doc.RootElement(); if(Root){ RootName = Root->Value(); } return RootName;}string EaseXml::GetEleValue(string EleNames){ string EleValue =""; TiXmlText* Text = NavigateNode(EleNames).FirstChild().Text(); if(Text){ EleValue = Text->Value(); } return EleValue;}string EaseXml::GetEleValue(string EleNames,string ChildName,int Index){ string EleValue=""; TiXmlText* Text = NavigateNode(EleNames).ChildElement(ChildName,Index).FirstChild().Text(); if(Text){ EleValue = Text->Value(); } return EleValue;}string EaseXml::GetEleAttrValue(string EleNames,string AttrName){ string AttrValue = ""; TiXmlElement* Ele = NavigateNode(EleNames).Element(); if(Ele){ const char* Value = Ele->Attribute(AttrName.c_str()); if(Value) AttrValue = Value; } return AttrValue;}string EaseXml::GetEleAttrValue(string EleNames,string ChildName,string AttrName,int Index){ string AttrValue = ""; TiXmlElement* Ele = NavigateNode(EleNames).ChildElement(ChildName,Index).Element(); if(Ele){ const char* Value = Ele->Attribute(AttrName.c_str()); if(Value) AttrValue = Value; } return AttrValue;}StringList EaseXml::GetEleValues(string EleNames,string ChildName){ StringList Values; int Index=0; TiXmlHandle Handle = NavigateNode(EleNames); do{ TiXmlText* Text = Handle.ChildElement(ChildName,Index).FirstChild().Text(); if(Text){ Values.push_back(Text->Value()); ++Index; } else break; }while(1); return Values; }StringPair EaseXml::GetChildEleNameValue(string EleNames,int Index){ StringPair NameValue; TiXmlElement* Element = NavigateNode(EleNames).ChildElement(Index).Element(); if(Element){ TiXmlText* Text = Element->FirstChild()->ToText(); NameValue = StringPair(Element->Value(),(Text?Text->Value():"")); } return NameValue;}StringPairList EaseXml::GetChildEleNameValues(string EleNames){ StringPairList NameValues; int Index = 0; TiXmlHandle Handle = NavigateNode(EleNames); do{ TiXmlElement* Element= Handle.ChildElement(Index).Element(); if(Element){ TiXmlText* Text = Element->FirstChild()->ToText(); NameValues.push_back(StringPair(Element->Value(),(Text?Text->Value():""))); ++Index; } else break; }while(1); return NameValues;}TiXmlHandle EaseXml::NavigateNode(string EleNames){ char EleBuf[EleNames.size()+1]; memset(EleBuf,0,EleNames.size()+1); strcpy(EleBuf,EleNames.c_str()); char *p=EleBuf,*p2=EleBuf; TiXmlHandle Node(&m_Doc); while(p2){ p2 = strchr(p,'/'); if(p2){ *p2 = 0; Node = Node.FirstChildElement(p); p = ++p2; } } Node = Node.FirstChildElement(p); return Node; }int EaseXml::GetEleOffset(int Row,int Column){ int Offset = Column-1; char* p = (char*)m_Xml.c_str(); while(--Row>0 && p){ p = strchr(p,'\n'); if(p) ++p; } if(p && p-m_Xml.c_str()<m_Xml.size()){ Offset += (p-m_Xml.c_str()); } return Offset;}string EaseXml::GetEleXml(string EleNames,int Index){ string EleXml =""; TiXmlElement* Element = NavigateNode(EleNames).Element(); if(Index>0){ while(Index-- && Element) Element = Element->NextSiblingElement(Element->Value()); } if(Element){ int eoff1 = GetEleOffset(Element->Row(),Element->Column()); TiXmlElement* NextEle = Element->NextSiblingElement(); if(NextEle){ int eoff2 = GetEleOffset(NextEle->Row(),NextEle->Column()); EleXml = m_Xml.substr(eoff1,eoff2-eoff1); } else{ TiXmlElement* Parent = (Element->Parent()?Element->Parent()->ToElement():NULL); while(Parent && !Parent->NextSiblingElement()){ Parent = (Parent->Parent()?Parent->Parent()->ToElement():NULL); } if(Parent){ Parent = Parent->NextSiblingElement(); int eoff3=GetEleOffset(Parent->Row(),Parent->Column()); EleXml = m_Xml.substr(eoff1,eoff3-eoff1); } else EleXml = m_Xml.substr(eoff1); string lName = "</"; lName += Element->Value(); lName += ">"; int pos = EleXml.rfind(lName); if(pos){ EleXml = EleXml.substr(0,pos+lName.size()); } else{ EleXml = ""; } } } return EleXml;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -