matlab.io.xml.dom.EntityResolver Class
Namespace: matlab.io.xml.dom
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
Public Methods
resolveEntity |
The |
Examples
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.
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; p.Configuration.AllowDoctype = true;
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]
Version History
Introduced in R2021a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)