Wednesday, December 22, 2004

Creating XML outputs with MATLAB

If you want to format your data output as an XML file, here's how to do it without doing any complicated string handling. MATLAB can use the XML handling capabilities of Java very easily. Here's how:

Assume that your data is available in arrays arrData1 and arrData2. Create an XML document node, say Node1 as follows:

docNode = com.mathworks.xml.XMLUtils.createDocument('Node1');
docRootNode = docNode.getDocumentElement;


Now put the data in the data nodes..

for i=1:length(arrData1),
% create nodes..
    elPar = docNode.createElement('DataParent');
    elData1 = docNode.createElement('Data1');
    elData2 = docNode.createElement('Data2');

% put data in nodes..
    elData1.appendChild(...
docNode.createTextNode(sprintf('%f', arrData1(i))));
    elData2.appendChild(...
docNode.createTextNode(sprintf('%f', arrData2(i))));

% put nodes in the correct positions..
    elParent.appendChild(elData1);
    elParent.appendChild(elData2);
    docRootNode.appendChild(elParent);
end

Now save the XML document. You can save it directly, or use uiputfile to get the standard save dialog of the OS.

[filename, pathname] = uiputfile(...
sprintf('%s.xml', datFile), 'Save XML file as');

% Save the XML document.
% xmlFileName = [dataOutFile,'.xml'];
    xmlFileName = fullfile(pathname, filename);
    xmlwrite(xmlFileName,docNode);
% open xml file to see your output
    edit(xmlFileName);

To see more ways of using XML with MATLAB, refer to Java's documentation and play around to see what works.

10 comments:

Anonymous said...

where from "elParent" will come?

Anonymous said...

is the DOM functionallity level exposed by MATLAB dependant on the version of JAXP being used by MATLAB?

Anonymous said...

how to add attributes in headers like
marketRequest marketDataRef="md1" marketDataXpathRef="/Rosetta/market/marketData" /

&

- Rosetta version="3.3"

Anonymous said...

Nice, thanks.

Only instead:
% create nodes..
elPar = docNode.createElement('DataParent');

should be
% create nodes..
elParent = docNode.createElement('DataParent');


and instead:

[filename, pathname] = uiputfile(...
sprintf('%s.xml', datFile), 'Save XML file as');

should be

[filename, pathname] = uiputfile(...
sprintf('%s.xml', 'datFile'), 'Save XML file as');

Glavx said...

Hello, I'm having a problem in the program that I cant solve.
I'm getting that: "??? Index exceeds matrix dimensions."

Do you know how can I solve that? I'm trying to export the data of 25 vectors, 9 lines each.

- Leonardo

Anil said...

Glavina,

post your question on the forum and see if you get any responses.

www.mathroot.com


Anil

Anil said...

Link to forum:
http://www.mathroot.com

Glavx said...

I posted there, but no answer yet.
I was trying to do in another way, creating multiple nodes, but I got the same error. =/

- Leonardo

Anonymous said...

Good, but i dont know how to get my data back.

i created
arrData1=[ 1 2 3 4 5];
arrData2=[ 2 4 6 8 10];

and got an output xml file.
but i cant get the data back into workspace. How to do that.
please help. thanks

mnikoli said...

I have a problem with inserting a xml-document in an existing document.

with 'xmlread' I read a xml-file and then i create a file like shown in this example before wirth 'docNode=....'

How to insert this docNode to an existing xml-file???

I tried with insertBefore(docNode, node from existing file);
or with

docNode = node.createElement('CHAPTER');

%--------------------------------------------------------------------------
%----------------- OPEN EXISTING XML-TEMPLATE TO READ ---------------------
%--------------------------------------------------------------------------

docu = 'file.xml';
xmlDocu = xmlread(docu);

%--------------------------------------------------------------------------
%---- SEARCH IN XML-TEMPLATE, WHERE THE SECTION 'SW-TEST-CASES' BEGINS ----
%--------------------------------------------------------------------------

%------- DETERMINE ALL CHAPTER-ELEMENTS --------
allListItems = xmlDocu.getElementsByTagName('CHAPTER');
node=allListItems.item(22);


%-----search the section with the Text Content 'SW-Test'-----
%allListItems.getTextContent('SW-Test')

%--------------------------------------------------------------------------
%------------- REMOVE THE CURRENT SECTION 'SW-TEST-CASES' -----------------
%--------------------------------------------------------------------------


if(node.hasChildNodes())
while(node.getLength >0)
%node.insertBefore(docNode, node.item(0));
node.removeChild(node.item(0));
end
end


%--------------------------------------------------------------------------
% --------------- CREATE THE NEW SECTION 'SW-TEST-CASES' ------------------
%--------------------------------------------------------------------------

docNode = node.createElement('CHAPTER');
%docNode = com.mathworks.xml.XMLUtils.createDocument('CHAPTER');
%docRootNode = docNode.getDocumentElement;


LongName = docNode.createElement('LONG-NAME');
LMV = docNode.createElement('L-4');

LongName.appendChild(LMV);
LMV.appendChild(docNode.createTextNode(sprintf('Testcases')));
docRootNode.appendChild(LongName);


%--------------------------------------------------------------------------
%--------------------- SAVE THE EDITED XML DOCUMENT -----------------------
%--------------------------------------------------------------------------

xmlFileName = [tempname,'.xml'];
xmlwrite(xmlFileName,xmlDocu);



edit(xmlFileName);




But no reaction.
Who can help?

Moving to a new website

This website was intended to be a little collection of tips and tricks, but I haven't been able to update it as often as I originally in...