Cookbook: Difference between revisions

Jump to navigation Jump to search
1,690 bytes added ,  12 March 2014
No edit summary
Line 117: Line 117:


Another option is to use the function <code>csvread</code>, however this function can't handle non-numerical data.
Another option is to use the function <code>csvread</code>, however this function can't handle non-numerical data.
===Load XML files===
Reading XML in octave can be achieved using the java library [http://xerces.apache.org/ 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.
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
{{Code|Load comma separated values files|<syntaxhighlight lang="octave" style="font-size:13px">
  javaaddpath('/path/to/xerces-2_11_0/xercesImpl.jar');
  javaaddpath('/path/to/xerces-2_11_0/xml-apis.jar');
</syntaxhighlight>}}
A sample script:
{{Code|Load comma separated values files|<syntaxhighlight lang="octave" style="font-size:13px">
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')
</syntaxhighlight>}}
The file <tt>sample.xml</tt>:
<root>
<data att="1">hello</data>
</root>


===Using variable strings in commands===
===Using variable strings in commands===
4

edits

Navigation menu