Main Content

matlab.io.xml.dom.EntityResolver class

Package: matlab.io.xml.dom

Abstract base class for entity resolvers

Description

matlab.io.xml.dom.EntityResolver is an abstract base class for deriving entity resolvers that resolve entity references encountered by a parser while parsing an XML file or string.

The matlab.io.xml.dom.EntityResolver class is a handle class.

Class Attributes

Abstract
true
ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Methods

expand all

Examples

collapse all

This example creates an entity resolver, configures a parser to use the resolver, and parses an XML file that includes an entity reference.

If the path of a file that contains an entity is specified relative to the location of the main XML document, a parser can use a default entity resolver to resolve the path. In this case, you do not have to define your own resolver. To make sure that a resolver is required for this example, the example saves the file that contains the entity and the main XML file in different folders at the same level.

In a subfolder chapters of the current folder, create a file chapter.xml that contains this markup for a chapter:

<?xml version="1.0" encoding="UTF-8"?>
<chapter><title color="red">Introduction</title></chapter>

In a subfolder books that is at the same level as the chapters folder, create a file books.xml that contains this markup for a book:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book [
<!ENTITY chapter SYSTEM "chapters/chapter.xml">
]>
<book>
    &chapter;
</book>

book.xml contains the entity reference &chapter; and declares that the resource for the entity is chapters/chapter.xml.

Define a subclass of the abstract class matlab.io.xml.dom.EntityResolver and name it BookEntityResolver. Save BookEntityResolver.m in the folder that contains the chapters and books folder.

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.

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

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

nl = getElementsByTagName(domDoc,"chapter");
ch = node(nl,1)
ch = 
  Element with properties:

          TagName: 'chapter'
    HasAttributes: 0
      TextContent: 'Introduction'
         Children: [1×1 matlab.io.xml.dom.Element]

Introduced in R2021a