xml_format

Converts a Matlab variable into an XML string.

 

Syntax

xmlstr = xml_format(v)

xmlstr = xml_format(v,attswitch)

xmlstr = xml_format(v,attswitch,name)

 

Description

xml_format converts Matlab variables and data structures (including deeply nested structures) into XML and returns the XML as string.

 

Input Arguments

v                   Matlab variable of type "struct", "char", "double"(numeric), "complex", "sparse", "cell", or "logical"(boolean).

attswitch optional, default='on':
'on' writes header attributes idx, size, type for identification by Matlab when parsing the XML later;
'off' writes "plain" XML without header attributes.

name            optional, give root element a specific name, eg. 'project'.

 

Output Arguments

xmlstr        string, containing XML description of the variable v.

 

The root element of the created XML string is called 'root' by default but this can be overwritten with the name input parameter. A default xml_tb_version attribute is added to the root element unless attswitch is set to 'off'.

 

If attswitch is left empty, [], or set to 'on', the default attributes idx, type, and size will be added to the XML element headers. This allows xml_parse to parse and convert the XML string correctly back into the original Matlab variable or data structure.

If attswitch is set to 'off', some of the information is lost and subsequently the contents of XML elements will be read in as strings when converting back using xml_parse.

 

Examples

 

This example shows how to convert a simple number into an XML string. Note that we could have used xml_format(5) instead.

v = 5;

xmlstr = xml_format(v)

 

xmlstr =

<root xml_tb_version="3.0" idx="1" type="double"
 size="1 1">5</root>

 

We can tell the command to ignore all the attributes and obtain the following XML:

xmlstr = xml_format(v,'off')

 

xmlstr =

<root>5</root>

 

The root elements can be assigned a different name by adding this as third parameter to the xml_format function:

xmlstr = xml_format(v,'off','myXmlNumber')

 

xmlstr =

<myXmlNumber>5</myXmlNumber>

 

This example shows how pre-defined Matlab data (here pi) is translated into XML. The number of decimals stored is the number required to reconstruct the exact same variable in Matlab from XML with the xml_parse function.

 

v = pi;

xmlstr = xml_format(v,[],'pi')

 

 

xmlstr =

<pi xml_tb_version="3.0" idx="1" type="double" size="1 1">
3.141592653589793</pi>

 

Character arrays or strings can also be converted into XML:

 

v = 'The Hitchhikers Guide to the Galaxy';

xmlstr = xml_format(v);

 

xmlstr =

<root xml_tb_version="3.0" idx="1" type="char" size="1 35">
The Hitchhikers Guide to the Galaxy</root>

 

One of the most powerful ways to use the XML Toolbox is to convert whole data structures (with substructures) which can contain any Matlab data type.

 

v.project.name = 'my Project no. 001';

v.project.date = datestr(now,31);

v.project.uid  = '208d0174-a752-f391-faf2-45bc397';

v.comment = 'This is a new project';

 

xmlstr = xml_format(v,'off');

 

xmlstr =
<root>

  <project>

    <name>my Project no. 001</name>

    <date>2004-09-09 16:18:29</date>

    <uid>208d0174-a752-f391-faf2-45bc397</uid>

  </project>

  <comment>This is a new project</comment>

</root>

Notes

If different attributes are required in the output string, please see description for xml_formatany.

 

See also

xml_parseany, xml_formatany, xml_parse , xml_load, xml_save, xml_help



xmltoolbox

contents

xml_formatany

Copyright © 2007, The Geodise Project, University of Southampton