[Gambas-user] XML - Property ReadFlags

Adrien Prokopowicz adrien.prokopowicz at ...626...
Fri Aug 11 15:24:28 CEST 2017


Le Fri, 11 Aug 2017 09:05:18 +0200, Hans Lehmann <hans at ...3219...> a  
écrit:
>Hello Adrien,
>Thanks for the explanation to ReadFlags in the above example. If you  
> want to read and display the content of the structure of an XML  
> document, the output is not helpful because it seems illogical.
>https://gambas-playground.proko.eu/?gist=b90eeff3dacbec0548d01701f3c05133
> --------------------------------------------------------------------------
>Use "gb.xml"
>Dim contents As String = "<xml>"
> "<foo>"
> "  <p attr=\"1\" mausi=\"xy\">Hello</p>"
> "  <p attr=\"2\">Hi</p>"
> "  <p attr=\"3\">Greetings</p>"
> "</foo>"
> "</xml>"
>Dim reader As New XmlReader
> reader.FromString(contents)
>'Toggle these :)
> reader.ReadFlags[XmlReaderNodeType.Element] = True
> reader.ReadFlags[XmlReaderNodeType.Attribute] = True
> reader.ReadFlags[XmlReaderNodeType.Text] = True
> reader.ReadFlags[XmlReaderNodeType.EndElement] = false
>reader.Read()
>Repeat
>     Print reader.Node.Type;;
>     Select Case reader.Node.Type
>       Case XmlReaderNodeType.Element
>         Print "element";;reader.Node.name
>       Case XmlReaderNodeType.Attribute
>         Print "attribute"
>       Case XmlReaderNodeType.Text
>         Print "text";;reader.node.value
>       Case XmlReaderNodeType.Comment
>         Print "comment"
>       Case XmlReaderNodeType.CDATA
>         Print "cdata"
>       Case XmlReaderNodeType.EndElement
>         Print "endelement"
>       Default
>       Print ""
>     End Select
>     reader.Read()
> Until reader.Eof
>Original-Output in a console:
>1 element xml1 element foo8 attribute8 attribute1 element p2 text Hello8  
> attribute1 element p2 text Hi8 attribute1 element p2 text Greetings
>This output I would have expected because it shows the structure of the  
> XML file. The upper output does not show which element the attributes  
> belong to. Extremely confusing!
>1 element xml1 element foo1 element p8 attribute8 attribute2 text Hello1  
> element p8 attribute2 text Hi1 element p8 attribute2 text Greetings
>What now - what to do?
>RegardsHans,who still has many questions about the component gb.xml ...

This is the normal behavior : the Read() method returns when it *finishes*  
reading something. Therefore, it finishes reading the attributes *before*  
finishing reading the element.

While this behavior is intended, I completely agree that it is very  
confusing.
However, XmlReader is not an easy class to use : its design is based on  
the standard XML SAX API, which is meant to be used when your data comes  
 from a stream (hence the complexity : you don't know what to expect going  
forward, and you have to remember yourself what has been previously read).

The XmlDocument class, on the other hand, is based on the XML DOM API. It  
is much easier to navigate into an XML document, and all it requires is to  
have the entirety of the document loaded in memory.

This requirement makes XmlReader more appropriate if you are in one of  
these two situations :
- The Document comes from a very slow stream source (i.e. a *very* slow  
network connection), and you want to start reading the document before it  
finishes loading.
- The Document you're trying to load is very big, and you can't afford to  
load it all in memory (today, it means more than 100MB in size, which is  
already crazy for an XML Document).

These are the only two reasons I see for actually using the XmlReader  
class. Otherwise, use the XmlDocument class, it is *way* simpler and more  
powerful. :)

(I also wrote a page on the wiki explaining the differences here :  
http://gambaswiki.org/wiki/doc/xml )

-- 
Adrien Prokopowicz




More information about the User mailing list