XML Deep Dive

To further that - I have tried it and it seems to work. I just want to make sure I’m not stepping on a mine.

I do that (reuse the node variable) regularly and haven’t had an issue.

[quote=67502:@Tim Jones]Since the nodes are being added as elements of “root”, is it safe to reuse the node variable as your code reuses the value node?

i.e.:

[code] Dim root As XmlNode
root = theXML.AppendChild(theXML.CreateElement(“Production”))

Dim theNode As XmlNode
Dim value As XmlNode

theNode = root.AppendChild(theXML.CreateElement(“ID”))
value = theNode.AppendChild(theXML.CreateTextNode(“092-1433212”))

theNode = root.AppendChild(theXML.CreateElement(“Name”))
value = theNode.AppendChild(theXML.CreateTextNode(“A DJ’s Life”))
[/code][/quote]
Yes. You can reuse the variable. It’s no different than reusing any other object reference. After AppendChild, the document has a reference to the original object, so it’s safe to drop your reference (by assigning a new value to theNode).

One final question for this conversation -

When dealing with binary data, I believe that a CDATA segment is required. However, when I create a new XmlCDATASection and then assign the backdrop of a canvas to it using theXML.CreateCDATASection(theCanvas.BackDrop), I get a syntax error indicating that the parameters are not compatible with the function. I was under the impression that a CDATA section could deal with any type of data by converting it to BASE64.

What and I missing?

Thanks,
Tim

Two things. First, the section name needs to be a string:
cdatanode = theXML.CreateCDATASection(“sectionname”)

And second, I THINK that the data needs to already be a string, so you’d do:
cdatanode.value = (some way of turning the backdrop into a string)

Of course, turning that picture into a string is the tricky part but perhaps somebody has done that on the forums already?

I haven’t tested it and it’s from 2005, but look at this site for “take a picture to and from a string”.

http://www.great-white-software.com/Great_White_Software/REALbasic_Code.html

Backdrop is a Picture object, so (assuming it exists / is not nil)

cdatanode = theXML.CreateCDATASection("sectionname")
cdatanode.value = theCanvas.Backdrop.GetData

The documentation is a bit misleading

[quote]XMLDocument.CreateCDATASection ( Data as String ) As XMLCDATASection
Creates a CDATA section with the passed Data and returns it as an XMLCDATASection.[/quote]
Here, “Data” sounds like the stuff to put in the CDATA section, but it’s not.

Theres a Xippet on this for converting a picture to a string and back. https://www.boxedbyte.com/xippet_view.php?xippet_id=149

Edit: Nope, the docs are right, that is the data to put in the CDATA section. There is no name.

BUT, it doesn’t encode it for you.

cdatanode = theXML.CreateCDATASection(EncodeBase64(theCanvas.Backdrop.GetData))

[quote=67526:@Tim Hare]Edit: Nope, the docs are right, that is the data to put in the CDATA section. There is no name.

BUT, it doesn’t encode it for you.

cdatanode = theXML.CreateCDATASection(EncodeBase64(theCanvas.Backdrop.GetData))[/quote]

The example shows it differently, but I just tried it and you can put the data either in the CreateCDATASection(data) call as you are saying, or in the “value=data” bit like the docs show. They both end up being the same.

The example in the docs is what is misleading. @Paul Lefebvre, would you take a look at that?

I updated the example.

Perfect. Thank you!

It was the .GetData call that I was missing. Adding that to my original code did the trick. Thanks again!

It turns out that there are NO line breaks in the resulting file. That’s what was happening. The parser I was using on the Linux control system expects some form of line break between the nodes. It looks like I will be using the Transform on the file since I don’t control the parser used by the customers in this case (regardless of its in/correctness).

Actually, EncodeBase64() does that very concisely in this case.