Main Content

matlab.io.xml.dom.ResourceIdentifier 类

命名空间: matlab.io.xml.dom

XML 资源标识符

自 R2021a 起

描述

matlab.io.xml.dom.ResourceIdentifier 类的对象标识实体解析器要标识的资源类型。

如果您配置 matlab.io.xml.dom.Parser 对象以通过使用从 matlab.io.xml.dom.EntityResolver 类派生的类来解析实体,并且解析器遇到实体,则解析器会创建一个 matlab.io.xml.dom.ResourceIdentifier 对象。您可以在实体解析器的 resolveEntity 方法中访问 ResourceIdentifier 对象。使用 ResourceIdentifier 对象来确定实体的解析。

matlab.io.xml.dom.ResourceIdentifier 类是一个 handle 类。

类属性

ConstructOnLoad
true
HandleCompatible
true

有关类属性的信息,请参阅类属性

属性

全部展开

资源的公共 ID,指定为字符串标量。

属性:

GetAccess
public
SetAccess
private
GetObservable
true
SetObservable
true

资源的系统 ID,指定为字符串标量。

属性:

GetAccess
public
SetAccess
private
GetObservable
true
SetObservable
true

架构的位置,指定为字符串标量。

属性:

GetAccess
public
SetAccess
private
GetObservable
true
SetObservable
true

要解析的实体的命名空间的 URI,指定为字符串标量。

属性:

GetAccess
public
SetAccess
private
GetObservable
true
SetObservable
true

资源的基本 URI,指定为字符串标量。

属性:

GetAccess
public
SetAccess
private
GetObservable
true
SetObservable
true

方法

全部展开

示例

全部折叠

此示例创建一个实体解码器,配置解析器来使用该解码器,并解析包含实体引用的 XML 文件。

该示例使用下列文件:

  • chapter.xml 包含章节的标记。

<?xml version="1.0" encoding="UTF-8"?>
<chapter><title color="red">Introduction</title></chapter>
  • book.xml 包含实体引用 &chapter;,并声明该实体的资源为 chapter.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book [
<!ENTITY chapter SYSTEM "chapter.xml">
]>
<book>
    &chapter;
</book>
  • BookEntityResolver 是抽象类 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

创建一个实体解析器作为 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 对象中。

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

要查看 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 中推出