%% Resolve an XML Entity
% This example creates an entity resolver, configures a parser to use the resolver, and parses an XML file that includes an entity reference.
% The example uses these files:
% chapter.xml contains markup for a chapter.
% <?xml version="1.0" encoding="UTF-8"?>
% <chapter><title color="red">Introduction</title></chapter>
% book.xml contains the entity reference &chapter; and declares that the resource for the entity is chapter.xml.
% <?xml version="1.0" encoding="UTF-8"?>
% <!DOCTYPE book [
% <!ENTITY chapter SYSTEM "chapter.xml">
% ]>
% <book>
%     &chapter;
% </book>
% BookEntityResolver is a subclass of the abstract class matlab.io.xml.dom.EntityResolver.
% classdef BookEntityResolver < matlab.io.xml.dom.EntityResolver
%
%     properties
%         BaseDir
%     end
%
%     methods
%
%         function obj =  BookEntityResolver(baseDir)
%             obj@matlab.io.xml.dom.EntityResolver()
%             obj.BaseDir = baseDir;
%         end
%
%         function res = resolveEntity(obj,ri)
%             import matlab.io.xml.dom.ResourceIdentifierType
%             if getResourceIdentifierType(ri) == ResourceIdentifierType.ExternalEntity
%                 res = fullfile(obj.BaseDir, ri.SystemID);
%             end
%         end
%     end
%
% end
% Create an entity resolver as an instance of the BookEntityResolver class.

import matlab.io.xml.dom.*

resolver = BookEntityResolver(pwd);

% Create a parser and configure it to use the resolver.

p = Parser();
p.Configuration.EntityResolver = resolver;

% Parse the file book.xml into a matlab.io.xml.dom.Document object.

domDoc = parseFile(p,"book.xml");

% To see that the chapter entity was resolved, find the chapter element node in the document.

nl = getElementsByTagName(domDoc,"chapter");
ch = node(nl,1)

%
% Copyright 2020 The MathWorks, Inc.