010
10.12.2006, 01:38 Uhr
J-jayz-Z
Perl Crack ala Carte (Operator)
|
Hmm, wirklich einfach, da sich Xerces exakt an die DOM Spezifikation hält, kann man auch die etwas bessere Java Dokumentation dafür nutzen. Mal ein kleines Beispiel:
C++: |
#include <xercesc/parsers/XercesDOMParser.hpp> #include <xercesc/dom/DOM.hpp> #include <xercesc/util/XMLString.hpp> #include <xercesc/util/PlatformUtils.hpp>
#include <iostream> XERCES_CPP_NAMESPACE_USE
int main (int argc, char* args[]) { try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); std::cout << message << std::endl; XMLString::release(&message); }
XercesDOMParser* parser = new XercesDOMParser();
std::cout << "Parsing XML File..." << std::endl;
std::string xmlFile = "C:\\foo.xml";
try { parser->parse(xmlFile.c_str()); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); std::cout << message << std::endl; XMLString::release(&message); } catch (const DOMException& toCatch) { char* message = XMLString::transcode(toCatch.msg); std::cout << message << std::endl; XMLString::release(&message); } catch (...) { std::cout << "Unexpected Exception" << std::endl; } DOMDocument* doc = parser->getDocument(); if(doc != 0) { std::cout << "You got the Document" << std::endl; XMLCh* rootElement = XMLString::transcode("bla"); DOMNodeList* list = doc->getElementsByTagName(rootElement); for(int i = 0; i < list->getLength(); i++) { DOMElement* element = (DOMElement*)doc->getElementsByTagName(XMLString::transcode("bla"))->item(i); DOMNode* node = element->getElementsByTagName(XMLString::transcode("name"))->item(0); std::cout << XMLString::transcode(node->getTextContent()) << std::endl; } } delete parser; std::cin.get(); return EXIT_SUCCESS; }
|
Und die Datei dazu
C++: |
<root> <bla> <name>test</name> </bla> <bla> <name>test2</name> </bla> </root>
|
Wirklich gut und auch sauber zu programmieren... -- perl -Mstrict -Mwarnings -e 'package blub; sub new { bless {} } sub bar {my $self=shift; $self->{bla}="66756e2d736f66742e6465"; return $self->{bla};} my $foo=blub->new();print "Hallo ";print pack("H*",$foo->bar()); print "\n"' |