2

I would like to read an XML file and modify some strings, save then close the file with MATLAB. So far I have:

f = fopen( 'output_results\results.xml', 'w' ); 

I need to add the following line inside the optList node of the file (see below):

<opt name="*_option1">true</opt>
<opt name="format">
    <f1>file:/C:/working/types.h</f1>
</opt>

save then close the file

fclose(f); 

How can I add the above lines in an XML file?

File content:

<?xml version="1.0" encoding="utf-8"?> 
<Custom_project name="" val="True" name="file1" path="file:/C:/Users/Local/Temp/info.xml" version="1.0">
<verif="true" name="values" path="file:/C:/Users/Temp/folder1">
    <optList name="values">
        <opt name="color">red</opt>
        <opt name="police">calibri</opt>
        <opt name="font">blue</opt>
    </optList>
</verif>
<toto="myvalue" name="option1">
    <opt name="myvalue_1">32</opt>
    <opt name="-total">All</opt> 
    <opt name="characteristic">hybrid</opt>
</toto> 
2
  • You are closing <opt> tags with </option>. I presume this is a typo? Commented Aug 16, 2012 at 9:40
  • One thing I'd watch out for is, at least for me, when I'd open and save an xml file MatLab would always add a bunch of white space in for me. But I didn't use fopen, I used xmlread. I had to write my own file to parse the xml file to remove the white space matlab added in for me. Commented Aug 16, 2012 at 13:17

2 Answers 2

1

As found here, it's not possible to open a file, seek a location, add stuff there, while preserving the rest of the text, and close.

You can work around this simply by re-writing the entire file:

f = fopen( 'output_results\results.xml', 'r' ); 
g = fopen( 'output_results\results.xml.TEMP', 'w' ); 

while ~feof(f)
    line = fgets(f);
    fprintf(g, '%s', line);
    if strcmpi(line, '<optList name="values">')
        fprintf(g, '%s\n%s\n%s\n%s\n',....
            '<opt name="*_option1">true</option>',...
            '<opt name="format">',...
            '<f1>file:/C:/working/types.h',...
            '</f1></option>');
    end
end

fclose(f), fclose(g);
movefile('output_results\results.xml.TEMP', 'output_results\results.xml');

If this is really a one-off problem, the hack above is OK. But, as suggested by @bdecaf, you should use the proper tool for the job. I'd suggest to do the writing entirely outside of MATLAB (to avoid overly complex code), and just call an external tool/library via MATLAB's system call syntax (type help !).

Sign up to request clarification or add additional context in comments.

Comments

1

In your example you never read the file.

But for XML you can save a lot of troubles if you use the java XML tools. You an call them directly from Matlab.

2 Comments

This is for using Java from Matlab: mathworks.de/help/techdoc/matlab_external/f44062.html - about XML and Java there are hundreds of nice tutorials found by search.
Agree with @bdecaf. Similar capabilities are available through .NET - Matlab supports that as well: mathworks.com/help/techdoc/matlab_external/brpb5k6.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.