Libxml++:在出现有效性错误时返回 Line/Column 数字
Libxml++: Returning Line/Column number upon validity errors
我正在编写一个简单的 C++ 程序来解析 XML 文件以检查它的格式是否正确,以及它是否对提供的模式有效。根据软件负责人的限制,我只能使用 Libxml++。
我让一切正常,现在正在尝试处理错误,以便它 returns 成为更有意义的错误消息。在解析错误中,这已经为我完成了,因为它 returns 发生解析问题的行号和列号。但是,对于有效性异常,它仅说明捕获到有效性错误的元素以及有关错误的简短消息。
是否可以修改它,使其也returns遇到的行号和列号?问题是,如果针对非唯一元素捕获了有效性错误,如果 XML 文件有数千行长,则很难找到它。
我正在使用 DomParser 来解析 XML,并使用 SchemaValidator class,如 libxml++
中所示
据我所知,libxml++ 无法做到这一点,但您可以直接使用底层的 libxml2 函数。关键是使用 xmlSchemaSetValidStructuredErrors. The error handler receives an xmlError 注册结构化错误处理程序,其中包含行号和列号的字段。该列存储在 int2
中。请参阅以下示例程序:
#include <stdio.h>
#include <libxml/xmlschemas.h>
void errorHandler(void *userData, xmlErrorPtr error) {
printf("Error at line %d, column %d\n%s",
error->line, error->int2, error->message);
}
int main() {
xmlSchemaParserCtxtPtr pctxt = xmlSchemaNewParserCtxt("so.xsd");
xmlSchemaPtr schema = xmlSchemaParse(pctxt);
xmlSchemaValidCtxtPtr vctxt = xmlSchemaNewValidCtxt(schema);
xmlSchemaSetValidStructuredErrors(vctxt, errorHandler, NULL);
xmlSchemaValidateFile(vctxt, "so.xml", 0);
return 0;
}
给定一个架构 so.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="doc">
<xs:complexType>
<xs:sequence>
<xs:element name="item" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="attr" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniq">
<xs:selector xpath="item"/>
<xs:field xpath="@attr"/>
</xs:unique>
</xs:element>
</xs:schema>
和一份文件so.xml
<doc>
<item attr="one"/>
<item attr="two"/>
<item attr="three"/>
<item attr="one"/>
</doc>
程序打印
Error at line 5, column 23
Element 'item': Duplicate key-sequence ['one'] in unique identity-constraint 'uniq'.
我正在编写一个简单的 C++ 程序来解析 XML 文件以检查它的格式是否正确,以及它是否对提供的模式有效。根据软件负责人的限制,我只能使用 Libxml++。
我让一切正常,现在正在尝试处理错误,以便它 returns 成为更有意义的错误消息。在解析错误中,这已经为我完成了,因为它 returns 发生解析问题的行号和列号。但是,对于有效性异常,它仅说明捕获到有效性错误的元素以及有关错误的简短消息。
是否可以修改它,使其也returns遇到的行号和列号?问题是,如果针对非唯一元素捕获了有效性错误,如果 XML 文件有数千行长,则很难找到它。
我正在使用 DomParser 来解析 XML,并使用 SchemaValidator class,如 libxml++
中所示据我所知,libxml++ 无法做到这一点,但您可以直接使用底层的 libxml2 函数。关键是使用 xmlSchemaSetValidStructuredErrors. The error handler receives an xmlError 注册结构化错误处理程序,其中包含行号和列号的字段。该列存储在 int2
中。请参阅以下示例程序:
#include <stdio.h>
#include <libxml/xmlschemas.h>
void errorHandler(void *userData, xmlErrorPtr error) {
printf("Error at line %d, column %d\n%s",
error->line, error->int2, error->message);
}
int main() {
xmlSchemaParserCtxtPtr pctxt = xmlSchemaNewParserCtxt("so.xsd");
xmlSchemaPtr schema = xmlSchemaParse(pctxt);
xmlSchemaValidCtxtPtr vctxt = xmlSchemaNewValidCtxt(schema);
xmlSchemaSetValidStructuredErrors(vctxt, errorHandler, NULL);
xmlSchemaValidateFile(vctxt, "so.xml", 0);
return 0;
}
给定一个架构 so.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="doc">
<xs:complexType>
<xs:sequence>
<xs:element name="item" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="attr" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniq">
<xs:selector xpath="item"/>
<xs:field xpath="@attr"/>
</xs:unique>
</xs:element>
</xs:schema>
和一份文件so.xml
<doc>
<item attr="one"/>
<item attr="two"/>
<item attr="three"/>
<item attr="one"/>
</doc>
程序打印
Error at line 5, column 23
Element 'item': Duplicate key-sequence ['one'] in unique identity-constraint 'uniq'.