Re: [xml] Getting out of xmlTextReaderReadAttributeValue mode

Hi Csaba, thanks for the reply! I think however I need to clarify my
question a little.

On Thu, Aug 22, 2013 at 10:49 AM, Csaba Raduly <rcsaba gmail com> wrote:
Hi Simon,

On Mon, Aug 19, 2013 at 11:54 PM, Simon Kågedal Reimer  wrote:

I'm having difficulties using xmlTextReaderReadAttributeValue. It
doesn't seem to work like the equivalent function in ECMA/.NET, at
least as implemented by Mono. Consider the following simple C#

It parses every attribute of every element that has attributes, and
for each attribute print the texts and entity references contained in
that attribute's value. In this case, it just prints "foo" and "bar",
as expected. Now, take a look at the equivalent C code using libxml2:

libxml2 doesn't quite work like that.

Once you called xmlTextReaderRead [optionally check that
xmlTextReaderNodeType() == 1, a.k.a. element ], you should call
xmlTextReaderMoveToFirstAttribute, then
xmlTextReaderMoveToNextAttribute. This would iterate through all the
At the end you should call xmlTextReaderMoveToElement(), and then you
can resume calling xmlTextReaderRead()

Actually, it works fine to call xmlTextReaderMoveToNextAttribute
directly after the xmlTextReaderRead call, and you don't have to call
xmlTextReaderMoveToElement before doing the next read. This code works
fine for iterating through elements and their attributes:

It prints the doc element and the two elements elem1 and elem2, with
two attributes each.

My question, however, concerns the function
xmlTextReaderReadAttributeValue, that "parses an attribute value into
one or more Text and EntityReference nodes" (from the docs). For
example, this code correctly prints both children of an attribute
value that has both text content and an entity reference:

But it fails when an element has more attributes to read. The problem
is that, when in "reading attribute value"-mode, the call to
xmlTextReaderMoveToNextAttribute does not allow you to move to the
next value; it just returns 0. I would expect it to do that, just like
xmlTextReaderRead allows you to move ahead relative to the previous
read, even if you're on an attribute.

Failing that, I would want some kind of function to move one step up
back to the attribute, just like xmlTextReaderMoveToElement allows you
to move one step up back to the element. But I can't find any such

A possible workaround would be to instead iterate through the
attributes by number, but I would prefer to use "next" as it's more
simple and clear.

Best regards,
Simon Kågedal Reimer

[Csaba, sorry for the double post to you, I accidentally did not add
xml-list to the reply the first time.]

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]