xml_parseany

Parses an XML string with attributes and returns corresponding Matlab structure.

 

Syntax

v = xml_parseany(xmlstr)

 

Description

Parses XML string xmlstr and returns the corresponding Matlab structure, v. In comparison with xml_parse, this command reads all XML element attributes and returns these in additional attribute fields, thus enabling the user to read most types of XML into a Matlab variable.

This is a non-validating parser. XML entries starting with the exclamation mark tag "<!" and "<?" are ignored by the parser.

Any substructure is returned as a cell data type in Matlab as the parser assumes that child elements can contain any kind of complex XML element.

 

Input Arguments

xmlstr        XML string, for example read from file with
xmlstr = fileread(filename)

 

Output Arguments

v                   Matlab variable or structure with field .ATTRIBUTE if XML element attributes are present.

 

Examples

In this example, we specify an XML string and look at the difference between the xml_parse and xml_parseany functions:

 

xmlstr = ...

'<root idx="1" type="double" size="1 2">3.1416 1.4142</root>';

v1 = xml_parse(xmlstr);

v1:  [3.1416, 1.4142]  % (class double)

 

v2 = xml_parseany(xmlstr);

 

v1.ATTRIBUTE.idx = '1'

v1.ATTRIBUTE.type = 'double'

v1.ATTRIBUTE.size = '1 2'

v1.CONTENT = '3.1416 1.4142'

 

We see that the xml_parse command uses the specific attributes to convert the content into the corresponding Matlab data types. The function xml_parseany, however, returns all attributes in a substructure called ATTRIBUTE and the content in a field called CONTENT. xml_parseany does not use the attributes for type conversions to Matlab data types as these may not have originated from the XML Toolbox.

 

For more generic XML, the xml_parseany command acts as follows:

 

xmlstr = ...

      '<root color="red" language="en">

         <project id="alpha">

           <name>Project_Alpha</name>

           <author>Arthur</author>

           <link location="url">http://www.com/a</link>

         </project>

         <project id="beta">

           <name>Project_Beta</name>

           <author>Ben</author>

           <link location="file">c:\temp\b.pro</link>

         </project>

      </root>';

 

 

 

v = xml_parseany(xmlstr)

 

v =

      project: {[1x1 struct]  [1x1 struct]}

 

    ATTRIBUTE: [1x1 struct]

 

v.ATTRIBUTE

 

ans =

       color: 'red'

 

    language: 'en'

 

 

 

v.project{1}

 

ans =

         name: {[1x1 struct]}

       author: {[1x1 struct]}

         link: {[1x1 struct]}

    ATTRIBUTE: [1x1 struct]

 

v.project{2}.name{1}

 

ans =

    ATTRIBUTE: [0x0 struct]

      CONTENT: 'Project_Beta'

 

v.project{2}.link{1}

 

ans =

    ATTRIBUTE: [1x1 struct]

      CONTENT: 'c:\temp\b.pro'

 

v.project{2}.link{1}.ATTRIBUTE

 

ans =

    location: 'file'

 

Note

All subfields of the returned data structure are Matlab cell data types and therefore indexed with curly braces {.}. This adds a bit more complexity for the developer if the level of nesting is high; however, it also means that XML documents are returned to Matlab in a well-defined state.

 

Namespaces & valid Matlab variable names:

If an XML element has a namespace attached, for example "soap:services", the "soap" namespace is transferred into a subfield of the ATTRIBUTE structure, called "NAMESPACE". This is done to ensure that the name corresponds to a valid Matlab variable name. For the same reasons are any hyphens, "-" replaced by the underscore "_" during the parsing operation.

 

See also

xml_formatany, xml_format, xml_parse , xml_load, xml_save, xml_help

 



xml_parse

contents

xml_save

Copyright © 2007, The Geodise Project, University of Southampton