Cookbook: Difference between revisions

49 bytes added ,  12 March 2014
Line 123: Line 123:
Reading XML in octave can be achieved using the java library [http://xerces.apache.org/ Xerces] (from apache).  
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.
It seems that the matlab's xmlread is just a thin wrapper around the Xerces library which is also included in matlab. One should note however, 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, as java does not provide a way to change the current working directory (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)
You need the jar file xercesImpl.jar and xml-apis.jar 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
Use javaaddpath to include these files:


{{Code|Load comma separated values files|<syntaxhighlight lang="octave" style="font-size:13px">
{{Code|Load comma separated values files|<syntaxhighlight lang="octave" style="font-size:13px">
Line 132: Line 132:
   javaaddpath('/path/to/xerces-2_11_0/xml-apis.jar');
   javaaddpath('/path/to/xerces-2_11_0/xml-apis.jar');
</syntaxhighlight>}}
</syntaxhighlight>}}


A sample script:
A sample script:
Line 139: Line 138:
  filename = 'sample.xml';
  filename = 'sample.xml';
   
   
  % These 3 lines are equivalent to xDoc = xmlread(filename)
  % These 3 lines are equivalent to xDoc = xmlread(filename) in matlab
  parser = javaObject('org.apache.xerces.parsers.DOMParser');
  parser = javaObject('org.apache.xerces.parsers.DOMParser');
  parser.parse(filename); % it seems that cd in octave are taken into account
  parser.parse(filename);  
  xDoc = parser.getDocument;
  xDoc = parser.getDocument;
   
   
4

edits