#include #include #include void generate_pyx(Arabica::DOM::Element 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 domParser; for(int i = 1; i < argc; ++i) { std::string file(argv[i]); Arabica::SAX::InputSource is; is.setSystemId(file); domParser.parse(is); Arabica::DOM::Document 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 element) { std::cout << '(' << element.getNodeName() << std::endl; if(element.hasAttributes()) { Arabica::DOM::NamedNodeMap attrs = element.getAttributes(); for(int a = 0; a != attrs.getLength(); ++a) { Arabica::DOM::Node attr = attrs.item(a); std::cout << 'A' << attr.getNodeName() << ' ' << escape(attr.getNodeValue()) << std::endl; } // for ... } // if ... for(Arabica::DOM::Node n = element.getFirstChild(); n != 0; n = n.getNextSibling()) { switch(n.getNodeType()) { case Arabica::DOM::Node::ELEMENT_NODE: generate_pyx(static_cast >(n)); break; case Arabica::DOM::Node::TEXT_NODE: case Arabica::DOM::Node::CDATA_SECTION_NODE: std::cout << '-' << escape(n.getNodeValue()) << std::endl; break; case Arabica::DOM::Node::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