Re: MSXML4 (Document Object Model XML), read the file, read the
variables, fill it, send over socket
Hello Pete,
I make this:
It is working, but I think, maybe is not optimize.
Do you know a better way?
I use this file. I read it and change the values.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<header eventId="Rt-Test Old eventId 000001111" version="Rt-Test Old
version V2.01" timeStamp="Rt-Test Old timeStamp 22.02.2008">
<location lineNo="Rt-Test Old lineNo Line0931" statNo="Rt-Test Old
statNo 920-270" statIdx="Rt-Test Old statIdx 534523" />
</header>
<event>
<partProcessed identifier="Rt-Test Old identifier 4545353535353"/>
</event>
<body>
<identifierType>FL</identifierType>
<resHead result="Rt-Test Old result 3333" typeNo="Rt-Test Old typeNo
3333" />
<partCount>Rt-Test001 partCount Neu</partCount>
<results>
<result position="1" state="Rt-Test Old state 3333" removed="Rt-
Test false"/>
</results>
<items>
<item name="Component1.timeStamp" value="Rt-Test001 timeStamp"
dataType="8"/>
<item name="Component1.Identifier" value="Rt-Test002 Identifier"
dataType="8"/>
</items>
</body>
</root>
Regards Andreas
CoInitialize(NULL);
try
{
IXMLDOMDocument2Ptr pXMLDoc = NULL;
HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument30));
// Set parser property settings
pXMLDoc->async = VARIANT_FALSE;
// Load the sample XML file
hr = pXMLDoc->load("C:\\xml_rt2.xml");
DWORD resCode = -1;
IXMLDOMNodePtr workNode;
IXMLDOMNodePtr resultNode;
resultNode = pXMLDoc->selectSingleNode(_T("//root/header"));
if (NULL != resultNode)
{
workNode = resultNode->attributes->getNamedItem("eventId");
if (NULL != workNode)
{
//resCode = _ttol(workNode->text);
workNode->text = "Rt-Test New eventId 000001111";
}
workNode = resultNode->attributes->getNamedItem("version");
if (NULL != workNode)
{
//resCode = _ttol(workNode->text);
workNode->text = "Rt-Test New version V2.01";
}
workNode = resultNode->attributes->getNamedItem("timeStamp");
if (NULL != workNode)
{
//resCode = _ttol(workNode->text);
workNode->text = "Rt-Test New timeStamp 22.02.2008";
}
}
//----------------------------------------------------
resultNode = pXMLDoc->selectSingleNode(_T("//root/header/
location"));
if (NULL != resultNode)
{
workNode = resultNode->attributes->getNamedItem("lineNo");
if (NULL != workNode)
{
//resCode = _ttol(workNode->text);
workNode->text = "Rt-Test New lineNo Line0931";
}
workNode = resultNode->attributes->getNamedItem("statNo");
if (NULL != workNode)
{
//resCode = _ttol(workNode->text);
workNode->text = "Rt-Test New statNo 920-270";
}
workNode = resultNode->attributes->getNamedItem("statIdx");
if (NULL != workNode)
{
//resCode = _ttol(workNode->text);
workNode->text = "Rt-Test New statIdx 534523";
}
}
//----------------------------------------------------
resultNode = pXMLDoc->selectSingleNode(_T("//root/event/
partProcessed"));
if (NULL != resultNode)
{
workNode = resultNode->attributes->getNamedItem("identifier");
if (NULL != workNode)
{
//resCode = _ttol(workNode->text);
workNode->text = "Rt-Test New identifier 4545353535353";
}
}
//----------------------------------------------------
resultNode = pXMLDoc->selectSingleNode(_T("//root/body/resHead"));
if (NULL != resultNode)
{
workNode = resultNode->attributes->getNamedItem("result");
if (NULL != workNode)
{
//resCode = _ttol(workNode->text);
workNode->text = "Rt-Test New result 3333";
}
workNode = resultNode->attributes->getNamedItem("typeNo");
if (NULL != workNode)
{
workNode->text = "Rt-Test New typeNo 3333";
}
}
//----------------------------------------------------
resultNode = pXMLDoc->selectSingleNode(_T("//root/body/
partCount"));
if (NULL != resultNode)
{
CString sTest = LPCTSTR(resultNode->text);
resultNode->text = "Rt-Test001 partCount Neu";
}
//----------------------------------------------------
resultNode = pXMLDoc->selectSingleNode(_T("//root/body/results/
result[@position]"));
if (NULL != resultNode)
{
workNode = resultNode->attributes->getNamedItem("state");
if (NULL != workNode)
{
//resCode = _ttol(workNode->text);
workNode->text = "Rt-Test New state 3333";
}
workNode = resultNode->attributes->getNamedItem("removed");
if (NULL != workNode)
{
workNode->text = "Rt-Test false";
}
}
//----------------------------------------------------
IXMLDOMNodePtr itemNode;
itemNode = pXMLDoc->selectSingleNode(_T("//root/body/items/
item[@name='Component1.timeStamp']"));
if (NULL != itemNode)
{
workNode = itemNode->attributes->getNamedItem(_T("value"));
if (NULL != workNode)
{
CString sTest = LPCTSTR(workNode->text);
workNode->text = "Rt-Test001 timeStamp";
}
}
itemNode = pXMLDoc->selectSingleNode(_T("//root/body/items/
item[@name='Component1.Identifier']"));
if (NULL != itemNode)
{
workNode = itemNode->attributes->getNamedItem(_T("value"));
if (NULL != workNode)
{
CString sTest = LPCTSTR(workNode->text);
workNode->text = "Rt-Test002 Identifier";
}
}
/* workNode->Release();
workNode = NULL;
resultNode->Release();
resultNode = NULL;
itemNode->Release();
itemNode = NULL;
*/
AfxMessageBox(pXMLDoc->xml);
hr = pXMLDoc->save("C:\\xml_rt2_NEU.xml");
}
catch(_com_error &e)
{
dump_com_error(e);
}
return TRUE;