-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlreadstring.m
63 lines (50 loc) · 1.7 KB
/
xmlreadstring.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function [parseResult,p] = xmlreadstring(stringToParse,varargin)
%XMLREADSTRING Modified XMLREAD function to read XML data from a string.
% Author: Luis Cantero.
% The MathWorks.
p = locGetParser(varargin);
locSetEntityResolver(p,varargin);
locSetErrorHandler(p,varargin);
% Parse and return.
parseStringBuffer = java.io.StringBufferInputStream(stringToParse);
parseResult = p.parse(parseStringBuffer);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function p = locGetParser(args)
p = [];
for i=1:length(args)
if isa(args{i},'javax.xml.parsers.DocumentBuilderFactory')
javaMethod('setValidating',args{i},locIsValidating(args));
p = javaMethod('newDocumentBuilder',args{i});
break;
elseif isa(args{i},'javax.xml.parsers.DocumentBuilder')
p = args{i};
break;
end
end
if isempty(p)
parserFactory = javaMethod('newInstance',...
'javax.xml.parsers.DocumentBuilderFactory');
javaMethod('setValidating',parserFactory,locIsValidating(args));
%javaMethod('setIgnoringElementContentWhitespace',parserFactory,1);
%ignorable whitespace requires a validating parser and a content model
p = javaMethod('newDocumentBuilder',parserFactory);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function tf=locIsValidating(args)
tf=any(strcmp(args,'-validating'));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function locSetEntityResolver(p,args)
for i=1:length(args)
if isa(args{i},'org.xml.sax.EntityResolver')
p.setEntityResolver(args{i});
break;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function locSetErrorHandler(p,args)
for i=1:length(args)
if isa(args{i},'org.xml.sax.ErrorHandler')
p.setErrorHandler(args{i});
break;
end
end