Xerces

install

from http://www.codesynthesis.com/projects/xsd/extras/build-windows.xhtml
$ cd
$ gzip -dc xerces-c-3.1.1.tar.gz | tar x
$ cd xerces-c-3.1.1
$ ./configure --disable-threads --disable-network \
--enable-transcoder-windows --disable-shared \
CXXFLAGS=-O2 CFLAGS=-O2
$ cd src
$ make

from http://www.cplusplus.com/forum/beginner/58232/
To summerise, cd into the directory, then type:
1
2
3
4
5
./configure
make
su
make install
exit



compiler

g++ test.c -o test  -lxerces-c

#include <xercesc/util/PlatformUtils.hpp>
// Other include files, declarations, and non-Xerces-C++ initializations.

using namespace xercesc;

int main(int argc, char* argv[])
{
  try {
    XMLPlatformUtils::Initialize();
  }
  catch (const XMLException& toCatch) {
    // Do your failure processing here
    return 1;
  }

  // Do your actual work with Xerces-C++ here.

  XMLPlatformUtils::Terminate();

  // Other terminations and cleanup.
  return 0;
}

fix error
./test: error while loading shared libraries: libxerces-c-3.2.so: cannot open shared object file: No such file or directory

cp /usr/local/lib/libxerces-c-3.2.so /usr/lib/
ldconfig


from https://www.ibm.com/developerworks/jp/xml/library/x-xsdxerc/index.html
 Xerces-C++ SAX 2パーサーでのスキーマ検証を有効にする
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Necessary includes. We refer to these as "common includes"
// in the following examples.
#include <xercesc/sax2/XMLReaderFactory.hpp>
#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/DefaultHandler.hpp>
// Handy definitions of constants.
#include <xercesc/util/XMLUni.hpp>
// Create a SAX2 parser object.
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
// Set the appropriate features on the parser.
// Enable namespaces, schema validation, and the checking
// of all Schema constraints.
// We refer to these as "common features" in following examples.
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
parser->setFeature(XMLUni::fgXercesDynamic, false);
parser->setFeature(XMLUni::fgXercesSchema, true);
parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
// Set appropriate ContentHandler, ErrorHandler, and EntityResolver.
// These will be referred to as "common handlers" in subsequent examples.
// You will use a default handler provided by Xerces-C++ (no op action).
// Users should write their own handlers and install them.
DefaultHandler handler;
parser->setContentHandler(&handler);
// The object parser calls when it detects violations of the schema.
parser->setErrorHandler(&handler);
// The object parser calls to find the schema and
// resolve schema imports/includes.
parser->setEntityResolver(&handler);
// Parse the XML document.
// Document content sent to registered ContentHandler instance.
parser->parse(xmlFile);
// Delete the parser instance.
delete parser;
 




from http://mokky14.hatenablog.com/entry/2014/03/18/190356


インストは以下手順でインスト。
$  tar xvzf xerces-c-3.1.1.tar.gz 
$  cd xerces-c-3.1.1
$  ./configure
$  make
$  sudo make install
まずは写経してみる。
解析するxmlファイルはこんなやつを用意。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE infos [
  <!ELEMENT infos (info)*>
  <!ELEMENT info (id, status, datas?)>
  <!ELEMENT id (#PCDATA)>
  <!ELEMENT status (#PCDATA)>
  <!ELEMENT datas (data)*>
  <!ELEMENT data (#PCDATA)>
  <!ATTLIST data
    type CDATA #REQUIRED
    value CDATA #REQUIRED>
  ]>
<infos>
 <info>
  <id>id1</id>
  <status>0</status>
  <datas>
   <data type="1" value="user@123"/>
  </datas>
 </info>
 
 <info>
  <id>id2</id>
  <status>1</status>
 </info>
 <info>
  <id>id3</id>
  <status>2</status>
  <datas></datas>
 </info>
 <info>
  <id>id4</id>
  <status>3</status>
  <datas>
   <data type="1" value="user@123"/>
   <data type="2" value="user@12345678"/>
   <data type="5" value="12345678"/>
  </datas>
 </info>
</infos>

ソースはここのやつを参考(というか丸パクリ)にして作成。
xmlからDOMを生成して頭から表示するプログラム。
#include <iostream>
using namespace std;

#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/dom/DOMNode.hpp>
#include <xercesc/dom/DOMText.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/sax/HandlerBase.hpp>
using namespace xercesc;

#define OK 0
#define NG 1

#define XMLFILE_PATH "sample.xml"

void writeNode(DOMNode* node);
void writeElement(DOMElement* element);
void writeText(DOMText* text);

int main(int argc, char** argv) {
 try{
  XMLPlatformUtils::Initialize();
 }
 catch(const XMLException& exp) {
  char* message = XMLString::transcode(exp.getMessage());
  cerr << "Xerces-C++ 初期化エラー" << endl;
  cerr << message << endl;
  XMLString::release(&message);
  return NG;
 }
 
 XercesDOMParser* parser = new XercesDOMParser();
 parser->setValidationScheme(XercesDOMParser::Val_Always);
 parser->setDoNamespaces(true);

 ErrorHandler* errHandler = new HandlerBase();
 parser->setErrorHandler(errHandler);

 
 try {
  const char* xmlFile = XMLFILE_PATH;
  parser->parse(xmlFile);
  DOMDocument* dom = parser->getDocument();
  writeNode(dom);
 }
 catch ( const XMLException& exp ) {
  char* message = XMLString::transcode(exp.getMessage());
  cout<< "Exception message is: \n" << message << endl;
  XMLString::release(&message);
  return NG;
 }
 catch ( const DOMException& exp ) {
  char* message = XMLString::transcode(exp.getMessage());
  cout<< "Exception message is: \n" << message << endl;
  XMLString::release(&message);
  return NG;
 }
 catch (...) {
  cout << "Unexpected Exception" << endl;
  return NG;
 }

 delete parser;
 delete errHandler;
 
 return OK;
}

void writeNode(DOMNode* node) {
 assert(node != NULL);

 switch( node->getNodeType() ) {
  case DOMNode::ELEMENT_NODE:
   writeElement(static_cast<DOMElement*>(node));
   break;

  case DOMNode::TEXT_NODE:
   writeText(static_cast<DOMText*>(node));
   break;
 }

 DOMNode* child = node->getFirstChild();
 while(child) {
  writeNode(child);
  DOMNode* next = child->getNextSibling();
  child = next; 
 }
}

void writeElement(DOMElement* element) {
 char* name = XMLString::transcode(element->getTagName());
 cout << "tag     :" << name << endl;
 XMLString::release(&name);

 DOMNamedNodeMap* map = element->getAttributes();
 for ( XMLSize_t i = 0; i < map->getLength(); i++ ) {
  DOMAttr* attr= static_cast<DOMAttr*>(map->item(i));
  char* attr_name = XMLString::transcode(attr->getName());
  char* attr_value = XMLString::transcode(attr->getValue());
  cout << attr_name << ": " << attr_value << endl;

  XMLString::release(&attr_name);
  XMLString::release(&attr_value);
 }
}

void writeText(DOMText* text) {
 XMLCh* buffer = new XMLCh[XMLString::stringLen(text->getData()) + 1];
 XMLString::copyString(buffer, text->getData());
 XMLString::trim(buffer);
 char* content = XMLString::transcode(buffer);
 delete[] buffer;

 cout << "content :" << content << endl;
 XMLString::release(&content);
}

留言

熱門文章