%% Access Document Type and Entities in Parsed Document
% This example shows how to access document type and entity information in a DOM document that was parsed from XML markup that contains a document type definition (DTD).
% The example uses these files, which must all be in the same folder:
% book.xml contains a document type definition that declares that the resource for the chapter entity is chapter.xml.
% <?xml version="1.0" encoding="UTF-8"?>
% <!DOCTYPE book [
% <!ENTITY chapter SYSTEM "chapter.xml">
% ]>
% <book>
%     &chapter;
% </book>
% chapter.xml contains markup for a chapter.
% <?xml version="1.0" encoding="UTF-8"?>
% <chapter><title color="red">Introduction</title></chapter>
% Parse the XML into a matlab.io.xml.dom.Document object.

import matlab.io.xml.dom.*

doc = parseFile(Parser,'book.xml');

% To get information about the document type, use the getDoctype method of the Document object.

docTypeObj = getDoctype(doc)

%  To get information about the entities defined by the document type, use the getEntities method of the DocumentType object. The method returns a list of the entities as a NamedNodeMap object.

namedNodeMapObj = getEntities(docTypeObj);

% To return the matlab.io.xm.dom.Entity objects that represent the entities, use the item method of the NamedNodeMap object. Specify the first index as 0.

n = getLength(namedNodeMapObj)-1;
for i=0:n
    item(namedNodeMapObj,i)
end

% Copyright 2020 The MathWorks, Inc.