## Copyright (C) 2015 Pantxo Diribarne
## 
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {Function File} {@var{node} =} xmlread (@var{fname})
##
## Parse an xml file @var{fname} using Xerces java library and return
## a java object representing an xml DOM document.
##
## Octave does not ship with a Xerces library so you should take care
## of adding the required .jar files to your java_path, e.g:
## 
## @example
## @code{javaaddpath ("/path/to/xerces-2_11_0/xercesImpl.jar");}
## @code{javaaddpath ("/path/to/xerces-2_11_0/xml-apis.jar");}
## @end example
## @seealso{xmlwrite}
## @end deftypefn

function node = xmlread (fname)
  if (nargin < 1 || ! ischar (fname))
    print_usage ()
  endif
  
  try
    parser = javaObject ('org.apache.xerces.parsers.DOMParser');
  catch
    disp (lasterr ());
    error ("xmlread: couldn't load Xerces parser object, you may \
have to add required .jar files to your javapath")
  end_try_catch
  
  parser.parse (fname); 
  node = parser.getDocument;
endfunction


%!demo
%! ## Create an svg file, which is nothing but an xml tree
%! tk = graphics_toolkit ();
%! graphics_toolkit ("fltk");
%! hf = figure ();
%! sombrero ();
%! fname = [tempname(), ".svg"];
%! print (fname);
%! close (hf)
%! graphics_toolkit (tk);
%! 
%! ## Read the svg file and check the root node is named "svg"
%! dom = xmlread (fname);
%! if (dom.hasChildNodes ())
%!   root_node = dom.getChildNodes ().item (0);
%!   printf ("The name of the root node is \"%s\"\n", ...
%!            char (root_node.getNodeName ()))
%! endif
