Node * ReadFile ( const char * fileName ) { IStream * pStream; IXmlReader * pReader; HANDLE fileHandle = CreateFile(fileName, FILE_GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); if (fileHandle == INVALID_HANDLE_VALUE) { Fatal("Cannot open XML file %s", fileName); } if (FAILED(CreateStreamOnHandle(fileHandle, &pStream))) { Fatal("Cannot create stream from file"); } if (FAILED(CreateXmlReader(__uuidof(IXmlReader), (void**) &pReader, nullptr))) { Fatal("Cannot create XML reader"); } if (FAILED(pReader->SetProperty(XmlReaderProperty_DtdProcessing, DtdProcessing_Prohibit))) { Fatal("Cannot prohibit DTD processing"); } if (FAILED(pReader->SetInput(pStream))) { Fatal("Cannot set XML reader input"); } Node * topNode; if (FAILED(ParseXml(pReader, &topNode))) { unsigned int line, linePos; pReader->GetLineNumber(&line); pReader->GetLinePosition(&linePos); fprintf( stderr, "Error on line %d, position %d in \"%s\".\n", line, linePos, fileName); Fatal("Error parsing XML"); } SAFERELEASE(pReader); SAFERELEASE(pStream); CloseHandle(fileHandle); return topNode; }
/* Modify this to avoid temp file I/O */ bool CXMLParser::extractData( wstring filename) { bool bRet = false; HRESULT hr = S_OK; IXmlReader *pReader = nullptr; IStream *pStream = nullptr; XmlNodeType nodeType; const TCHAR* pwszName; const TCHAR* pwszValue; if (FAILED(hr = SHCreateStreamOnFile(filename.c_str(), STGM_READ, &pStream))) { return bRet; } if (!GetDataContainer()) { if (FAILED(hr = CreateXmlReader(__uuidof(IXmlReader), (void**)&pReader, nullptr))) { return bRet; } if (FAILED(hr = pReader->SetInput(pStream))) { return bRet; } bool inKey = false; wstring csKeyFoundStr; while (!pReader->IsEOF()) { pReader->Read(&nodeType); switch (nodeType) { case XmlNodeType_Element: if (S_OK == pReader->GetLocalName(&pwszName, nullptr)) { if (true == isNodeInKeys(pwszName)) { inKey = true; csKeyFoundStr = pwszName; } } break; case XmlNodeType_CDATA: case XmlNodeType_Text: { if (true == inKey) { if (S_OK == pReader->GetValue(&pwszValue, nullptr)) { GetDataContainer()->setkeyValue(csKeyFoundStr, pwszValue); inKey = false; bRet = true; } } } break; default: break; } } } return bRet; }