Jump to: navigation, search

Reading XML in octave can be achieved using the java library Xerces (from apache).

It seems that the matlab's xmlread is just a thin wrapper around the Xerces library which is also included in matlab. One odd thing however, is that java functions have the working directory set to the working directory when octave starts and the working directory is not modified by a cd in octave. Matlab has the same behavior maybe due to http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4045688. To avoid any issues it is thus better to use the absolute path to the xml file. The absolute path can be obtained using which:

filename = which(filename);

You need these jar files from e.g. https://www.apache.org/dist/xerces/j/Xerces-J-bin.2.11.0.tar.gz (check for the latest version) Use javaaddpath to include these files


A sample script:

filename = 'sample.xml';

% These 3 lines are equivalent to xDoc = xmlread(filename)
parser = javaObject('org.apache.xerces.parsers.DOMParser');
parser.parse(filename); % it seems that cd in octave are taken into account
xDoc = parser.getDocument;

% get first data element 
elem = xDoc.getElementsByTagName('data').item(0);
% get text from child
data = elem.getFirstChild.getTextContent
% get attribute named att
att = elem.getAttribute('att')

The file sample.xml:

<data att="1">hello</data>
Personal tools