Xstream

Xstream is a new streaming XML API for Navision I’ve been working on. It actually makes writing XML in Navision quite elegant.

To create this fragment

<persons>
   <person id=”BRA”>
      <name>
         <first>Bent</first>
         <last>Rasmussen</last>
      </name>
      <age>33</age>
   </person>
</persons>

This code will do

WITH xs DO BEGIN
   setup;
   “>”(‘persons’);
      “>”(‘person’); “@”(‘ID’,’BRA’);
         “>”(‘name’);
            “=”(‘first’,’Bent’);
            “=”(‘last’,’Rasmussen’);
         “<“;
     “>”(‘age’,33);
   save(‘c:\persons.xml’);
END;

There are a basic set of operators that are used for node construction

The “>” operator creates a new element, puts it into the context element and sets the new element as the context element.

The “<” operator sets the parent of the context element as the new context element.

The “=” operator creates a new element, adds a text node to it and adds the the new element to the context node.

The “@” operator creates an attribute node, assigns a value for it and adds the attribute node to the context element.

Now let’s look at the function definitions

“>”(k : Text[50])
{
   tmp := doc.createElement(k);
   ctx := ctx.appendChild(tmp);
}

“<“()
{
   ctx := ctx.parentNode;
}

“=”(k : Text[30], v : Variant) : Boolean
{
   IF NOT ref.XmlSchemaFormat(v,s) THEN
      EXIT(FALSE);
   tmp := doc.createElement(k);
   tmp.text := s;
   ctx.appendChild(tmp);
   EXIT(TRUE);
}

“@”(k : Text[30], v : Text[1024]) : Boolean
{
   IF NOT ref.XmlSchemaFormat(v,s) THEN
      EXIT(FALSE);
   attr := ctx.ownerDocument.createAttribute(k);
   attr.nodeValue := s;
   ctx.attributes.setNamedItem(attr);
   EXIT(TRUE);
}

That’s it.

Advertisements

About xosfaere

Software Developer
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s