c++中使用xercesc对xml进行schema校验
头文件
#pragma once
#if !defined(AFX_A1CONTENTHANDLER_H__E0CFBC18_CCC1_42F3_B0A4_B03331AB9693__INCLUDED_)
#define AFX_A1CONTENTHANDLER_H__E0CFBC18_CCC1_42F3_B0A4_B03331AB9693__INCLUDED_
#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/sax/SAXException.hpp>
#include <iostream>
using namespace std;
using namespace xercesc;
class OperContentHandler :
public DefaultHandler
{
public:
OperContentHandler();
virtual ~OperContentHandler();
void characters
(
const XMLCh* const chars,
const XMLSize_t length
);
void endElement
(
const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname
);
virtual void startElement
(
const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname,
const Attributes& attrs
);
// 继承自default Handle, 用于对错误的处理
void error(const SAXParseException& exc)
{
throw exc;
}
void fatalError(const SAXParseException& exc)
{
throw exc;
}
void warning(const SAXParseException& exc)
{
throw exc;
}
wstring goods;
};
#endif // !defined(AFX_A1CONTENTHANDLER_H__E0CFBC18_CCC1_42F3_B0A4_B03331AB9693__INCLUDED_)
cpp文件
// OperContentHandler.cpp: derived class from SAXContentHandlerImpl
#include "OperContentHandler.hpp"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
OperContentHandler::OperContentHandler()
{
}
OperContentHandler::~OperContentHandler()
{
//object destruction is handled by the Release() impl of parent class
}
void OperContentHandler::startElement
(
const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname,
const Attributes& attrs
)
{
wstring sNodeName = localname;
goods += sNodeName;
}
void OperContentHandler::endElement( const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname )
{
wstring sNodeName = localname;
goods += sNodeName;
}
void OperContentHandler::characters( const XMLCh* const chars,
const XMLSize_t length )
{
wstring sNodeValue(chars);
goods += sNodeValue;
}
头文件
#pragma once
#ifndef _OPSMGRXMLVALIDATOR__H_
#define _OPSMGRXMLVALIDATOR__H_
#include "xercesc\sax2\XMLReaderFactory.hpp"
#include "OperContentHandler.hpp"
#include<boost/filesystem/config.hpp>
#include <3rd/boost/filesystem.hpp>
#include<sstream>
struct ERRORCODE
{
std::wstring wstrCol;
std::wstring wstrRow;
std::wstring wstrErrorMessage;
};
class OPSXMLValidator
{
public:
OPSXMLValidator();
~OPSXMLValidator();
private:
void GetSchemaPath(std::string strSchemaSrcPath);
private:
OperContentHandler * m_handler;
SAX2XMLReader* m_parser;
Grammar * m_grammar;
int m_iErrorCol;
int m_iErroRow;
std::wstring m_wstrErrorMessage;
std::string m_strSchemaParentPath;
std::string m_strSchemaPath;
public:
//接口
int ParseXML(std::string strPath);
int GetTheErrorState(std::wstring& wstrErrorCol, std::wstring &wstrErrorRow, std::wstring &wstrErrorMessage);
};
#endif
cpp文件
#include"opsMgrXMLValidator.h"
#include<boost/filesystem/config.hpp>
#include <3rd/boost/filesystem.hpp>
using namespace xercesc;
OPSXMLValidator::OPSXMLValidator():
m_handler(NULL),
m_parser(NULL),
m_grammar(NULL),
m_iErrorCol(0),
m_iErroRow(0),
m_strSchemaPath(""),
m_strSchemaParentPath(""),
m_wstrErrorMessage(L"")
{
XMLPlatformUtils::Initialize();
m_handler = new OperContentHandler;
m_parser = XMLReaderFactory::createXMLReader();
GetSchemaPath("");
if(m_strSchemaPath.empty())
{
std::cout << " error:the sechema path is empty."<<std::endl;
}
// m_grammar = m_parser->loadGrammar(m_strSchemaPath.c_str(), Grammar::SchemaGrammarType, true);
m_parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
m_parser->setFeature(XMLUni::fgXercesSchema, true);
m_parser->setFeature(XMLUni::fgXercesValidationErrorAsFatal, true);
m_parser->setFeature(XMLUni::fgXercesContinueAfterFatalError, false);
m_parser->setContentHandler(m_handler);
m_parser->setErrorHandler(m_handler);
}
OPSXMLValidator::~OPSXMLValidator()
{
if(NULL != m_handler)
{
delete m_handler;
}
if(NULL != m_grammar)
{
delete m_grammar;
}
if(NULL != m_parser)
{
delete m_parser;
}
XMLPlatformUtils::Terminate();
}
void OPSXMLValidator::GetSchemaPath(std::string strSchemaSrcPath)
{
if(!strSchemaSrcPath.empty())
{
m_strSchemaPath = strSchemaSrcPath;
return ;
}
boost::filesystem::path CurrentPath = boost::filesystem::current_path();
boost::filesystem::path SchemaPath = CurrentPath.parent_path().parent_path();
SchemaPath.append("\\data\\schema");
if(!boost::filesystem::exists(SchemaPath))
{
// the schema path is not exist.
return ;
}
m_strSchemaPath = SchemaPath.string().c_str();
SchemaPath = SchemaPath.parent_path();
m_strSchemaParentPath = SchemaPath.string().c_str();
boost::filesystem::path TargetPath(m_strSchemaParentPath);
TargetPath.append("/temp");
if(!boost::filesystem::exists(TargetPath))
{
boost::filesystem::create_directory(TargetPath);
}
TargetPath.append("/xml");
if(!boost::filesystem::exists(TargetPath))
{
boost::filesystem::create_directory(TargetPath);
}
}
int OPSXMLValidator::ParseXML(std::string strPath)
{
int errorIndex = 0;
std::string fileName;
boost::filesystem::path TargetPath(m_strSchemaParentPath);
try
{
boost::filesystem::path ResourcePath(strPath.c_str());
fileName = ResourcePath.filename().string().c_str();
TargetPath.append("/temp/xml/");
TargetPath.append(fileName.c_str());
if(boost::filesystem::exists(TargetPath))
{
boost::filesystem::remove(TargetPath);
}
boost::filesystem::copy_file(ResourcePath, TargetPath);
TargetPath.normalize();
m_parser->parse(TargetPath.wstring().c_str());
}
catch(const SAXParseException& toCatch)
{
m_iErrorCol = (int) toCatch.getColumnNumber();
m_iErroRow = (int) toCatch.getLineNumber();
m_wstrErrorMessage = toCatch.getMessage();
errorIndex = -1;
}
catch(const XMLException & xe)
{
m_wstrErrorMessage = xe.getMessage();
errorIndex = -1;
}
if(boost::filesystem::exists(TargetPath))
{
boost::filesystem::remove(TargetPath);
}
return errorIndex;
}
int OPSXMLValidator::GetTheErrorState(std::wstring &wstrErrorCol, std::wstring &wstrErrorRow, std::wstring &wstrErrorMessage)
{
int iRet = 0;
wstringstream iTows;
if(0 != m_iErrorCol || 0 != m_iErroRow)
{
iTows << m_iErrorCol;
iTows >> wstrErrorCol;
iTows.clear();
iTows << m_iErroRow;
iTows >> wstrErrorRow;
wstrErrorMessage = m_wstrErrorMessage.c_str();
}
return iRet ;
}
c++中使用xercesc对xml进行schema校验的更多相关文章
- 用xerces-c来进行xml schema校验
在xerces-c的官方站点上有文章指引说明是怎样进行xml schema校验. http://xerces.apache.org/xerces-c/schema-3.html 给出的样例代码: // ...
- 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化
谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...
- xml语法、DTD约束xml、Schema约束xml、DOM解析xml
今日大纲 1.什么是xml.xml的作用 2.xml的语法 3.DTD约束xml 4.Schema约束xml 5.DOM解析xml 1.什么是xml.xml的作用 1.1.xml介绍 在前面学习的ht ...
- XML约束——Schema约束
XML Schema 也是一种用于定义和描述 XML 文档结构与内容的模式语言,其出现是为了克服 DTD 的局限性 XML Schema VS DTD: •XML Schema符合XML语法结构. • ...
- JavaScripts学习日记——XML DTD Schema
今日关键词: XML DTD Schema 1.XML 1 XML的概述 1.1 什么是XML XML全称为Extensible Markup Language,意思是可扩展的标记语言.XML语法上和 ...
- XML Dtd Schema
在XML技术里,可以编写一个文档来约束一个XML文档的书写规范,这称之为XML约束. 整体比较: XML Schema符合XML语法结构. DOM.SAX等XML API很容易解析出XML Schem ...
- XML的Schema约束
XSD文档至少要包含:schema根元素和XML模式命名空间的定义.元素定义.需要注意的是XSD中必须定义一个且只能定义一个schema根元素,根元素中包括模式的约束,XML模式命名空间的定义,其他命 ...
- Springboot中使用Xstream进行XML与Bean 相互转换
在现今的项目开发中,虽然数据的传输大部分都是用json格式来进行传输,但是xml毕竟也会有一些老的项目在进行使用,正常的老式方法是通过获取节点来进行一系列操作,个人感觉太过于复杂.繁琐.推荐一套简单的 ...
- 如何在IJ中使用Jaxb2通过xml定义生成对应的Java Entity类的文件
#0. 准备要转换的xml文件,在Project视界中,右击这个xml文件,在弹出的菜单上选择“Generate XSD schema from XML File...”, 按默认设置生成xsd文件. ...
随机推荐
- web1 - HTML&CSS
Brackets 编辑器的安装和使用 Emmet:HTML/CSS代码快速编写 HTML && CSS
- Python 上下文管理器和else块
最终,上下文管理器可能几乎与子程序(subroutine)本身一样重要.目前,我们只了解了上下文管理器的皮毛--Basic 语言有with 语句,而且很多语言都有.但是,在各种语言中 with 语句的 ...
- SDL 2.0 如何在 windows 上使用?
https://wiki.libsdl.org/APIByCategory http://adolfans.github.io/sdltutorialcn/sdl-2-dot-0-tutorial-i ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- 【java学习笔记】正则表达式
一.正则表达式 1.预定义字符集 . 表示任意一个字符 \d 表示任意一个数字 \w 表示任意一个单词字符(只能是数字.字母.下划线) \s 表示任意一个空白字符(\t\r\n\f\x0B) \D ...
- mongodb命令行group分组和java代码中group分组
group分组统计是数据库比较常用的功能,mongodb也不例外.不过相对于普通的增删改查,group操作就略微麻烦一些, 这里对group在shell中的操作.使用java原生代码操作以及集成spr ...
- mysql 查询表死锁 和结束死锁的表步骤
1.查询是否锁表 show OPEN TABLES ; 2.查询进程 show processlist 查询到相对应的进程===然后 kill id 3.查看正在锁的事务 SELECT * FR ...
- HighCharts之2D带Label的折线图
HighCharts之2D带Label的折线图 1.HighCharts之2D带Label的折线图源码 LineLabel.html: <!DOCTYPE html> <html&g ...
- Java获取当前的年月
今天,我在尝试从数据库取数据的过程中,发现页面初始化时需要给时间控件赋初值.于是,我就写了一个获取当前年月的时间工具类. 1.具体源码如下: YearAndMonth.java: /** * @Tit ...
- org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException
1.错误原因 org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't ...