本文主要是介绍解析信令的类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- // XmlParser.cpp: implementation of the CXmlParser class.
- //
- //
- #include "stdafx.h"
- //#include "zxc.h"
- #include "XmlParser.h"
- #include <cassert>
- //
- // Construction/Destruction
- //
- inline BYTE MakeByte(BYTE b1, BYTE b2)
- {//高半字节放在b1,低半字节放在b2
- BYTE b = b1 << 4;
- // b <<= 4;
- b |= b2 & 0x0f;
- return b;
- }
- inline WORD MakeWord(BYTE b1, BYTE b2)
- {
- //WORD w = b1;
- //w <<= 8;
- WORD w = b1 << 8;
- w |= b2;
- return w;
- }
- inline DWORD MakeDWord(BYTE b1, BYTE b2, BYTE b3, BYTE b4)
- {
- DWORD d = b1 << 24;
- // d <<= 24;
- d |= (b2 << 16);
- d |= (b3 << 8);
- d |= b4;
- return d;
- }
- DWORD GetBits(BYTE *src, size_t pos, size_t len)
- {
- assert( len <= 32);
- DWORD t;
- BYTE *p = src + pos/8;
- t = MakeDWord(*p, *(p+1), *(p+2), *(p+3));
- // std::cout << "word:" << std::hex << t << std::endl;
- if (pos % 8 + len > 32)
- {//跨度超过4个字节
- DWORD b = -1;
- b >>= pos % 8; // 0x0..0FFFFF
- t &= b;//清除左边无效位
- t <<= (pos + len) % 8;//合适的位置,低位补0
- DWORD t1 = *(p + 4) >> (8 - (pos + len) % 8);//第5个字节的有效位
- t |= t1;
- return t;
- }
- DWORD n = sizeof( t);
- DWORD b = (n<<3) - pos%8 - len;// - 1;
- t >>= b;
- // std::cout << "right shift: " << std::hex << t << std::endl;
- DWORD x = -1;
- // std::cout << "-1/0xff...:" << std::hex << x << std::endl;
- x ^= x << len;
- // std::cout << "mask: " << std::hex << x << std::endl;
- t &= x;
- //src += (pos + len) / 8;
- pos += len;
- return t;
- }
- CXmlParser::CXmlParser()
- {
- m_pXmlDoc = 0;
- // ::CoInitialize(NULL);
- HRESULT hr = m_pXmlDoc.CreateInstance(__uuidof(DOMDocument40));
- if(FAILED(hr))
- throw L"failed to create DOM DOC";
- }
- CXmlParser::~CXmlParser()
- {
- // ::CoUninitialize();//有这句会出错,不知道为什么
- }
- BOOL CXmlParser::LoadXml(const CString& strXml)
- {//XML文件中不要加注释,保存为unicode格式,里面不要写汉语
- if (m_strXml == strXml)
- return TRUE;
- m_strXml = strXml;
- return m_pXmlDoc->load(_variant_t(strXml));
- }
- inline BOOL CXmlParser::IsGotaBurst(BYTE src[], size_t len)
- {
- if (len < 3) return FALSE;
- return (*src == 0) && (src[1] == 0) && (src[2] = 0x3c);
- }
- MSXML2::IXMLDOMElementPtr CXmlParser::GetFirstChildByID(const MSXML2::IXMLDOMElementPtr& pElement, int id) const
- {
- MSXML2::IXMLDOMElementPtr pChild = pElement->GetfirstChild(), pTemp = NULL;
- MSXML2::IXMLDOMAttributePtr pAttr = NULL;
- while (pChild != NULL)
- {
- pAttr = pChild->getAttributeNode(L"id");
- if (pAttr == NULL)//每一个子节点都必须有id属性
- {
- wcout << L"wrong. the item doesn't has id attribute." << endl;
- return NULL;
- }
- _bstr_t text = pAttr->Gettext();
- if (_wtoi((LPCTSTR)text) == id)
- {
- return pChild;
- }
- pTemp = pChild->GetnextSibling();
- swap(pChild, pTemp);
- }
- #ifdef _DEBUG
- wcout << L"can not find the corresponding child with the specified id" << endl;
- #endif
- return NULL;
- }
- MSXML2::IXMLDOMNodePtr CXmlParser::GetFirstChildByID(const MSXML2::IXMLDOMNodePtr& pFather, const CString &strID) const
- {
- MSXML2::IXMLDOMNodePtr pChild = NULL, pTemp = NULL, pAttr = NULL;
- MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
- BSTR text;
- if (pFather == NULL)
- {
- return NULL;
- }
- pChild = pFather->firstChild;
- while (pChild)
- {
- pAttrs = pChild->attributes;
- if (pAttrs == NULL)
- {
- return NULL;
- }
- pAttr = pAttrs->nextNode();
- if (pAttr == NULL)
- return NULL;
- text = pAttr->text;
- if (strID == (LPCTSTR)((_bstr_t)text))
- {
- return pChild;
- }
- pTemp = pChild->nextSibling;
- swap(pChild, pTemp);
- }
- return NULL;
- }
- bool CXmlParser::ParseMessage(const MSXML2::IXMLDOMElementPtr& pChannel)
- {
- MSXML2::IXMLDOMElementPtr pMsg, pChild, pTemp;
- MSXML2::IXMLDOMAttributePtr pAttr;
- int msgID = static_cast<int>(m_aRawData[0]);
- if (msgID <= 0)
- {
- wcout << L"wrong message id in the raw data" << endl;
- return false;
- }
- pMsg = GetFirstChildByID(pChannel, msgID);
- if (pMsg == NULL)
- {
- wcout << L"can not find the message item with the specified id" << endl;
- return false;
- }
- m_nPos += 8;//first byte if message id.
- _bstr_t msgName;
- pAttr = pMsg->getAttributeNode(L"name");
- if (pAttr != NULL)
- {
- msgName = pAttr->Gettext();
- m_strInfo += (LPCTSTR)msgName;
- m_strInfo += L"/n";
- }
- else
- {
- wcout << L"can not get message name." << endl;
- return false;
- }
- pAttr = pMsg->getAttributeNode(L"summary");
- if (pAttr != NULL)
- {
- _bstr_t text = pAttr->Gettext();
- if (wstring(L"true") == (LPCTSTR)text)
- m_strSummary = (LPCTSTR)msgName;
- }
- else
- {
- wcout << L"do not has summary attribute" << endl;
- }
- pChild = pMsg->GetfirstChild();
- while (pChild != NULL)
- {
- int nParsed = ParseXmlElement(pChild);
- if (nParsed < 0)
- {
- wcout << L"parsed error, exit from ParseMessage" << endl;
- return false;
- }
- pTemp = pChild->GetnextSibling();
- swap(pTemp, pChild);
- }
- return false;
- }
- //解析实际数据,不包括ID,len
- BOOL CXmlParser::ParseMessage(const MSXML2::IXMLDOMNodePtr& pChannel)
- {
- MSXML2::IXMLDOMNodeListPtr pChildren = NULL;
- MSXML2::IXMLDOMNodePtr pMsg = NULL, pChild = NULL, pAttr1 = NULL, pAttr2 = NULL, pAttr3 = NULL;
- MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
- try
- {
- // pMsgList = pChannel->childNodes;
- CString strID;
- strID.Format(L"%d", m_aRawData[0]);
- pMsg = GetFirstChildByID(pChannel, strID);//对于GOTA的db消息,是找到db下的合适的子消息
- if (pMsg == NULL)//没有找到符合ID的消息
- return FALSE;
- m_aRawData++;
- m_nRemaine -= 8;
- pAttrs = pMsg->attributes;
- pAttrs->nextNode();//忽略第一个属性 (ID)
- pAttr2 = pAttrs->nextNode();
- BSTR msgName = pAttr2->text;
- m_strInfo += (LPCTSTR)_bstr_t(msgName);
- m_strInfo += L"/n";
- pAttr3 = pAttrs->nextNode();
- BSTR text = pAttr3->text;
- if (wstring(L"true") == (LPCTSTR)(_bstr_t)text)
- m_strSummary = (LPCTSTR)(_bstr_t)msgName;
- pChildren = pMsg->childNodes;
- m_nPos = 0;//important!
- while ((pChild = pChildren->nextNode()) != NULL)
- {
- // BSTR s;
- // s = pChild->Gettext();
- // wcout << (TCHAR *)(_bstr_t)text << endl;
- int len = ParseXmlNode(pChild);
- //m_nRemaine -= len;
- }
- // wcout << L"m_strInfo: " << (LPCTSTR)m_strInfo << endl;
- }
- catch(const CString& str)
- {
- wcout << L"error: " << str << endl;
- }
- return TRUE;
- }
- BOOL CXmlParser::ParseDBMessage(const MSXML2::IXMLDOMNodePtr &pMsg)
- {
- MSXML2::IXMLDOMNodeListPtr pChildren = NULL;
- MSXML2::IXMLDOMNodePtr pChild = NULL, pAttr1 = NULL, pAttr2 = NULL, pAttr3 = NULL;
- MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
- pAttrs = pMsg->attributes;
- pAttrs->nextNode();//忽略第一个属性
- pAttr2 = pAttrs->nextNode();//消息名
- BSTR msgName = pAttr2->text;
- m_strInfo += (LPCTSTR)_bstr_t(msgName);// L"/n";
- m_strInfo += L"/n";
- pAttr3 = pAttrs->nextNode();
- BSTR text = pAttr3->text;
- if (wstring(L"true") == (LPCTSTR)(_bstr_t)text)
- m_strSummary = (LPCTSTR)(_bstr_t)msgName;
- pChildren = pMsg->childNodes;
- m_nPos = 0;//important!
- while ((pChild = pChildren->nextNode()) != NULL)
- {
- BSTR s;
- s = pChild->Gettext();
- // wcout << (TCHAR *)(_bstr_t)text << endl;
- m_nRemaine -= ParseXmlNode(pChild);
- }
- wcout << L"m_strInfo: " << (LPCTSTR)m_strInfo << endl;
- return FALSE;
- }
- BOOL CXmlParser::ParseData(BYTE src[], size_t len, int nChannelID)
- {
- Reset();
- MSXML2::IXMLDOMElementPtr pRoot = m_pXmlDoc->GetdocumentElement();
- MSXML2::IXMLDOMElementPtr pChannel = GetFirstChildByID(pRoot, nChannelID);
- if (pRoot == NULL)
- {
- return FALSE;
- }
- if (*src != len)//第一个字节(后面数据的)是长度
- {
- return FALSE;
- }
- m_aRawData = src + 1;
- m_nDataLength = (len - 1) * 8;
- if (ParseMessage(pChannel))
- return TRUE;
- return TRUE;
- }
- BOOL CXmlParser::ParseData(BYTE src[], size_t len, const CString& strChannelID)
- {
- Reset();//清理上次解析时的设置的变量
- MSXML2::IXMLDOMNodePtr pRoot = m_pXmlDoc->firstChild;
- MSXML2::IXMLDOMNodePtr pChannel = GetFirstChildByID(pRoot, strChannelID);
- BSTR text = pChannel->baseName;
- // wcout << (TCHAR *)(_bstr_t)text << endl;
- if (*src != len)// that's right, the len is the len of the raw data(contains it self)包括自己在内的字节的个数
- {
- return FALSE;
- }
- m_aRawData = src + 1;//length
- // m_nPos += 8;
- /*size_t */m_nRemaine = len - 1;
- m_nDataLength = m_nRemaine * 8;
- m_nRemaine = m_nDataLength;
- ParseMessage(pChannel);
- return TRUE;
- }
- int CXmlParser::ParseXmlNode(const MSXML2::IXMLDOMNodePtr &pNode)
- {
- int nParsed = 0;
- MSXML2::IXMLDOMNodeListPtr pChildren;
- MSXML2::IXMLDOMNodePtr pChild, pAtt1, pAtt2, pAtt3, pAtt4;
- MSXML2::IXMLDOMNamedNodeMapPtr pAttrs;
- BSTR text;
- int d;
- pNode->get_baseName(&text);//得到节点名称
- if (wstring(L"_") == (wchar_t*)((_bstr_t)text))//如果是控制节点
- {
- pNode->get_attributes(&pAttrs);//属性
- pAtt1= pAttrs->nextNode();//第一个属性节点
- pAtt1->get_text(&text);
- long l = _wtoi((_bstr_t)text);//, pos;//所依赖的节点的位置
- size_t xc = m_vdwData.size();
- if (xc + l >= xc)
- {
- wcerr << L"error" << endl;
- return 0;
- }
- d = m_vdwData[m_vdwData.size() + l];
- pAtt2=pAttrs->nextNode();
- pAtt2->get_text(&text);//第二个属性的值
- pNode->get_childNodes(&pChildren);
- if (wstring(L"is") == (wchar_t *)((_bstr_t)text))
- {
- pAtt3 = pAttrs->nextNode();
- pAtt3->get_text(&text);//第三个属性的值
- if (d == _wtoi((_bstr_t)text))
- {//有子项
- pNode->get_childNodes(&pChildren);
- while ((pChild = pChildren->nextNode()) != NULL)
- {//处理每个子项
- nParsed += ParseXmlNode(pChild);
- }
- }
- else
- {
- //没有子项
- }
- }
- else if (wstring(L"has") == (wchar_t *)((_bstr_t)text))
- {
- if (d & _wtoi((_bstr_t)text))//wrong!!!!
- {//有子项
- pNode->get_childNodes(&pChildren);
- while ((pChild = pChildren->nextNode()) != NULL)
- {
- nParsed += ParseXmlNode(pChild);
- }
- }
- else
- {//没有子项
- }
- }
- else if (wstring(L"len") == (wchar_t *)((_bstr_t)text))
- {
- if (nParsed >= d)
- return nParsed;
- long nChildren;
- pChildren->get_length(&nChildren);
- if (nChildren == 0)
- {//没有子项,则直接输出剩余的数据,并返回
- wostringstream wos;
- //得到长度单位
- pAtt3 = pAttrs->nextNode();
- pAtt3->get_text(&text);
- // wcout << (LPCTSTR)(_bstr_t)text << endl;
- int unit = _wtoi((_bstr_t)text);
- if (unit < 0)
- d = m_nRemaine;
- else
- d *= unit;
- //长度单位
- pAtt4 = pAttrs->nextNode();
- wos << (LPCTSTR)(_bstr_t)pAtt4->text << L" = ";
- unsigned long ul;
- for (int i = 0; i < d/8; i++)
- {
- ul = GetBits(m_aRawData, m_nPos, 8);
- wos << hex << L"0x" << ul << L" ";
- m_nPos += 8;
- }
- if (d % 8)
- {
- ul = GetBits(m_aRawData, m_nPos, d % 8);
- wos << hex << L"0x" << ul << endl;
- }
- /*for (int i = 0; i < d; i++)
- {
- ul = GetBits(m_aRawData, m_nPos, 8);
- wos << hex << L"0x" << ul << L" ";
- m_nPos += 8;
- }*/
- m_strInfo += wos.str().c_str();
- m_strInfo += L"/n";
- nParsed += d;
- m_nRemaine -= d;
- return nParsed;
- }
- pNode->get_childNodes(&pChildren);
- while ((pChild = pChildren->nextNode()) != NULL)
- {
- nParsed += ParseXmlNode(pChild);
- }
- }
- else if (wstring(L"recur") == (wchar_t *)((_bstr_t)text))
- {
- pAtt3 = pAttrs->nextNode();
- pAtt3->get_text(&text);
- // wcout << (LPCTSTR)(_bstr_t)text << endl;
- int unit = _wtoi((_bstr_t)text);
- if (unit < 0)
- d = m_nRemaine;
- else
- d *= unit;
- MSXML2::IXMLDOMNodePtr pTemp, pFirst;
- pFirst = pChildren->nextNode();
- while (d > nParsed)
- {
- // wcout << (LPCTSTR)(_bstr_t)pNode->baseName << endl;
- pChild = pFirst;
- while (pChild != NULL)
- {
- // wcout << (LPCTSTR)(_bstr_t)pChild->baseName << endl;
- nParsed += ParseXmlNode(pChild);
- pTemp = pChild->nextSibling;
- swap(pTemp, pChild);
- }
- }
- }
- //if (m_vdwData.size() > 1)
- // m_vdwData.pop_back();
- return nParsed;
- }
- pNode->get_attributes(&pAttrs);
- pAtt1 = pAttrs->nextNode();
- pAtt1->get_baseName(&text);
- pAtt1->get_text(&text);
- int n = _wtoi((TCHAR *)(_bstr_t)text);
- if (n + m_nPos > m_nDataLength)
- {//如果长度不匹配,则出错
- wcerr << L"error data length" << endl;
- return -1;
- }
- DWORD x = GetBits(m_aRawData, m_nPos, n);
- m_nPos += n;
- nParsed += n;
- m_nRemaine -= n;
- pAtt2 = pAttrs->nextNode();
- pAtt2->get_text(&text);//name
- wostringstream ostr;
- ostr << (TCHAR *)((_bstr_t)text) << L" = " << /*m_vdwData[m_vdwData.size() -1]*/ x << endl;//name = dword
- m_strInfo += ostr.str().c_str();
- // wcout << (LPCTSTR)m_strInfo << endl;
- pAtt3 = pAttrs->nextNode();
- if (pAtt3 == NULL)//第3个属性不存在
- return nParsed;
- pAtt3->get_text(&text);//第三个属性为"save"
- if (wstring(L"true") == (LPCTSTR)((_bstr_t)text))
- m_vdwData.push_back(x);
- return nParsed;
- }
- int CXmlParser::ParseXmlElement(const MSXML2::IXMLDOMElementPtr& pElement)
- {
- int nParsed = 0;
- MSXML2::IXMLDOMNodeListPtr pChildren;
- MSXML2::IXMLDOMElementPtr pChild;
- MSXML2::IXMLDOMNamedNodeMapPtr pAttrs;
- MSXML2::IXMLDOMAttributePtr /*pAttr, */pAttr1, pAttr2, pAttr3, pAttr4;
- _bstr_t text = pElement->baseName;
- if (wstring(L"_") == (wchar_t *)text)
- {
- pAttr1 = pElement->getAttributeNode(L"de");
- text = pAttr1->Getvalue();
- int nBack = _wtoi((LPCTSTR)text);
- int nvlen = static_cast<int>(m_vdwData.size());//避免有符号正数与无符号负数相加时出错
- if (nvlen + nBack >= nvlen)
- {
- #if _DEBUG
- wcerr << L"error on _ element's /"de/" attribute" << endl;
- #endif
- return -1;
- }
- int nDepend = m_vdwData[nvlen + nBack];
- pAttr2 = pElement->getAttributeNode(L"be");
- text = pAttr2->Getvalue();
- if (wstring(L"is") == (wchar_t *)text)
- {
- pAttr3 = pElement->getAttributeNode(L"dval");
- text = pAttr3->Gettext();
- if (nDepend == _wtoi((LPCTSTR)text))
- {
- pChildren = pElement->GetchildNodes();
- while ((pChild = pChildren->nextNode()) != NULL)
- {
- int ix = ParseXmlElement(static_cast<MSXML2::IXMLDOMElementPtr>(pChild));
- if (ix < 0)
- {
- wcout << L"error when parse, exit from ParseXmlElement().." << endl;
- return -1;
- }
- nParsed += ix;
- }
- }
- }
- else if (wstring(L"has") == (wchar_t *)text)
- {//此块要重写!
- //if (nDepend )
- }
- else if (wstring(L"len") == (wchar_t *)text)
- {
- pChildren = pElement->GetchildNodes();
- if (pChildren->Getlength() == 0)
- {//没有子项,不保存,不会发生需要弹栈现象
- wostringstream wos;
- pAttr3 = pElement->getAttributeNode(L"dval");//长度单位
- text = pAttr3->Gettext();
- int unit = _wtoi((LPCTSTR)text);
- if (unit > 0)
- {
- nDepend *= unit;
- }
- else if (unit < 0)
- {
- nDepend = m_nDataLength - m_nPos;//剩余数据
- if (nDepend <= 0)
- {
- wcout << L"no data exit from ParseXmlElement...len" << endl;
- return -1;
- }
- }
- else
- {
- #ifdef _DEBUG
- wcout << L"dval should not be negative." << endl;
- #endif
- return -1;
- }
- pAttr4 = pElement->getAttributeNode(L"name");
- if (pAttr4 == NULL)
- {//没有子项,则一定有name属性
- #ifdef _DEBUG
- wcout << L"name attribute not exist." << endl;
- #endif
- return -1;
- }
- text = pAttr4->Gettext();
- wos << (LPCTSTR)text << L" = ";
- for (int i = 0; i < nDepend / 8; i++)
- {
- int r = GetBits(m_aRawData, m_nPos, 8);
- wos << hex << L"0x" << r << L" ";
- m_nPos += 8;
- }
- if (nDepend % 8)
- {
- int r = GetBits(m_aRawData, m_nPos, nDepend % 8);
- wos << hex << L"0x" << r << endl;
- m_nPos += nDepend % 8;
- }
- m_strInfo += wos.str().c_str();
- m_strInfo += L"/n";
- nParsed += nDepend;
- if (m_nPos > m_nDataLength)
- {
- wcout << L"fatal error...exit from ParseXmlElement...len." << endl;
- return -1;
- }
- }
- else
- {
- pAttr3 = pElement->getAttributeNode(L"dval");//长度单位
- text = pAttr3->Gettext();
- int unit = _wtoi((LPCTSTR)text);
- if (unit > 0)
- {
- nDepend *= unit;
- }
- else if (unit < 0)
- {
- nDepend = m_nDataLength - m_nPos;//剩余数据
- }
- else
- {
- #ifdef _DEBUG
- wcout << L"dval should not be negative." << endl;
- #endif
- return -1;
- }
- while ((pChild = pChildren->nextNode()) != NULL && nParsed < nDepend)
- {
- int ix = ParseXmlElement(static_cast<MSXML2::IXMLDOMElementPtr>(pChild));
- if (ix < 0)
- {
- wcout << L"error when parse, exit from ParseXmlElement()...len" << endl;
- return -1;
- }
- nParsed += ix;
- }
- }
- }
- else if (wstring(L"recur") == (wchar_t *)text)
- {
- pAttr3 = pElement->getAttributeNode(L"dval");
- if (pAttr3 == NULL)
- {
- return -1;
- }
- text = pAttr3->Gettext();
- int unit = _wtoi((LPCTSTR)text);
- if (unit > 0)
- {
- nDepend *= unit;
- }
- else if (unit < 0)
- {
- nDepend = m_nDataLength - m_nPos;//剩余数据
- }
- else
- {
- #ifdef _DEBUG
- wcout << L"dval should not be negative except -1." << endl;
- #endif
- return -1;
- }
- MSXML2::IXMLDOMElementPtr pTemp, pFirst;
- pChildren = pElement->GetchildNodes();
- if (pChildren == NULL || pChildren->Getlength() == 0)
- {
- #ifdef _DEBUG
- wcout << L"recur not have children." << endl;
- #endif
- return -1;
- }
- //pFirst = pChildren->nextNode();
- while (nDepend > nParsed)
- {
- //pChild = pFirst;
- pChild = pElement->GetfirstChild();
- while(pChild != NULL)
- {
- int ix = ParseXmlElement(static_cast<MSXML2::IXMLDOMElementPtr>(pChild));
- if (ix < 0)
- {
- wcout << L"error when parse, exit from ParseXmlElement()..--recur " << endl;
- return -1;
- }
- if (m_nPos > m_nDataLength)
- {
- wcout << L"fatal error....exit from ParseXmlElement()..--recur" << endl;
- return -1;
- }
- nParsed += ix;
- pTemp = pChild->GetnextSibling();
- swap(pTemp, pChild);
- }
- }
- }
- else
- {
- #ifdef _DEBUG
- wcerr << L"error on _ element's second attribute" << endl;
- #endif
- return -1;
- }
- //对于len 元素,应是pAttr5
- if ((pAttr4 = pElement->getAttributeNode(L"del")) != NULL)
- {//弹栈
- m_vdwData.pop_back();
- }
- #ifdef _DEBUG
- else
- {//不是错误
- wcout << L"del attri not exist." << endl;
- }
- #endif
- return nParsed;
- }
- pAttr1 = pElement->getAttributeNode(L"length");
- pAttr2 = pElement->getAttributeNode(L"name");
- pAttr3 = pElement->getAttributeNode(L"save");
- if (pAttr1 == NULL)
- {
- #ifdef _DEBUG
- wcerr << L"error xml no length attribute" << endl;
- #endif
- return -1;
- }
- if (pAttr2 == NULL)
- {
- #ifdef _DEBUG
- wcerr << L"error xml element no name attribute" << endl;
- #endif
- return -1;
- }
- text = pAttr1->Getvalue();
- int nLength = _wtoi((wchar_t *)text);
- if (nLength + m_nPos > m_nDataLength)
- {
- #ifdef _DEBUG
- wcerr << L"error data length..." << endl;
- #endif
- return -1;
- }
- wostringstream ostrData, ostr;
- DWORD data;
- if (nLength > 32)
- {
- for (int i = 0; i < nLength; )
- {
- data = GetBits(m_aRawData, m_nPos, 8);
- ostrData << hex << L"0x" << data << L" ";// << endl;
- m_nPos += 8;
- i += 8;
- }
- if (nLength % 8 != 0)
- {
- data = GetBits(m_aRawData, m_nPos, nLength % 8);
- ostrData << hex << L"0x" << data << endl;
- m_nPos += nLength % 8;
- }
- }
- else
- {
- data = GetBits(m_aRawData, m_nPos, nLength);
- ostrData << dec << data << L"( 0x" << hex << data << L" )";// << endl;
- m_nPos += nLength;
- }
- text = pAttr2->Getvalue();
- ostr << (LPCTSTR)text << L" = " << ostrData.str() << endl;
- m_strInfo += ostr.str().c_str();
- #ifdef _DEBUG
- // wcout << (LPCTSTR)m_strInfo;
- #endif
- if (pAttr3 != NULL && wstring(L"true") == (LPCTSTR)(_bstr_t)pAttr3->Getvalue())
- {
- m_vdwData.push_back(data);
- }
- nParsed = nLength;
- return nParsed;
- }
- bool CXmlParser::ParseDataBurst(BYTE src[], size_t len, int nChannelID, int nMsgID)
- {//ID, len, data...其中len不包括自身
- if (!IsGotaBurst(src, len))
- {
- wcout << L"the data burst does not contain the gota msg." << endl;
- return false;
- }
- Reset();
- MSXML2::IXMLDOMElementPtr pRoot = m_pXmlDoc->GetdocumentElement();
- if (pRoot == NULL)
- {
- wcout << L"can not find the root element of the xml doc" << endl;
- return false;
- }
- MSXML2::IXMLDOMElementPtr pChannel = GetFirstChildByID(pRoot, nChannelID);
- if (pChannel == NULL)
- {
- wcout << L"can not get the channel with the specified channel id" << endl;
- return false;
- }
- MSXML2::IXMLDOMElementPtr pMsg = GetFirstChildByID(pChannel, nMsgID);
- if (pMsg == NULL)
- {
- wcout << L"can not get message node with the specified msg id" << endl;
- return false;
- }
- m_aRawData = src + 3;
- m_nDataLength = (len - 3) * 8;
- return ParseMessage(pMsg);
- }
- BOOL CXmlParser::ParseDataBurst(BYTE src[], size_t len, const CString &strChannelID, const CString &strMsgID)
- {
- return ParseDataBurst(src, len, _wtoi((LPCTSTR)strChannelID), _wtoi((LPCTSTR)strMsgID));
- if (!IsGotaBurst(src, len))
- {
- // AfxMessageBox(L"worng");
- return FALSE;
- }
- //for (size_t i = 0; i < len; i++)
- // wcout << src[i] << L" ";
- //wcout << endl;
- Reset();
- MSXML2::IXMLDOMNodePtr pRoot = m_pXmlDoc->firstChild;
- MSXML2::IXMLDOMNodePtr pChannel = GetFirstChildByID(pRoot, strChannelID);
- MSXML2::IXMLDOMNodePtr pMsg = GetFirstChildByID(pChannel, strMsgID);
- if (pMsg == NULL)
- return FALSE;
- // BSTR text = pChannel->baseName;
- m_aRawData = src + 3;// 00 00 3c
- m_nRemaine = len - 3;
- m_nDataLength = m_nRemaine * 8;
- m_nRemaine = m_nDataLength;
- ParseMessage(pMsg);
- return TRUE;
- }
- BOOL CXmlParser::Reset()
- {
- m_nPos = 0;//important.
- m_strInfo = L"";
- m_strSummary = L"";
- m_vdwData.clear();
- return TRUE;
- }
- BOOL CXmlParser::ParseGotaData(BYTE src[], size_t len, int nChannelID, int nMsgID)
- {
- if (IsFcsch(nChannelID))
- {
- if (GetFcsch_DB(s_nFcsch) == nMsgID)
- return ParseDataBurst(src, len, s_nFcsch, nMsgID);
- return ParseData(src, len, s_nFcsch);
- }
- if (IsRcsch(nChannelID))
- {
- if (GetRcsch_DB(s_nRcsch) == nMsgID)
- return ParseDataBurst(src, len, s_nRcsch, nMsgID);
- return ParseData(src, len, s_nRcsch);
- }
- if (IsFdsch(nChannelID))
- {
- if (GetFdsch_DB(s_nFdsch) == nMsgID)
- return ParseDataBurst(src, len, s_nFdsch, nMsgID);
- return ParseData(src, len, s_nFdsch);
- }
- if (IsRdsch(nChannelID))
- {
- if (GetRdsch_DB(s_nFdsch) == nMsgID)
- return ParseDataBurst(src, len, s_nFdsch, nMsgID);
- return ParseData(src, len, s_nFdsch);
- }
- return FALSE;
- }
- BOOL CXmlParser::ParseGotaData(BYTE src[], size_t len, const CString &strChannelID, const CString &strMsgID)
- {
- int nChannelID = _wtoi((LPCTSTR)strChannelID);
- int nMsgID = _wtoi((LPCTSTR)strMsgID);
- return ParseGotaData(src, len, nChannelID, nMsgID);
- }
这篇关于解析信令的类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!