MFC C++ XML Parse - Using MSXML.

  • Thread starter Thread starter JosephParser
  • Start date Start date
J

JosephParser

Guest
Good day,

I got stuck to this part of the feature I am designing can you please help?

Here is my XML Code:

<tables>
<table name="Stamps.0409
<row index="1" >
<column>1st Class Letter</column>
<column>(Up to 100g)</column>
<column>60p</column>
<column>stampbutton1.png</column>
</row>
<row index="2" >
<column>2nd Class Letter</column>
<column>(Up to 100g)</column>
<column>50p</column>
<column>stampbutton2.png</column>
</row>
</table>
</tables>




Here is my Method:

//log
_bstr_t logData;
_bstr_t logAttribute;


//Attribute variables
_variant_t vValue;
BSTR bstrAttributeName = ::SysAllocString(_T("row"));
IXMLDOMElement *pIDOMElement = NULL;

IXMLDOMNodeList *pIDOMNodeList = NULL;
IXMLDOMNode *pIDOMNode = NULL;

BSTR bstrItemText = NULL;

long value = 0;
HRESULT hResult = CoInitialize(NULL);
if(SUCCEEDED(hResult))
{
try{
MSXML2::IXMLDOMDocumentPtr XMLToRead;
MSXML2::IXMLDOMNodeListPtr NodeListPtr;
MSXML2::IXMLDOMNodePtr NodeRowPtr, NodeIndexPtr, NodeTablePtr, NodeNamePtr;

hResult = XMLToRead.CreateInstance(__uuidof(MSXML2::DOMDocument40), NULL, CLSCTX_INPROC_SERVER);
//TODO:PLACE LOG and TEST.
if(XMLToRead->load(cXMLFile) != VARIANT_TRUE)
{
trace(L6,_T("XMLDataReader: Failed to Read Data on XML"));
}

trace(L6,_T("XMLDataReader: Successful Read Data on XML"));

//load xml
_variant_t varXml(cXMLFile);
_variant_t varOut((bool)TRUE);
varOut = XMLToRead->load(cXMLFile);
if((bool)varOut == FALSE)
throw(0);

ofstream logfile;
logfile.open("c:\\UKPostOfficeStampDat.log");
NodeListPtr = XMLToRead->getElementsByTagName("column");
//Attribute
//Table
NodeTablePtr = XMLToRead->selectSingleNode("//table");
NodeNamePtr = NodeTablePtr->attributes->getNamedItem("name");
//Row
NodeRowPtr = XMLToRead->selectSingleNode("//row");
NodeIndexPtr = NodeRowPtr->attributes->getNamedItem("index");

string logAtt = _bstr_t(NodeIndexPtr->nodeValue);
string logTable = _bstr_t(NodeNamePtr->nodeValue);

logfile<<logTable<<","<<logAtt<<",";
for(int i = 0; i < NodeListPtr->length; i++)
{
logData = NodeListPtr->Getitem(i)->text;
logfile<<(string)logData<<",";

}

logfile.close();

trace(L6,_T("XMLDataReader: Successful Write Data on log"));


}
catch(_com_error &e)
{
trace(L6,_T("XMLDataReader: ERROR: %ws"), e.ErrorMessage());
}

CoUninitialize();

}

This is my output code:

Stamps.0409,1,1st Class Letter,(Up to 100g),60p,stampbutton.png,1st Class Letter,(Up to 100g),60p,stampbutton.png,


It seems like I have only the first part of the XML data


What I want to do is this:

Stamps.0409,1,1st Class Letter,(Up to 100g),60p,stampbutton.png,2,2nd Class Letter,(Up to 100g),50p,stampbutton2.png,


since there is row 2. It should have a 2 before the 2nd XML data.


Thanks,

Continue reading...
 
Back
Top