added dom2pyx example

This commit is contained in:
jez 2007-10-25 20:40:08 +00:00
parent a7d050841d
commit 240c97814c
2 changed files with 92 additions and 1 deletions

View file

@ -1,4 +1,4 @@
noinst_PROGRAMS = domwriter
noinst_PROGRAMS = domwriter dom2pyx
AM_CPPFLAGS = -I$(top_srcdir)/include @PARSER_HEADERS@ $(BOOST_CPPFLAGS)
LIBARABICA = $(top_builddir)/src/libarabica.la
@ -6,3 +6,6 @@ LIBARABICA = $(top_builddir)/src/libarabica.la
domwriter_SOURCES = DOMWriter.cpp
domwriter_LDADD = $(LIBARABICA)
dom2pyx_SOURCES = dom2pyx.cpp
dom2pyx_LDADD = $(LIBARABICA)

88
examples/DOM/dom2pyx.cpp Executable file
View file

@ -0,0 +1,88 @@
#include <string>
#include <iostream>
#include <DOM/SAX2DOM/SAX2DOM.hpp>
void generate_pyx(Arabica::DOM::Element<std::string> element);
std::string escape(const std::string& str);
int main(int argc, char* argv[])
{
if(argc < 2)
{
std::cout << "Usage : " << argv[0] << " xmlfile ... " << std::endl;
return 0;
} // if(argc < 2)
Arabica::SAX2DOM::Parser<std::string> domParser;
for(int i = 1; i < argc; ++i)
{
std::string file(argv[i]);
Arabica::SAX::InputSource<std::string> is;
is.setSystemId(file);
domParser.parse(is);
Arabica::DOM::Document<std::string> doc = domParser.getDocument();
if(doc == 0)
{
std::cerr << "Error parsing " << file << std::endl;
continue;
}
generate_pyx(doc.getDocumentElement());
}
return 0;
} // main
void generate_pyx(Arabica::DOM::Element<std::string> element)
{
std::cout << '(' << element.getNodeName() << std::endl;
if(element.hasAttributes())
{
Arabica::DOM::NamedNodeMap<std::string> attrs = element.getAttributes();
for(int a = 0; a != attrs.getLength(); ++a)
{
Arabica::DOM::Node<std::string> attr = attrs.item(a);
std::cout << 'A' << attr.getNodeName()
<< ' ' << escape(attr.getNodeValue())
<< std::endl;
} // for ...
} // if ...
for(Arabica::DOM::Node<std::string> n = element.getFirstChild(); n != 0; n = n.getNextSibling())
{
switch(n.getNodeType())
{
case Arabica::DOM::Node<std::string>::ELEMENT_NODE:
generate_pyx(static_cast<Arabica::DOM::Element<std::string> >(n));
break;
case Arabica::DOM::Node<std::string>::TEXT_NODE:
case Arabica::DOM::Node<std::string>::CDATA_SECTION_NODE:
std::cout << '-' << escape(n.getNodeValue()) << std::endl;
break;
case Arabica::DOM::Node<std::string>::PROCESSING_INSTRUCTION_NODE:
std::cout << '?' << n.getNodeName()
<< ' ' << n.getNodeValue() << std::endl;
break;
} // switch ...
} // for ...
std::cout << ')' << element.getNodeName() << std::endl;
} // generate_pyx
std::string escape(const std::string& str)
{
std::string estr(str);
std::string::size_type i(estr.find("\n"));
while(i != std::string::npos)
{
estr.replace(i, 1, "\\n", 2);
i = estr.find("\n", i);
} // while ...
return estr;
} // escape
// end of file