次の方法で共有


Serializing Axapta Foundation Class instances as XML streams

The Axapta foundation classes are a set of generally applicable classes that contain values of either simple types or other classes. They are:

  • Structs, structures of named fields,
  • Arrays, arrays of any type, not just simole type
  • Lists, lists allowing traversal with enumerators
  • Maps, dictionaries allowing lookup of values, yielding a result value
  • Sets, ordered groups where duplicates cannot occur.

The classes are generally well understood, so this is not going to be a tutorial on how to use them. This blog will deal with the infrastructure provided to allow serializing and deserializing values to XML streams, that can then be processed (searched with XPath queries, persisted in files or databases etc) as any other XML document.

 

In the example below we will be describing how this works for structs, but the same principles apply to the other foundation classes as well.

The struct class has a method that yields an XML representation of its content. Not suprisingly, this method is called Xml(). As you can imagine, this method can easily deal with instances of simple types, but structs are not limited to containing simple types: They can contain instances of any type defined by the user. Ideally we would like these classes to be able to participate in the serialization and subsequent deserialization. As it happens this is exactly what the infrastructure provides, with a little help from the developer.

 

When the xml method is called on the struct, the implementation will call xml (int indent) on any objects in the struct. It is then up to this implementation to serialize the state of the object into XML. Let's consider an example where a struct contains a user defined Point class, with the obvious set of x,y values. The class, listed below contains an XML method that returns a string containing the XML format of the state of the object. The indent parameter indicates the indentation level; padding with spaces as done in the example below is not really needed: There are no semantics for white space in XML, but it makes the XML document look good in the eyes of carbon based lifeforms.

 

class Point
{
int x;
int y;

public void new(int _x, int _y)
{
x = _x; y = _y;
}

public str ToString()
{
return strfmt("Point(%1,%2)", x,y);
}

public str xml(int indent=0)
{
return strrep(" ", indent) + strfmt("<Point x='%1' y='%2' />", x,y);
}
}

 

If you use string operations like above, you should be careful to not include the XML metacharacters <,>,&,'," in the values, You should use the &lt;, &gt; &amp; &apos; and &quot; placeholders instead. You can use .NET to build the XML stream for you (it will escape the characters for you) or place content inside [CDATA[]] tags. For the example above this would clearly be overkill.

 

With this in place, you can serialize the content of a struct into an XML stream and work with it as an XML document:

 

static void structserializetest(Args _args)
{
struct s = new Struct ("str name; int age; Point p");
XmlDocument d = new XmlDocument();
str xml;

    s.value("Name", "John Doe");
s.value("Age", 42);
s.value("Point", new Point(43, 77));

    xml = s.xml();

// Load the XML we just serialized into an XML DOM

    d.loadXml(xml);

}

 

But that's not all. If you add a

 

static public CreateFromXml(XmlNode n)
{
return new Point(
str2int(n.attributes().getNamedItem("x").value()),
str2int(n.attributes().getNamedItem("y").value()));
}

 

method to the class you will be able to create the point instance from deserializing the XML you just created:

 

// … continued from example above…

// Load the XML wejust serialized into an XML DOM

d.loadXml(xml);

copy = struct::createFromXML(d.documentElement()); // Serialize

 

// Show the results. Should match the original value.

print copy.definitionString();

print copy.toString();

Comments