XmlLite questions:

I am using XmlLite (the reader) to parse some XML fragments, and I have a few questions.

Example fragment:

<Top>
<Second>
<Data>
<Top>
</Top>
</Data>
</Second>
</Top>

1. How can I determine when I am finish parsing?

I have tried using the GetDepth() to determine when finish, but I am not clear on the meaning, since
the depth can change when reading attributes. I also do not know exactly when the depth is changed.

2. I use CreateStreamOnHGlobal to create a stream used for IXmlWriter. When finish with IXmlWriter
calls, I return the XML data in a CStringA by using:

// Get the size of the stream.

ULONG nRead;
ULONG nSize;
STATSTG oStatus;
hr = m_pMemoryStream->Stat(&oStatus, STATFLAG_DEFAULT);

nSize = (ULONG)oStatus.cbSize.LowPart ;

// Go to the beginning of the stream.

hr = RewindStream();
assert(SUCCEEDED(hr));

// Read the XML formatted data.

hr = m_pMemoryStream->Read(sBuffer.GetBuffer(nSize), nSize, &nRead);
sBuffer.ReleaseBuffer();

However, it seems the size of the stream may be greater than the size of the XML written. How can I
determine the number of bytes written by IXmlWriter?


--
Frank

Re: A few questions about XmlLite by Giovanni

Giovanni
Tue Jul 22 17:35:55 CDT 2008

[Frank S]

> I am using XmlLite (the reader) to parse some XML fragments, and I have a
> few questions.
[...]
> 1. How can I determine when I am finish parsing?

You may use IXmlReader::Read HRESULT return value and a while loop, like
this:

<code>

// IXmlReader * pXmlReader
// XmlNodeType nodeType
// HRESULT hr
while ( (hr = pXmlReader->Read( &nodeType )) == S_OK )
{
... switch nodeType and do processing here
}

</code>


Giovanni



Re: A few questions about XmlLite by Frank

Frank
Wed Jul 23 07:14:59 CDT 2008

Giovanni,

I use a separate class to read each XML element. Each class has a while loop like you mentioned. I
need to know how to determine when each class is finish parsing their XML. I can't rely on the node
name when the node type is XmlNodeType_EndElement because it may be the name of a nested element.

And there is still question 2...

Thanks,

Frank

Giovanni Dicanio wrote:
> [Frank S]
>
>> I am using XmlLite (the reader) to parse some XML fragments, and I have a
>> few questions.
> [...]
>> 1. How can I determine when I am finish parsing?
>
> You may use IXmlReader::Read HRESULT return value and a while loop, like
> this:
>
> <code>
>
> // IXmlReader * pXmlReader
> // XmlNodeType nodeType
> // HRESULT hr
> while ( (hr = pXmlReader->Read( &nodeType )) == S_OK )
> {
> ... switch nodeType and do processing here
> }
>
> </code>
>
>
> Giovanni
>
>

--
Frank

Re: A few questions about XmlLite by changliw

changliw
Thu Jul 24 08:06:29 CDT 2008

Hi Frank,
Regarding your first question, you can use IXmlReader::MoveToNextAttribute(), for example:
HRESULT hr = pXmlReader->MoveToFirstAttribute();
if(hr!=S_OK)
return;
else
{
while(TRUE)
{
if(S_OK!=pXmlReader->MoveToNextAttribute())
break;
}
}

Regarding your second question, I think that there might be some encoding issue that you
encountered. I recommend that you refer to this article to see read Xml formatted data.
A Small And Fast XML Parser For Native C++
http://msdn.microsoft.com/en-us/magazine/cc163436.aspx

There are many Write methods for IXmlWriter, however there is no public properties or
method for counting the total number of bytes written to your file. You need to count the
number of bytes written by IXmlWriter by yourself when you call related Write methods to
write data according to the parameters.

Hope this helps. If you have any other questions or concerns, please feel free to let me know.

Have a nice day!

Best regards,
Charles Wang
Microsoft Online Community Support
===========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: msdnmg@microsoft.com.
===========================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for
non-urgent issues where an initial response from the community
or a Microsoft Support Engineer within 1 business day is acceptable.
Please note that each follow up response may take approximately
2 business days as the support professional working with you may
need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by
contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
============================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================


Re: A few questions about XmlLite by changliw

changliw
Thu Jul 24 08:12:35 CDT 2008

Sorry, I misunderstood something. My answer for your first question was reading through the
attributes of a give node. Currently I just find one way of using IXmlReader::Read method to
judge if all nodes are parsed. The method is same as Giovanni's.

Now I am trying to consult our product team to see if there are any other alternative methods.
Appreciate your patience!

Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: msdnmg@microsoft.com.
=========================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================


Re: A few questions about XmlLite by Frank

Frank
Thu Jul 24 15:30:20 CDT 2008



Charles Wang [MSFT] wrote:
> Sorry, I misunderstood something. My answer for your first question was reading through the
> attributes of a give node. Currently I just find one way of using IXmlReader::Read method to
> judge if all nodes are parsed. The method is same as Giovanni's.
>
> Now I am trying to consult our product team to see if there are any other alternative methods.
> Appreciate your patience!
>
> Best regards,
> Charles Wang
> Microsoft Online Community Support
> =========================================================
> Delighting our customers is our #1 priority. We welcome your
> comments and suggestions about how we can improve the
> support we provide to you. Please feel free to let my manager
> know what you think of the level of service provided. You can
> send feedback directly to my manager at: msdnmg@microsoft.com.
> =========================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
> =========================================================
>
Just to clarify what I need...

If I am passed an IXmlReader interface after the caller has read the XmlNodeType_Element for node
XYZ, how do I know when a subsequent XmlNodeType_EndElement is the end element for XYZ? Keep in mind
that element XYZ might contain XYZ elements.

Regards,

Frank

Re: A few questions about XmlLite by David

David
Thu Jul 24 16:43:59 CDT 2008

>If I am passed an IXmlReader interface after the caller has read the XmlNodeType_Element for node
>XYZ, how do I know when a subsequent XmlNodeType_EndElement is the end element for XYZ? Keep in mind
>that element XYZ might contain XYZ elements.

Frank,

A recursive solution would appear to fit the bill - i.e. when you
encounter a new element, recurse into it's handler and return when the
handler has encountered an end element - which should be it's own end
element otherwise the XML is faulty.

Dave

Re: A few questions about XmlLite by Frank

Frank
Mon Jul 28 16:57:10 CDT 2008



David Lowndes wrote:
>> If I am passed an IXmlReader interface after the caller has read the XmlNodeType_Element for node
>> XYZ, how do I know when a subsequent XmlNodeType_EndElement is the end element for XYZ? Keep in mind
>> that element XYZ might contain XYZ elements.
>
> Frank,
>
> A recursive solution would appear to fit the bill - i.e. when you
> encounter a new element, recurse into it's handler and return when the
> handler has encountered an end element - which should be it's own end
> element otherwise the XML is faulty.
>
> Dave

That would work normally, but I have one case where I want to parse all the elements contained in a
<DATA>...</DATA> tree. I can't use recursion here.

In another case, I would love to be able to read all of the data in the <DATA> element without
parsing. But IXmlReader does not offer this capability, as far as I know.

Regards,

Frank

Re: A few questions about XmlLite by David

David
Mon Jul 28 17:57:35 CDT 2008

>> A recursive solution would appear to fit the bill - i.e. when you
>> encounter a new element, recurse into it's handler and return when the
>> handler has encountered an end element - which should be it's own end
>> element otherwise the XML is faulty.
>>
>> Dave
>
>That would work normally, but I have one case where I want to parse all the elements contained in a
><DATA>...</DATA> tree. I can't use recursion here.

It may not be immediately obvious, but I feel sure that there will be
some (relatively) elegant way of structuring your code/classes to
accommodate the existing XML data you have.

>In another case, I would love to be able to read all of the data in the <DATA> element without
>parsing. But IXmlReader does not offer this capability, as far as I know.

No, I don't believe you can do DOM like operations with it.

Dave

Re: A few questions about XmlLite by Alex

Alex
Tue Jul 29 03:50:27 CDT 2008

"Frank S" wrote:
> In another case, I would love to be able to read all of the data
> in the <DATA> element without parsing. But IXmlReader does not
> offer this capability, as far as I know.

I'd second David's advice. It seems that DOM would be more
appropriate tool for the job.

Alex



Re: A few questions about XmlLite by changliw

changliw
Wed Jul 30 04:05:11 CDT 2008

Hi Frank,
I was confirmed that you could only use the Read method of IXmlReader to parse a
XML segment. The reason is that XmlLite is simply a lightweight XML parser that has
higher performance for parsing XML content than MSXML. It focuses on high
performance on reading/writing XML and does not provide many complex methods like
MSXML.

Regarding your first question, I recommend that you use a counter to count the
occurance of the start and end element DATA. Assume that you set the counter n=1 for
the first DATA element position. Then call IXmlReader::Read method to loop reading
through the nodes, when you read another element DATA, increase the counter by +1;
when you read an end element DATA, decrease the counter by -1. By this way, when
you read the end element DATA and find that the counter value equals to 1, you can
finish parsing the XML segment <DATA>...</DATA>.
Regarding reading XML nodes, you can refer to this article:
Read
http://msdn.microsoft.com/en-us/library/ms752878(VS.85).aspx

Regarding your second question, I agree with Dave. You have to parse the XML
segment to read all the data if you need to use XmlLite library.

Hope this helps. If you have any other questions or concerns, please feel free to let us
know.

Have a nice day!

Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: msdnmg@microsoft.com.
=========================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================