MATLAB Community

MATLAB, community & more

Using XML in MATLAB 22

Posted by Michael Katz,

Much of the data on the Internet is stored in some flavor of XML. Fortunately for us, MATLAB has some built in functions for handling XML file. This will be the first in a series of non-consecutive posts about working with XML in MATLAB. Today I'm going to describe the functions for reading, writing, and transforming XML files.

There are three functions in MATLAB to specifically deal with XML files. The first is xmlread. This function takes either a URL or a filename and creates a Java XML object in the workspace:

xmlfile = fullfile(matlabroot, 'toolbox/matlab/general/info.xml');
xDoc = xmlread(xmlfile)
 
xDoc =
 
[#document: null]
 

 

Don't worry that the return value says: "[#document: null]". The xmlread function returns a Java object that represents the file's Document Object Model, or DOM. The "null" is simply what the org.apache.xerces.dom.DeferredDocumentImpl's implementation of toString() dumps to the MATLAB Command Window. To learn more about interacting with Java objects in MATLAB, see my previous article.

Just to make sure this object has all our XML text, let's use the next MATLAB XML function: xmlwrite. Without any additional arguments, xmlwrite will display the contents of the DOM in the Command Window:

xmlwrite(xDoc)
ans =

<?xml version="1.0" encoding="utf-8"?>
<productinfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.mathworks.com/namespace/info/v1/info.xsd">
<?xml-stylesheet type="text/xsl" href="http://www.mathworks.com/namespace/info/v1/info.xsl"?>

   <matlabrelease>14</matlabrelease>
   <name>MATLAB</name>
   <type>matlab</type>
   <icon>ApplicationIcon.MATLAB</icon>
   <help_location>$docroot/techdoc</help_location>
   <dialogpref_registrar>   
      <source>com.mathworks.mde.editor.EditorOptions</source>
   </dialogpref_registrar>
      <!-- cut for brevity..... -->
   </list>
</productinfo>

 

Of course, you can also use xmlwrite to save the XML document to disk, by calling xmlwrite with the following signature:

xmlwrite(outfile,xDoc)

One gotcha when working with xmlread is that because the created DOM object is a Java object, it is stored in Java memory. The amount of memory set aside for Java is system dependent, but is generally between 64 and 256 MB. This means that if you read in a 200MB XML file, you're going to run out of memory, no matter how much free main memory MATLAB says is available. In that case, you just need to adjust the amount of memory available to Java, using the new preference panel.

The final XML function provided by MATLAB deals with transforming XML documents with XSL Stylesheets. The xslt function takes a DOM object or a filename or url specifying an XML document, a filename or url specifying an XSL stylesheet, and the output destination and performs the transform. Working with stylesheets is pretty complicated so I'm not going to delve into them here.

Next time, I'll talk about the DOM object itself and how to add, remove, and query nodes.

Note

Comments are closed.

22 CommentsOldest to Newest

StephenLL replied on : 1 of 22

One of the first tasks I wanted to do with the xmlread function was to read in the XML from a string. It wasn’t obvious to me. After some research I put together the following code. I’m not sure if it is the best or the ideal but it works.

import org.xml.sax.InputSource
import javax.xml.parsers.*
import java.io.*

% str is a character string of XML you want to read in.

iS = InputSource();
iS.setCharacterStream( StringReader(str) );

p = xmlread(iS);

Yair Altman replied on : 2 of 22

A useful reference is the official javadoc for the XML functionality: ht​tps://jaxp-sources.dev.java.net/nonav/docs/api/
There are also numerous online resources explaining how to use the XML functionality and its many alternative libraries.

Java-savvy programmers may be interested in the following related article: http://UndocumentedMatlab.com/blog/undocumented-xml-functionality/
(note that it relies on unsupported functionality which may change without warning across Matlab releases).

Mike replied on : 3 of 22

@StephenLL,

That is one good way to do it, thanks for sharing.

@Yair,
Thanks for the reference and for exploring the XML function in your blog. I’m going to have a follow up post describing some the basics of working with XML node objects, but of course, I won’t be able to detail everything.

Simon replied on : 4 of 22

Hello Mike,

When is the next blog about the DOM object itself and how to add, remove, and query nodes?

Thank you in advance,

Simon

Mike replied on : 5 of 22

@Simon,

I don’t have a specific time set yet, but probably in the next two – three weeks.

Arindam replied on : 6 of 22

Hi Mike,

Does the Java object also take care of validation of the xml file as per the xsd specs?

Thanks

-Arindam

Richie replied on : 7 of 22

Hey Mike,

when will be the next update with adding and querying nodes?

I put myself something together by reading many different threads online, but I haven’t found anything yet describing the function of the commands. So it’s really hard to change things, when you don’t know what it -really- does.

Greetz,
Richie

vishnusekhar replied on : 9 of 22

Hello Mike,

This is very userfull post.

How we can pass the XML object as parameter to the stored procedure?

Thanks,
Vishnu

Mike replied on : 10 of 22

@Vishnu,

I don’t understand the question. You can create a reference to the Java XML object with xmlread, as described above. You can then use resulting variable like any other MATLAB variable.

vishnusekhar replied on : 11 of 22

Mike,
I was tryiing to calla procedure from matlab and pass the xml dom object as a parameter.
I had declared the input argument as xml type in stored procedure.
Here I am copying the code for your reference:
matlab Code
xmlContent=xmlread(‘tmpfile_11_04_2010.xml’)
x=xmlwrite(xmlContent)
sqlOutput = fetch(conn,strcat(‘{call sp_test (‘, {x},’)’)

also I tried
K>> sqlOutput = runstoredprocedure(conn,’stp_RM_INS_RiskMatlabOutput’, {x})
??? Error using ==> database.runstoredprocedure at 86
Procedure may return resultset. Use EXEC and FETCH

sql code

Alter proc sp_test(@p_xmlObject varchar(3000))As
begin
select ‘Sucess’
end

I am getting error from matlab. Could you please let me know the syntax for passing xml object to sql ?

Thanks in advance.
Vishnu

vishnusekhar replied on : 12 of 22

sorry, please find the code for the stored procedure

Alter proc sp_test(@p_xmlObject xml)As
begin
select ‘Sucess’
end

Mike replied on : 13 of 22

@Vishnu,

I understand, now. There may be an escape character issue, either with the single-quotes (‘) that may be in the XML file that aren’t concatenating well, or perhaps with the > and < or & symbols in your xml document. You should contact technical support and they can help you track down the issue and show you how to fix it.

vishnusekhar replied on : 14 of 22

Hello Mike,

Exactly. I had a discussion with the support team.

xmlContent=xmlread(xmlFileName);

xmlObject=xmlwrite(xmlContent) ;

conn = database(strDatabaseName,strUserName,strPassword,strDriverName,strcat(strURL,strDatabaseName));

K>> sqlOutput = runstoredprocedure(conn,’sp_test’,{”’, xmlObject, ”’ },{java.sql.Types.sqlXml})
??? No appropriate method, property, or field sqlXml for class java.sql.Types.

let me know how I can use java.sql.Types.sqlXml ?

Thanks,
Vishnu

vishnusekhar replied on : 15 of 22

we are resolved this issue using the below code …

xmlContent=xmlread(xmlFileName);
xmlObject=xmlwrite(xmlContent) ;
conn = database(strDatabaseName,strUserName,strPassword,strDriverName,strcat(strURL,strDatabaseName));
sqlOutput=fetch(conn, strcat(‘{call sp_test(”’, xmlObject, ”’)}’));

Removed the single quote from the xml. This is working fine.

Thanks
Vishnu

Mike replied on : 18 of 22

@Beverlyn,

By design, XMLWRITE uses our default encoding which is UTF-8 when writing the file, regardless of what you might set in the DOM. To work around this issue, you have to modify your XMLWRITE function in the MATLAB toolbox. I recommend saving a backup of it first.

edit('xmlwrite')

Then modify the line:

javaMethod('serializeXML',...
    'com.mathworks.xml.XMLUtils',...
    source,result);

to:

javaMethod('serializeXML',...
    'com.mathworks.xml.XMLUtils',...
    source,result,'ISO-8859-1');

if you wanted to go further you can modify the xmlwrite.m file to take the encoding a parameter. I’ve created an enhancement request to have that added to MATLAB.

Michael Katz replied on : 22 of 22

@Abby,

I have answered that question. Hope that helps. I’ll try to cover that in a future post.