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文件. ...
随机推荐
- Activt工作流数据库对应表的作用
1.资源库流程规则表 1) act_re_deployment 部署信息表 2) act_re_model 流程设计模型部署表 3) ...
- network programming-简单的TCP客户服务器编程
简单的TCP程序客户端流程:创建套接字(套接字用IP地址:端口号)表示)socket()->请求连接connect()->交换数据 send()/recv()->关闭连接 close ...
- qt Multimedia 模块类如何使用?
qt 多媒体模块介绍 类名 英文描述 中文描述 QAudioBuffer Represents a collection of audio samples with a specific format ...
- 我博客上的围棋js程序
作为一个围棋爱好者,就决定在博客里加个围棋js程序.于是,申请了博客的js权限,美化美化我的博客. 好在js的语法像C系的,看了看,写个程序应该还是可以的. 围棋里,设计好基本的数据结构: //a是1 ...
- FineUIPro控件库深度解析
FineUIPro控件库 FineUIPro是一套基于jQuery的专业ASP.NET控件库,始于2008年的开源版FineUI控件库. 当年为了提升项目的开发效率,降低代码复杂度,减少对CSS和Ja ...
- H3C虚拟化之IRF
SA system-view irf domain 10 irf member 1 ren 1 y int ten 1/0/50 shu qu irf-port 1/1 port group int ...
- Mybatis认识
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .iB ...
- 文件无法复制的原因-IT33
Win7系统复制数据至其他硬盘或者是移动存储设备是,有时会发生无法复制文件过大的情况.这里先大致介绍一下硬盘文件系统分为NFTS格式和FAT32格式这两种,其中FAT32仅支持单次移动4G以下容量的数 ...
- 我的Java设计模式-责任链模式
今天来说说程序员小猿和产品就关于需求发生的故事.前不久,小猿收到了产品的需求. 产品经理:小猿,为了迎合大众屌丝用户的口味,我们要放一张图,要露点的. 小猿:......露点?你大爷的,让身为正义与纯 ...
- C++ string数据类型的实现
#include <cstdlib> #include <cstring> class Mystring { public: Mystring(const char * pda ...