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文件. ...
随机推荐
- Hibernate学习(三)一对多数据的保存
保存习惯代码 @Test public void saveTwoTableTest(){ Transaction transaction = null; try { transaction = ses ...
- IOS 使用cocoapods后无法导入头文件问题
IOS 使用cocoapods后无法导入头文件问题 这时候如果你发现import的时候没有提示AFN e t wo r k i n g.h的文件,可以在target-Build Settings下修改 ...
- Java多维数组各轴长度可以不对齐
- 《Android进阶之光》--Android新特性
Android 5.0新特性 1)全新的Material Design设计风格 2)支持多种设备 3)全新的通知中心设计--按照优先级显示 4)支持64位ART虚拟机 5)多任务视窗Overview ...
- Web自动化之Headless Chrome测试框架集成
使用Selenium操作headless chrome 推荐 简介 WebDriver是一个W3C标准, 定义了一套检查和控制用户代理(比如浏览器)的远程控制接口,各大主流浏览器来实现这些接口以便调用 ...
- 实用技巧:如何通过IP地址进行精准定位
在甲方工作的朋友可能会遇到这样的问题,服务器或者系统经常被扫描,通过IP地址我们只能查到某一个市级城市,如下图: 当我们想具体到街道甚至门牌号,该怎么办??? 偶然间发现百度地图有高精度IP定位API ...
- pyinstaller打包py文件成exe文件时,出现ImportError: No module named 'pefile'错误解决办法!
首先pyinstaller的安装与使用详见如下链接: 安装完成之后,命令行中输入pyinstaller之后,结果如下: ImportError: No module named 'pefile' 缺少 ...
- MySQL参数max_connect_errors分析释疑
最近一MySQL服务器,由于一些特殊因素遇到"ERROR 1129 (00000): Host 'xxx' is blocked because of many connection e ...
- FFMPEG在windows平台编译的详细过程,包括环境安装
下面开始: 由于FFMpeg是基于Linux开发的开源项目,源代码和Windows下最常见的Visual Studio提供的C/C++编译器不兼容,因此它不能使用MSVC++编译.要想使用FFMpeg ...
- Java之List排序
1.Java封装类 Student.java: /** * @Title:Student.java * @Package:com.you.data * @Description: * @Author: ...