如何在解析 XML 实例文档时访问 XSD 元数据
How to access XSD metadata while parsing an XML instance document
在我的应用程序中,我需要根据 XSD 解析 XML 文件。 XSD 文件将存储一些针对特定 XSD 节点的元数据,当针对此 XSD 解析 XML 文件时,需要在我的应用程序中访问这些元数据。基本上,元数据是允许我 link 从输入 XML 标记值到某些特定于应用程序的上下文的信息。例如,我的 XSD 元数据可能存储标签到数据库 table 名称等的映射。
我一直在网上搜索有关如何实现上述目标的示例。我发现 xsd:appinfo 是将元数据存储在 XSD 文件中的一种方式,但似乎无法在 [=28] 时访问 appinfo(或任何其他 XSD 标记) =] 正在根据此 XSD 进行解析。例如,如果我使用 Xerces SAX 解析器,理想情况下我希望在 startElement() 回调中将 appinfo 提供给我。但是,我在 SAX 或 DOM 解析中看不到任何实现此目的的方法。
难道上面的要求不能实现吗?
注意:我正在考虑对解析器库使用 C/C++ 或 Python。
谢谢。
有些实用程序可以根据 XML 架构为 C++ 类 生成代码。例如,CodeSynthesis、Codalogic、Altova 等。使用它们,您可以得到一个从元数据派生的强类型 类 模型。
关键是你想使用语法接口...
Apache Xerces2 Java
Xerces2-J 为 XSD 语法实现 XML Schema API。
您可以获得 Grammar by in various ways. What's best will depend upon your needs. Read the FAQ for Caching & Preparsing Grammars. Also, consult the sample, xni.XMLGrammarBuilder 处理语法的代码示例。
然后您可以将语法转换为 XSGrammar interface, which has a toXSModel() method, which returns a XSModel, which has a getAnnotations() 方法。
Apache Xerces C++
对于SAXParser, see getRootGrammar() or getGrammar()。
对于XercesDOMParser, see getRootGrammar() or getGrammar()。
在我的应用程序中,我需要根据 XSD 解析 XML 文件。 XSD 文件将存储一些针对特定 XSD 节点的元数据,当针对此 XSD 解析 XML 文件时,需要在我的应用程序中访问这些元数据。基本上,元数据是允许我 link 从输入 XML 标记值到某些特定于应用程序的上下文的信息。例如,我的 XSD 元数据可能存储标签到数据库 table 名称等的映射。
我一直在网上搜索有关如何实现上述目标的示例。我发现 xsd:appinfo 是将元数据存储在 XSD 文件中的一种方式,但似乎无法在 [=28] 时访问 appinfo(或任何其他 XSD 标记) =] 正在根据此 XSD 进行解析。例如,如果我使用 Xerces SAX 解析器,理想情况下我希望在 startElement() 回调中将 appinfo 提供给我。但是,我在 SAX 或 DOM 解析中看不到任何实现此目的的方法。
难道上面的要求不能实现吗?
注意:我正在考虑对解析器库使用 C/C++ 或 Python。
谢谢。
有些实用程序可以根据 XML 架构为 C++ 类 生成代码。例如,CodeSynthesis、Codalogic、Altova 等。使用它们,您可以得到一个从元数据派生的强类型 类 模型。
关键是你想使用语法接口...
Apache Xerces2 Java
Xerces2-J 为 XSD 语法实现 XML Schema API。
您可以获得 Grammar by in various ways. What's best will depend upon your needs. Read the FAQ for Caching & Preparsing Grammars. Also, consult the sample, xni.XMLGrammarBuilder 处理语法的代码示例。
然后您可以将语法转换为 XSGrammar interface, which has a toXSModel() method, which returns a XSModel, which has a getAnnotations() 方法。
Apache Xerces C++
对于SAXParser, see getRootGrammar() or getGrammar()。
对于XercesDOMParser, see getRootGrammar() or getGrammar()。