头文件

#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校验的更多相关文章

  1. 用xerces-c来进行xml schema校验

    在xerces-c的官方站点上有文章指引说明是怎样进行xml schema校验. http://xerces.apache.org/xerces-c/schema-3.html 给出的样例代码: // ...

  2. 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化

    谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...

  3. 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 ...

  4. XML约束——Schema约束

    XML Schema 也是一种用于定义和描述 XML 文档结构与内容的模式语言,其出现是为了克服 DTD 的局限性 XML Schema VS DTD: •XML Schema符合XML语法结构. • ...

  5. JavaScripts学习日记——XML DTD Schema

    今日关键词: XML DTD Schema 1.XML 1 XML的概述 1.1 什么是XML XML全称为Extensible Markup Language,意思是可扩展的标记语言.XML语法上和 ...

  6. XML Dtd Schema

    在XML技术里,可以编写一个文档来约束一个XML文档的书写规范,这称之为XML约束. 整体比较: XML Schema符合XML语法结构. DOM.SAX等XML API很容易解析出XML Schem ...

  7. XML的Schema约束

    XSD文档至少要包含:schema根元素和XML模式命名空间的定义.元素定义.需要注意的是XSD中必须定义一个且只能定义一个schema根元素,根元素中包括模式的约束,XML模式命名空间的定义,其他命 ...

  8. Springboot中使用Xstream进行XML与Bean 相互转换

    在现今的项目开发中,虽然数据的传输大部分都是用json格式来进行传输,但是xml毕竟也会有一些老的项目在进行使用,正常的老式方法是通过获取节点来进行一系列操作,个人感觉太过于复杂.繁琐.推荐一套简单的 ...

  9. 如何在IJ中使用Jaxb2通过xml定义生成对应的Java Entity类的文件

    #0. 准备要转换的xml文件,在Project视界中,右击这个xml文件,在弹出的菜单上选择“Generate XSD schema from XML File...”, 按默认设置生成xsd文件. ...

随机推荐

  1. $.ajax的一些坑啊

    1.如果发送ajax返回的数据为json务必设置其 Content-Type:application/json;charset=UTF-8 不然会导致其success:function(data)中的 ...

  2. C#委托与事件--后续补充

    委托.事件补充 针对昨天文章 委托:让方法可以跟简单对象一样作为参数进行传递,也就是将方法作为参数进行封装. 方法:本质就是代码段 其实也好理解,目的就是为了封装,多态,既然简单对象如int i可以做 ...

  3. linux命令学习汇总

    1. touch 命令 touch -简单来说touch 命令可以用来建立文件 文件不存在的情况下,也可以更改创建时间等等 2. pwd 查看目录---一般看目录用pwd 即可但是pwd -P 会不以 ...

  4. PhpStorm如何下载github上的代码到本地

    1.看着菜单栏有一个VCS(Virus Capture Scripter)集群服务器的选项,选择其下面的Checkout from Version Control,然后 (1)选择GIT:输入git的 ...

  5. Linux CentOS安装配置MySQL数据库

    没什么好说的,直接正面刚吧. 安装mysql数据库 a)下载mysql源安装包:wget http://dev.mysql.com/get/mysql57-community-release-el7- ...

  6. VxWorks启动流程

    镜像种类不同,VxWorks的启动过程会有所不同. 我们项目中使用的是加载型VxWorks镜像 函数 函数功能 所在文件 bootTask() (a)    通过createBootLineFromF ...

  7. USB设备驱动概述

    USB设备驱动 ·  )USB Hub:每个USBHost控制器都会自带一个USB Hub,被称为根(Root)Hub.这个根Hub可以接子(Sub)Hub,每个Hub上挂载USB设备.一般PC有8个 ...

  8. SecurityError:Error:#2148

    1.错误描述 SecurityError:Error:#2148:SWF文件http://localhost:8888/UploadDownload/Flash/ReadLocalFile.swf/[ ...

  9. HashMap,LinkedHashMap,TreeMap对比

    共同点: HashMap,LinkedHashMap,TreeMap都属于Map:Map 主要用于存储键(key)值(value)对,根据键得到值,因此键不允许键重复,但允许值重复. 不同点: 1.H ...

  10. 手机端仿ios的银行下拉脚本五

    代码 <script> $('#bankName').click(function(){ var $this = $(this); new Picker({ "title&quo ...