 |
 |
 |
Hi,
I am trying to read an xml doc using msxml. I am having trouble
accessing child nodes.
In the test xml file I am trying to read below, there are two sections
here. I have used some code I found on a web site and it can read the
top portion fine.
In the second section, starting with controllor harnessID, I cannot
read circiutNumber or type. Not sure what the problem is. I have
posted my console app code below the xml file.
Any good examples would sure be appreciated. I am trying to read the
nodes and then store the data in strings.
Thanks
Jeff
<?xml version="1.0" encoding="UTF-8"?>
<SampleDocumentElement
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="SampleSchema.xsd">
<!--This comment is a child of the document element-->
<FirstLevelChild Attribute1="Attribute Value">
<SecondLevelChild>Element Text</SecondLevelChild>
</FirstLevelChild>
<FirstLevelChild/>
<controller harnessId="0xE"/>
<module moduleType="constantCurrent" harnessId="0xAE"
inputCount="4" outputCount="8">
<input ioPoint="1">
<description>
<circuitName>Weight Transfer Transducer</circuitName>
<circuitNumber>1</circuitNumber>
</description>
<type>batteryOrGround</type>
</input>
</module>
</SampleDocumentElement>
// XMLTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "XMLTest.h"
//Might have used MSXML4 previously
#import <msxml4.dll>
using namespace MSXML2;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
// Local variables and initializations
HRESULT hResult = S_OK;
int i;
char cNodeType;
MSXML2::IXMLDOMTextPtr typeText;
cout << endl << "Sample ConsumeXML Program" << endl << endl;
// Initialize the COM library
hResult = CoInitialize(NULL);
if (FAILED(hResult))
{
cerr << "Failed to initialize COM environment" << endl;
return 0;
}
// Main try block for MSXML DOM operations
try
{
// MSXML COM smart pointers
// Use the Dccument2 class to enable schema validation
MSXML2::IXMLDOMDocument2Ptr spDocInput;
MSXML2::IXMLDOMNodePtr spNodeTemp;
MSXML2::IXMLDOMElementPtr spElemTemp;
MSXML2::IXMLDOMAttributePtr spAttrTemp;
MSXML2::IXMLDOMNodeListPtr spNLChildren;
// Create the COM DOM Document object
hResult = spDocInput.CreateInstance(__uuidof(DOMDocument40));
if FAILED(hResult)
{
cerr << "Failed to create Document instance" << endl;
return 1;
}
// Load the document synchronously
spDocInput->async = VARIANT_FALSE;
// Load input document. MSXML default for load is to
// validate while parsing.
//hResult = spDocInput->load("SampleDocument.xml");
hResult = spDocInput->load("IOConfig-1-2-0.xml");
// Check for load, parse, or validation errors
if( hResult != VARIANT_TRUE)
{
cout << "Parsing error" << endl;
return 1;
}
// Get document element
spElemTemp = spDocInput->documentElement;
cout << endl
//this line outputs ioConfig element
<< "Document Element name: "
<< spElemTemp->nodeName << endl << endl;
// Walk through children of document element
// and process according to type
spNodeTemp = spElemTemp->firstChild;
//Test to see what this prints out
//cout << endl
// //this line outputs the controllor element
// << "A node name?: "
// << spNodeTemp->nodeName << endl << endl;
//end print test
while (spNodeTemp != NULL)
{
// Process node depending on type
cNodeType = spNodeTemp->nodeType;
switch (cNodeType)
{
// Comment Node
case MSXML2::NODE_COMMENT:
cout << "Comment Node:" << endl << " "
<< _bstr_t(spNodeTemp->nodeValue)
<< endl << endl;
break;
// Element Node
case MSXML2::NODE_ELEMENT:
spElemTemp = (MSXML2::IXMLDOMElementPtr) spNodeTemp;
cout << "Element name: "
<< spElemTemp->nodeName << endl;
// Display the value of Attribute1, if present
// MSXML doesn't support the hasAttribute method,
// so we'll try to get the Attribute node,
// then its value.
/*spAttrTemp =
spElemTemp->getAttributeNode("Attribute1");
if (spAttrTemp != NULL)
{
cout << " Attribute1 Value: "
<< _bstr_t(spAttrTemp->nodeValue)
<< endl;
}*/
spAttrTemp =
spElemTemp->getAttributeNode("moduleType");
if (spAttrTemp != NULL)
{
cout << " Module Type: "
<< _bstr_t(spAttrTemp->nodeValue)
<< endl;
}
//Try to get the harness ID
spAttrTemp =
spElemTemp->getAttributeNode("harnessId");
if (spAttrTemp != NULL)
{
cout << " Harness ID: "
<< _bstr_t(spAttrTemp->nodeValue)
<< endl;
//Test string coversions
USES_CONVERSION;
BSTR bstr = _bstr_t(spAttrTemp->nodeValue);
CString testStr = _bstr_t(spAttrTemp->nodeValue);
CString str(bstr == NULL ? L"" : bstr);
cout << str;
SysFreeString(bstr);
//end string conversion test
}
//Try to get the Input Count
spAttrTemp =
spElemTemp->getAttributeNode("inputCount");
if (spAttrTemp != NULL)
{
cout << " InputCount: "
<< _bstr_t(spAttrTemp->nodeValue)
<< endl;
//Test string coversions
USES_CONVERSION;
BSTR bstr = _bstr_t(spAttrTemp->nodeValue);
CString testStr = _bstr_t(spAttrTemp->nodeValue);
CString str(bstr == NULL ? L"" : bstr);
// wcout << str;
SysFreeString(bstr);
//end string conversion test
}
//Try to get the Output Count
spAttrTemp =
spElemTemp->getAttributeNode("outputCount");
if (spAttrTemp != NULL)
{
cout << " OutputCount: "
<< _bstr_t(spAttrTemp->nodeValue)
<< endl;
}
//Try to get the Input Number
spAttrTemp =
spElemTemp->getAttributeNode("input");
if (spAttrTemp != NULL)
{
cout << " ioPoint: "
<< _bstr_t(spAttrTemp->nodeValue)
<< endl;
}
// Process SecondLevelChild children
// of FirstLevelChild
spNLChildren =
spElemTemp->getElementsByTagName("circuitName");
// Get the text node with the element content,
// and display it. If present it will be the
// first child.
for (i = 0; i < spNLChildren->length; i++)
{
spElemTemp =
(MSXML2::IXMLDOMElementPtr) spNLChildren->item[i];
cout << " Element name: "
<< spElemTemp->nodeName << endl;
// Get the text node with the element content,
// and display it. If present it will be the
// first child.
MSXML2::IXMLDOMTextPtr crtDescText = spElemTemp-
>firstChild;
if (crtDescText != NULL)
{
cout << " Circuit Name: "
<< _bstr_t(crtDescText->nodeValue) << endl;
}
//crtDescText = spDocInput->selectSingleNode("circuitNumber");
}// end
// Process SecondLevelChild children
// of FirstLevelChild
spNLChildren =
spElemTemp->getElementsByTagName("circuitNumber");
// Get the text node with the element content,
// and display it. If present it will be the
// first child.
for (i = 0; i < spNLChildren->length; i++)
{
spElemTemp =
(MSXML2::IXMLDOMElementPtr) spNLChildren->item[i];
cout << " Element name2: "
<< spElemTemp->nodeName << endl;
// Get the text node with the element content,
// and display it. If present it will be the
// first child.
MSXML2::IXMLDOMTextPtr crtNumText = spElemTemp-
>firstChild;
if (crtNumText != NULL)
{
cout << " Circuit Number: "
<< _bstr_t(crtNumText->nodeValue) << endl;
}
//crtDescText = spDocInput->selectSingleNode("circuitNumber");
}// end
typeText = spDocInput->selectSingleNode("/ioConfig");
// MSXML2::IXMLDOMTextPtr crtNumText = spDocInput-
>selectSingleNode("circuitNumber");
// Process SecondLevelChild children
// of FirstLevelChild
//spNLChildren =
// spElemTemp->getElementsByTagName("SecondLevelChild");
//for (i = 0; i < spNLChildren->length; i++)
//{
// spElemTemp =
// (MSXML2::IXMLDOMElementPtr) spNLChildren->item[i];
// cout << " Element name: "
// << spElemTemp->nodeName << endl;
// // Get the text node with the element content,
// // and display it. If present it will be the
// // first child.
// MSXML2::IXMLDOMTextPtr spText = spElemTemp->firstChild;
// if (spText != NULL)
// {
// cout << " Element content: "
// << _bstr_t(spText->nodeValue) << endl;
// }
//}// end
// Finished with FirstLevelChild element
cout << endl;
break;
// Everything else allowed by the schema
default:
// Skip unexpected processing instructions
// and white space text nodes
break;
} // End of switch block
// Get the next child of the Document Element
spNodeTemp = spNodeTemp->nextSibling;
} // End of while block
} // End of try block
// Catch COM exceptions
catch (_com_error &e)
{
cerr << "COM Error" << endl;
cerr << "Message = " << e.ErrorMessage() << endl;
return 1;
}
// Release COM resources
CoUninitialize();
cout << endl << endl << "Successful Completion" << endl;
return 0;
}
|
 | 

|  |
These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.
|  |
| |
 |
 |
 |