XML library

corwin42

Expert
Licensed User
Longtime User
Hi Andrew,

if you have some time, can you please add a new Open() Method with the possibility to pass IgnoreWhitespace and IgnoreComments properties of XMLReaderSettings (or just expand the normal Open() but I don't know if you want to change the interface?

B4X:
        public void Open2(String filename, Boolean ignoreComment, Boolean ignoreWhitespace)
        {
            Close();
            XmlReaderSettings set = new XmlReaderSettings();
            set.IgnoreComments = ignoreComment;
            set.IgnoreWhitespace = ignoreWhitespace;
            rdr = System.Xml.XmlReader.Create(filename, set);
        }

I have tested it with reading GPX files and this makes a major speed improvement.

Thanks very much
Markus
 

mjcoon

Well-Known Member
Licensed User
I've just downloaded v2.4 (wondering how I'll get on with using it for KML).

The Help says for XmlNodeList that "Count : Int32 [O] : Gets the number of XmlNodes in the XmlNodeList."

I'm sure that, as hinted by "Gets" and by other examples, this property should be listed as "" ("I signifies readable, O signifies settable.")
 

mjcoon

Well-Known Member
Licensed User
I've just downloaded v2.4 (wondering how I'll get on with using it for KML).

I still haven't tried using the XML library. (I've been writing my KML as plain text.)

But I've had a play with the demo programs.

Got a complaint when I tried to execute (under PC IDE) XmlDocumentDemo.sbp that line 94
B4X:
      SbXml.Append(CRLF & InsetStr & "<" & nicename & " " & GetAttributes)
has InsetStr which has not been assigned a value. At start of Sub SaveChildNodes, if
B4X:
If Inset > 0 Then
is false then it will not be set. Should it merely be a null string in this case?

If I continue regardless the indented listing that I see (after dismissing an empty MsgBox!) has only some nested <font> items. Perhaps this is because the supplied TestDoc.xml file has a missing <font> terminator? Viewing with IE6 suggests this might be the case.

When I tried XmlReadWrriteDemo.sbp (sic) I saw a listing of the XML and noticed that there is a duplication:
B4X:
  ' Lookup the prefix and write the ISBN element.
  wtr.WriteStartElement("ISBN")
  wtr.WriteString("1-861003-78")
  wtr.WriteString("1-861003-78")
  wtr.WriteEndElement

This is probably just a pasting typo, but what is stranger is that I experimentally tried (just to see the effect) altering it to
B4X:
  ' Lookup the prefix and write the ISBN element.
  wtr.WriteStartElement("ISBN")
  wtr.WriteString("1-861003-78a; ")
  wtr.WriteString("1-861003-78b")
  wtr.WriteEndElement

when I tried to type the space after ";" the IDE crashes! (message attached)

Mike.
 

agraham

Expert
Licensed User
Longtime User
Uncheck Tools-> Check for unassigned /unused variables. I'm afraid I tend to run with this off as it sometimes flags things that I don't regard as errors.

The default for an unassigned variable is an empty string.

I can't see a missing font terminator in TestDoc.xml, in fact if there was one I would expect that the XML parser would throw an exception to that effect although I do admit that the examples were thrown together in haste and that I am not brilliant at, nor particularly interested in, XML (nor HTML). This library sort of ran away from me as people requested extra features!

I also get an IDE crash like you - from the stack trace it looks like a bug in the IDE at Dbasic.CCodePage.SmartHelp :(. One for Erel methinks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I also get an IDE crash like you - from the stack trace it looks like a bug in the IDE at Dbasic.CCodePage.SmartHelp
frown.gif
. One for Erel methinks!
@agraham or mjcoon can you upload a some program with this behavior?
 

agraham

Expert
Licensed User
Longtime User
Did you change the classes XMLReader / XMLWriter to XmlReader / XmlWriter (the difference is in the 'ml')?
Looking back in the source code archives I find that I did on the major upgrade to version 2.0 from 1.2. So I guess it's a partial case-insensitivity in whatever parses the Designer section of the source code file then?
 

mjcoon

Well-Known Member
Licensed User
I can't see a missing font terminator in TestDoc.xml, in fact if there was one I would expect that the XML parser would throw an exception to that effect although I do admit that the examples were thrown together in haste and that I am not brilliant at, nor particularly interested in, XML (nor HTML).

I'm happy to rescind my comment that there is a </font> missing; it's at the end of the file. Virtually the whole document is contained in the outermost font element which is no doubt valid but not what I was expecting!

I have successfully written my KML using XMLWriter, and read it back in again with XmlDocument.LoadXmlFile. I did the latter because I want to be able to add further structure to my file (several bites at my cherry!) and I can't see that this is going to be easy with XmlReader.

But I still don't understand some aspects of XmlDocument (despite having briefly used M$'s DOM at work before retiring). In particular the help says "The different Node types that may be specified by the CreateNode methods are as follows." but I cannot find how to set the node type. There is a property, but it can only be read, not set.

Mike.
 

agraham

Expert
Licensed User
Longtime User
Virtually the whole document is contained in the outermost font element
I would have thought that inevitable if you want a certain body style without having loads of font elements around individual bits of text.
But I still don't understand some aspects of XmlDocument
That makes at least two of us then!

but I cannot find how to set the node type.
You can't change a node type. It has to be created as the type you want by XmlDocument.CreateNode.

I'm afraid that the .NET XmlDocument and its' related Node objects don't map very well onto Basic4ppc as there are lots of them, one for each type of Node. Fortunately they are inherit from XmlNode with small differences so you can get away with only having a single XmlNode object in Basic4ppc.
 

mjcoon

Well-Known Member
Licensed User
I would have thought that inevitable if you want a certain body style without having loads of font elements around individual bits of text.

Yes; the document uses lots of "font elements around individual bits of text" as well. That is what led to my confusion. I guess the outermost sets up a default font which is reverted to at the end of each embedded font usage.

That makes at least two of us then! [Not understanding some aspects of XmlDocument]

Ah, may I tread in your pioneering footsteps?

You can't change a node type. It has to be created as the type you want by XmlDocument.CreateNode.

I'm afraid that the .NET XmlDocument and its related Node objects don't map very well onto Basic4ppc as there are lots of them, one for each type of Node. Fortunately they are inherit from XmlNode with small differences so you can get away with only having a single XmlNode object in Basic4ppc.

Thanks. I had found XmlDocument.CreateNode, but I have not yet used it successfully. The Help says "The should be assigned to the ControlRef of an XmlNode object for further processing." but many Help items say that, so maybe it is not highly significant! I don't understand the implication that the method has no effect on the XmlDocument node itself (I am presuming) rather than being a method of the node that is being created/established.

Part of my problem is the lack of match between the Help for V2.4 and the methods that are offered by the IDE. E.g. the former has three variants of CreateElement for XmlDocument whereas the IDE offers me none. The Help has one variant of CreateNode whereas the IDE offers two.

Also I see that the properties that are offered by the IDE for multiple (all?) objects are annotated as [O] when is obviously appropriate. But this is, I suppose, merely documentary and easy to ignore!

Mike.
 

agraham

Expert
Licensed User
Longtime User
I don't understand the implication that the method has no effect on the XmlDocument node itself (I am presuming) rather than being a method of the node that is being created/established.
It just returns a new XmlNode, you have to be able to make one somehow but you don't want literally hundreds of XmlNode objects littering up your Basic4ppc (or C#) program so you create it, assign it an XmlNode, play with it, add it to the document and move on to the next.

the former has three variants of CreateElement for XmlDocument whereas the IDE offers me none. The Help has one variant of CreateNode whereas the IDE offers two.
Sorry, when I added XmlNode and XmlNodeList I removed the CreateElement methods and added CreateNode instead but was obviously a little sloppy updating the help.

I can't document how to use the DOM (especially as I don't fully understand it myself!). I can provide the means of manipulating it, amend those means in the light of feedback and requests, but as I say in the library help "Overview" topic it needs other resources to be able to undertand how to use it.
 

mjcoon

Well-Known Member
Licensed User
A revised help file is now available at post #1.

Thank you very much Andrew.

I have now got XmlDocumentDemo.sbp.CreateNode to work for me. I had originally used this method immediately after the XmlDocument.New1 and it then failed ("Object reference not set to an instance of an object" see image). After I had done XmlDocument.LoadXmlFile it works. Fortunately this is how I could arrange things in my program (since the KML file is generated in the first place with your XMLWriter).

So perhaps the New1 generates an "empty" Basic4PPC object with no underlying .NET XmlDocument object but this does not support CreateNode. So if I did not have a starter-for-ten KML/XML file, how would I get out of this Catch-22 situation?

If I've got it wrong and there is no Catch-22, could you enhance your demo XmlDocumentDemo.sbp to include the creation of a trivial XmlDocument from scratch (no Load!)?

Mike.
 

agraham

Expert
Licensed User
Longtime User
After Googling I've found how to create a new XML document from scratch.

In version 2.5 now posted. XmlDocument.New1 now creates a new document and adds the obligatory XML declaration. Also the new XmlDocument.CreateDocument method discards any existing document and creates a new one. This in itself is not a valid XML docuemnt. To form the smallest legal XML document a root element needs to be added.
 

mjcoon

Well-Known Member
Licensed User
In version 2.5 now posted...

Thank you, Andrew, that is amazingly responsive of you.

I'll just mention the IDE-hints for the properties of (all?) the objects as having annotation of [O] where is obviously intended as a to-do item for the next release.

Speaking of the next release, could the XmlNode object have a "Clone" method? I see in XmlNode Members (System.Xml) (which I am guessing is the underlying .NET object though I don't understand the relationship between System.Xml and .NET) has a Clone method.

This is because my program is generating KML files with multiple Placemarks with the same structure and cloning would be a convenient and, I assume, efficient way of creating them. I am currently using the XML text property to do this replication and having some problem that I do not yet understand. Not having to do this serialisation and parsing would, I imagine, be much mmore efficient and reliable.

Mike.
 
Top