📄 xmlelement.cpp
字号:
// XmlElement.cpp: implementation of the CXmlElement class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "XmlElement.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CXmlElement::~CXmlElement()
{
}
void CXmlElement::get()
{
if (pos!=-1)
{
elem=0;
CComPtr<IXMLDOMNode> inode;
nlist->get_item(pos,&inode);
if (inode==0) return;
DOMNodeType type;
inode->get_nodeType(&type);
if (type!=NODE_ELEMENT) return;
CComQIPtr<IXMLDOMElement> e(inode);
elem=e;
}
clen=0;
if (elem!=0)
{
CComPtr<IXMLDOMNodeList> iNodeList;
elem->get_childNodes(&iNodeList);
iNodeList->get_length(&clen);
}
}
CString CXmlElement::name() const
{
if (!elem) return L"";
CComBSTR bn;
elem->get_tagName(&bn);
return CString(bn);
}
CString CXmlElement::attr(const CString name) const
{
if (!elem) return L"";
CComBSTR bname(name);
CComVariant val(VT_EMPTY);
elem->getAttribute(bname,&val);
if (val.vt==VT_BSTR) return val.bstrVal;
return L"";
}
bool CXmlElement::attrBool(const CString name,bool def) const
{
CString a = attr(name);
if (a==L"true" || a==L"TRUE") return true;
else if (a==L"false" || a==L"FALSE") return false;
else return def;
}
int CXmlElement::attrInt(const CString name, int def) const
{
CString a = attr(name);
int i, res=swscanf(a,L"%i",&i);
if (res==1) return i; else return def;
}
CString CXmlElement::val() const
{
if (!elem) return L"";
CComVariant val(VT_EMPTY);
elem->get_nodeTypedValue(&val);
//AfxMessageBox(val.bstrVal);
if (val.vt==VT_BSTR) return val.bstrVal;
return L"";
}
CXmlElement CXmlElement::subnode(const CString name) const
{
if (!elem) return CXmlElement();
for (CXmlElement c=begin(); c!=end(); c++)
{
if (c.name()==name) return c;
}
return CXmlElement();
}
CXmlElement CXmlElement::subnode(const CString name,const CString type,const CString values) const
{
if (!elem) return CXmlElement();
//if (!elem) return NULL;
for (CXmlElement c=begin(); c!=end(); c++)
{
if (c.name()==name && c.attr(type)==values) return c;
}
//return NULL;
return CXmlElement();
}
/*
CXmlElement CXmlElement::subnodeName() const
{
// *elementNameList=new CString() ;
if (!elem) return CXmlElement();
int count =0;
for (CXmlElement c=begin(); c!=end(); c++)
{
// elementNameList.Add(&c.name());
// elementNameList[count] = c.name();
count ++;
}
return CXmlElement();
}
*/
CString CXmlElement::subval(const CString name) const
{
if (!elem) return L"";
CXmlElement c=subnode(name);
return c.val();
}
CString CXmlElement::subval(const CString name,const CString type,const CString values) const
{
if (!elem) return L"";
CXmlElement c=subnode(name,type,values);
return c.val();
}
CXmlElement CXmlElement::begin() const
{
if (!elem) return CXmlElement();
CComPtr<IXMLDOMNodeList> iNodeList;
elem->get_childNodes(&iNodeList);
return CXmlElement(iNodeList);
}
CXmlElement CXmlElement::end() const
{
return CXmlElement(clen);
}
CXmlElement CXmlElement::operator++(int)
{
if (pos!=-1) {pos++; get();}
return *this;
}
bool CXmlElement::operator!=(const CXmlElement &e) const
{
return pos!=e.clen;
}
/*
CXmlElement::CXmlElement(const CXmlElement &XmlElement)
{
} */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -