Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XmlLite issue - IXmlReader::IsEmptyElement()

From: dj11235813@-----.---
To: NULL
Date: 1/31/2008 7:26:00 AM
On Jan 31, 10:11 am, dj11235...@gmail.com wrote:
> Hello
>
> I'm working on a product that is using XmlLite.  Following the example
> given by Microsoft on their website (http://msdn2.microsoft.com/en-us/
> library/ms753116(VS.85).aspx) I have made a while loop that calls
> IXmlReader::Read() and switches on the nodeType gotten.  In the
> example, they use IXmlReader::IsEmptyElement() to determine if a node
> looks like this:
>
> <nodeName attributeName="attributeValue" />
>
> As I am walking through a hierarchical XML, I need to know when a node
> ends.  So I have both that call to IXmlReader::IsEmptyElement() and
> code in the case for XmlNodeType_EndElement that walks me back up to
> the parent.
>
> Unfortunately, I am not seeing IXmlReader::IsEmptyElement() returning
> true ever.  The node it is reading in my testing is this:
>
>           <parameterType name="stringParameter" type="string"
> multiValued="false" />
>
> And the code reading that node is effectively this:
>
>         while(S_OK == (hr = xmlReader->Read(&nodeType)))
>         {
>                 switch(nodeType)
>                 {
>                 case XmlNodeType_Element:
>                         {
>                                 if(FAILED(hr = xmlReader->GetLocalName(&nodeName, NULL)))
>                                         // ... error handling
>
>                                 // ... do stuff with the node
>
>                                 while(true)
>                                 {
>                                         if(FAILED(hr = reader->MoveToNextAttribute()))
>                                                 // ... error handling
>
>                                         if(S_FALSE == hr)
>                                                 break;
>
>                                         if(FAILED(hr = reader->GetLocalName(&attributeName, NULL)))
>                                                 // ... error handling
>
>                                         if(FAILED(hr = reader->GetValue(&attributeValue, NULL)))
>                                                 // ... error handling
>
>                                         // ... do stuff with the attribute
>                                 }
>
>                                 // ... I never see this return true, even on the node example
> above
>                                 if(! xmlReader->IsEmptyElement())
>                                         // ... walk down the hierarchical tree of data
>
>                                 // ... note that I expect to stay put (not walk down) if the
> element is empty
>                         }
>                         break;
>                 case XmlNodeType_EndElement:
>                         {
>                                 // ... walk up the hierarchical tree of data
>                         }
>                         break;
>
>                 // ...
>
> If anyone has used IXmlReader::IsEmptyElement successfully, can you
> please tell me what you did to make it work?  Or, if anyone else has
> encountered the same problem, I would be encouraged to hear that,
> because then I wouldn't feel so bad kludging the hierarchical logic.

Hi again

I have discovered the problem.  The IXmlReader loses its state about
the element once you go read stuff about the attributes.  The example
code provided by Microsoft (http://msdn2.microsoft.com/en-us/library/
ms753116(VS.85).aspx) is incorrect.  You need to check
IsEmptyElement() before doing any work with the attributes.  I am
posting this to hopefully help anyone who comes along with the same
problem later on.  I now have this, and it works fine:

	while(S_OK == (hr = xmlReader->Read(&nodeType)))
	{
		switch(nodeType)
		{
		case XmlNodeType_Element:
			{
				// ******************** new boolean to save the state
				isEmptyElement = xmlReader->IsEmptyElement();

				if(FAILED(hr = xmlReader->GetLocalName(&nodeName, NULL)))
					// ... error handling

				// ... do stuff with the node

				while(true)
				{
					if(FAILED(hr = reader->MoveToNextAttribute()))
						// ... error handling

					if(S_FALSE == hr)
						break;

					if(FAILED(hr = reader->GetLocalName(&localAttributeName, NULL)))
						// ... error handling

					if(FAILED(hr = reader->GetValue(&value, NULL)))
						// ... error handling

					// ... do stuff with the attribute
				}

				// ******************** check my saved state rather than asking
the reader
				if(! isEmptyElement)
					// ... walk down the heirarchical tree of data

				// ... note that I expect to stay put (not walk down) if the
element is empty
			}
			break;
		case XmlNodeType_EndElement:
			{
				// ... walk up the hierarchical tree of data
			}
			break;

		// ...

I find this behavior tremendously annoying, but at least I was able to
get things working.


transparent
Print
Mail
Like It
Disclaimer
.

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.

.
.

transparent

transparent