<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<HELLO>Привет, народ!</HELLO>
</ROOT>
Пример чтения данных из XML-файла
Хотелось бы увидеть небольшой пример на Visual C++ 6 с использованием данных из XML-файла (желательно msxml).
К примеру: имеется некий файл Hello.xml
Код:
Как мне в нём получить строку из тега HELLO? Документации - горы, но реального примерчика - как не было так и нет(((
П.С. Любители гугла, знатоки мест нахождения документов и просто тролли - лесом :)
Код:
#include <iostream.h>
#include <msxml.h>
#include <conio.h>
void main()
{
// инициализация COM
CoInitialize(NULL);
// создаём XML-парсер
CComPtr<IXMLDOMDocument> spXMLDOM;
HRESULT hr = spXMLDOM.CoCreateInstance(__uuidof(DOMDocument));
if ( FAILED(hr) )
throw "Unable to create XML parser object";
if ( spXMLDOM.p == NULL )
throw "Unable to create XML parser object";
// загружаем XML-документ
VARIANT_BOOL bSuccess = false;
hr = spXMLDOM->load(CComVariant(L"Hello.xml"), &bSuccess);
if ( FAILED(hr) )
throw "Unable to load XML document into the parser";
if ( !bSuccess )
throw "Unable to load XML document into the parser";
// ищем XML-ноду
CComBSTR bstrSS(L"ROOT/HELLO");
CComPtr<IXMLDOMNode> spXMLNode;
hr = spXMLDOM->selectSingleNode(bstrSS,&spXMLNode);
if ( FAILED(hr) )
throw "Unable to locate 'HELLO' XML node";
if ( spXMLNode.p == NULL )
throw "Unable to locate 'HELLO' XML node";
CComVariant varValue(VT_EMPTY); // сюда будем читать
hr = spXMLNode->get_nodeTypedValue(&varValue); // читаем
if ( FAILED(hr) ) // нет ошибки?
throw "Unable to retrieve 'HELLO' text";
if ( varValue.vt == VT_BSTR ) // там строка?
{
// конвертируем полученную строку из BSTR в ANSI
USES_CONVERSION;
LPTSTR lpstrMsg = W2T(varValue.bstrVal);
std::cout << lpstrMsg << std::endl;
}
else
{
// произошла ошибка
throw "Unable to retrieve 'HELLO' text";
}
}
#include <msxml.h>
#include <conio.h>
void main()
{
// инициализация COM
CoInitialize(NULL);
// создаём XML-парсер
CComPtr<IXMLDOMDocument> spXMLDOM;
HRESULT hr = spXMLDOM.CoCreateInstance(__uuidof(DOMDocument));
if ( FAILED(hr) )
throw "Unable to create XML parser object";
if ( spXMLDOM.p == NULL )
throw "Unable to create XML parser object";
// загружаем XML-документ
VARIANT_BOOL bSuccess = false;
hr = spXMLDOM->load(CComVariant(L"Hello.xml"), &bSuccess);
if ( FAILED(hr) )
throw "Unable to load XML document into the parser";
if ( !bSuccess )
throw "Unable to load XML document into the parser";
// ищем XML-ноду
CComBSTR bstrSS(L"ROOT/HELLO");
CComPtr<IXMLDOMNode> spXMLNode;
hr = spXMLDOM->selectSingleNode(bstrSS,&spXMLNode);
if ( FAILED(hr) )
throw "Unable to locate 'HELLO' XML node";
if ( spXMLNode.p == NULL )
throw "Unable to locate 'HELLO' XML node";
CComVariant varValue(VT_EMPTY); // сюда будем читать
hr = spXMLNode->get_nodeTypedValue(&varValue); // читаем
if ( FAILED(hr) ) // нет ошибки?
throw "Unable to retrieve 'HELLO' text";
if ( varValue.vt == VT_BSTR ) // там строка?
{
// конвертируем полученную строку из BSTR в ANSI
USES_CONVERSION;
LPTSTR lpstrMsg = W2T(varValue.bstrVal);
std::cout << lpstrMsg << std::endl;
}
else
{
// произошла ошибка
throw "Unable to retrieve 'HELLO' text";
}
}
Код:
--------------------Configuration: forschung1 - Win32 Debug--------------------
Compiling...
forschung1.cpp
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(11) : error C2065: 'CComPtr' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(11) : error C2065: 'IXMLDOMDocument' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(11) : error C2065: 'spXMLDOM' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(11) : warning C4804: '>' : unsafe use of type 'bool' in operation
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(11) : warning C4552: '>' : operator has no effect; expected operator with side-effect
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(12) : error C2228: left of '.CoCreateInstance' must have class/struct/union type
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(12) : error C2065: 'DOMDocument' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(16) : error C2228: left of '.p' must have class/struct/union type
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(21) : error C2227: left of '->load' must point to class/struct/union
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(21) : error C2065: 'CComVariant' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(29) : error C2065: 'CComBSTR' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(29) : error C2146: syntax error : missing ';' before identifier 'bstrSS'
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(29) : error C2065: 'bstrSS' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(30) : error C2065: 'IXMLDOMNode' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(30) : error C2065: 'spXMLNode' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(30) : warning C4804: '>' : unsafe use of type 'bool' in operation
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(30) : warning C4552: '>' : operator has no effect; expected operator with side-effect
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(31) : error C2227: left of '->selectSingleNode' must point to class/struct/union
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(35) : error C2228: left of '.p' must have class/struct/union type
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(38) : error C2146: syntax error : missing ';' before identifier 'varValue'
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(38) : error C2065: 'varValue' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(39) : error C2227: left of '->get_nodeTypedValue' must point to class/struct/union
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(43) : error C2228: left of '.vt' must have class/struct/union type
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(46) : error C2065: 'USES_CONVERSION' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(47) : error C2065: 'W2T' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(47) : error C2228: left of '.bstrVal' must have class/struct/union type
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(47) : error C2440: 'initializing' : cannot convert from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(48) : error C2653: 'std' : is not a class or namespace name
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(48) : error C2653: 'std' : is not a class or namespace name
Error executing cl.exe.
forschung1.exe - 25 error(s), 4 warning(s)
Compiling...
forschung1.cpp
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(11) : error C2065: 'CComPtr' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(11) : error C2065: 'IXMLDOMDocument' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(11) : error C2065: 'spXMLDOM' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(11) : warning C4804: '>' : unsafe use of type 'bool' in operation
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(11) : warning C4552: '>' : operator has no effect; expected operator with side-effect
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(12) : error C2228: left of '.CoCreateInstance' must have class/struct/union type
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(12) : error C2065: 'DOMDocument' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(16) : error C2228: left of '.p' must have class/struct/union type
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(21) : error C2227: left of '->load' must point to class/struct/union
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(21) : error C2065: 'CComVariant' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(29) : error C2065: 'CComBSTR' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(29) : error C2146: syntax error : missing ';' before identifier 'bstrSS'
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(29) : error C2065: 'bstrSS' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(30) : error C2065: 'IXMLDOMNode' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(30) : error C2065: 'spXMLNode' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(30) : warning C4804: '>' : unsafe use of type 'bool' in operation
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(30) : warning C4552: '>' : operator has no effect; expected operator with side-effect
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(31) : error C2227: left of '->selectSingleNode' must point to class/struct/union
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(35) : error C2228: left of '.p' must have class/struct/union type
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(38) : error C2146: syntax error : missing ';' before identifier 'varValue'
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(38) : error C2065: 'varValue' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(39) : error C2227: left of '->get_nodeTypedValue' must point to class/struct/union
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(43) : error C2228: left of '.vt' must have class/struct/union type
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(46) : error C2065: 'USES_CONVERSION' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(47) : error C2065: 'W2T' : undeclared identifier
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(47) : error C2228: left of '.bstrVal' must have class/struct/union type
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(47) : error C2440: 'initializing' : cannot convert from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(48) : error C2653: 'std' : is not a class or namespace name
D:\Documents and Settings\Administrator\Desktop\cpptest\forschung1\forschung1.cpp(48) : error C2653: 'std' : is not a class or namespace name
Error executing cl.exe.
forschung1.exe - 25 error(s), 4 warning(s)
в чем беда, не подскажите?
но теперь находит ошибки в msxml.h :wow:
Код:
Compiling...
forschung1.cpp
d:\program files\microsoft visual studio\vc98\include\msxml.h(26) : error C2146: syntax error : missing ';' before identifier 'IXMLElementCollection'
d:\program files\microsoft visual studio\vc98\include\msxml.h(26) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
forschung1.exe - 2 error(s), 0 warning(s)
forschung1.cpp
d:\program files\microsoft visual studio\vc98\include\msxml.h(26) : error C2146: syntax error : missing ';' before identifier 'IXMLElementCollection'
d:\program files\microsoft visual studio\vc98\include\msxml.h(26) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
forschung1.exe - 2 error(s), 0 warning(s)