matlab.io.xml.dom.EntityResolver 类
包: matlab.io.xml.dom
描述
matlab.io.xml.dom.EntityResolver
是用于派生实体解码器的抽象基类,这些解码器对解析器在解析 XML 文件或字符串时遇到的实体引用进行解码。
matlab.io.xml.dom.EntityResolver
类是一个 handle
类。
方法
公共方法
resolveEntity |
|
示例
解析 XML 实体
此示例创建一个实体解码器,配置解析器来使用该解码器,并解析包含实体引用的 XML 文件。
如果包含实体的文件的路径是相对于主 XML 文档的位置指定的,解析器可以使用默认实体解析器来解析路径。在这种情况下,您不必定义自己的解析器。为了确保此示例需要解析器,示例将包含实体和主 XML 文件的文件保存在同一级别的不同文件夹中。
在当前文件夹的子文件夹 chapters
中,创建包含章节的以下标记的文件 chapter.xml
:
<?xml version="1.0" encoding="UTF-8"?> <chapter><title color="red">Introduction</title></chapter>
在与 chapters
文件夹处于同一级别的子文件夹 books
中,创建包含书籍的以下标记的文件 books.xml
:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE book [ <!ENTITY chapter SYSTEM "chapters/chapter.xml"> ]> <book> &chapter; </book>
book.xml
包含实体引用 &chapter;
,并声明该实体的资源为 chapters/chapter.xml
。
定义抽象类 matlab.io.xml.dom.EntityResolver
的子类,并将其命名为 BookEntityResolver
。将 BookEntityResolver.m
保存在包含 chapters
和 books
文件夹的文件夹中。
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
创建一个实体解析器作为 BookEntityResolver
类的实例。
import matlab.io.xml.dom.*
resolver = BookEntityResolver(pwd);
创建一个解析器,并将其配置为使用解码器。
p = Parser(); p.Configuration.EntityResolver = resolver; p.Configuration.AllowDoctype = true;
将文件 book.xml
解析到一个 matlab.io.xml.dom.Document
对象中。
filePath = "books/book.xml";
domDoc = parseFile(p,filePath);
要查看 chapter
实体是否已解析,请在文档中查找 chapter
元素节点。
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]
版本历史记录
在 R2021a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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)