For a project or two, I've been using XML::Smart for my XML needs. However, the module was last updated 7 years ago, and it's looked to have stopped developing.  It's so old that it doesn't seem to even mention XSD support, something I was looking to adapt for this new project.  There's also an annoying DESTROY bug that seems to pollute die() calls.

Since most agree that XML::LibXML is the way to go for a powerful XML interface (and supports XSD), I went that route and looked into converting what I had for XML::Smart code into XML::LibXML equivalents.  What I found was absolutely appalling, especially compared to XML::Smart:

my $sXML = XML::Smart->new('file.xml', 'SMART'); my $pXML = XML::LibXML->load_xml('file.xml'); ### Basic Transversing ### # Smart = hash trees $sXML = $sXML->cut_root; my $book = $sXML->{book}[3]; print $book->{title}; # LibXML = no hash trees $pXML = $pXML->documentElement(); my $book = $pXML->childNodes()->[3]; print $book->findvalue('title'); # (though, XPaths makes it a little bit better...) print $pXML->findvalue('book[4]/title'); ############################## ### Array of values ### # Smart = simple and elegant $sXML = $sXML->cut_root; my @books = $sXML->{book}('@'); # LibXML = ugly, ugly, ugly $pXML = $pXML->documentElement(); my @books = map { $_->textContent } $pXML->find('book')->get_nodelist( +); ############################## ### Creating a new XML doc ### # Smart = light on the code, and doesn't require searching for 500 met +hods my $sXML = XML::Smart->new(); $sXML->{root}->{'xmlns:xsi'} = "http://www.w3.org/2001/XMLSchema-insta +nce"; $sXML->{root}->{'xsi:schemaLocation'} = "test.xsd"; $sXML->{root}->{'data'} = "Hello World!"; $sXML->{root}->{'data'}->set_node(); # LibXML = WTF?!? my $pXML = XML::LibXML::Document->new('1.0', 'UTF-8'); my $root = XML::LibXML::Element->new('root'); $root->setNamespace("http://www.w3.org/2001/XMLSchema-instance", 'xsi' +, 0); $root->setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", 'xs +i:schemaLocation', "test.xsd"); $pXML->setDocumentElement($root); $root->addNewChild(undef, 'data')->setData("Hello World!"); ############################## ### Adding a new row of elements ### # Smart = blends in with Perl hashes my $node = ( 'title' => 'XML Schema', 'author' => 'Eric Van der Vlist', 'publisher' => "O'Reilly Media Inc.", 'phystype' => 'Paperback', 'year' => 2002, ); push(@{$sXML->{book}}, $node); # (though, this is a little ugly...) # LibXML = Bats**t crazy! my $node = XML::LibXML::Element->new('book'); $node->addNewChild(undef, 'title') ->setData('XML Schema'); $node->addNewChild(undef, 'author') ->setData('Eric Van der Vlist'); $node->addNewChild(undef, 'publisher')->setData("O'Reilly Media Inc.") +; $node->addNewChild(undef, 'phystype') ->setData('Paperback'); $node->addNewChild(undef, 'year') ->setData(2002); $sXML->appendChild($node); ### if exists OR define ### # Smart = a simple one-liner $row->{author} ||= "Freddy Krueger"; # LibXML = crappy $row->findvalue('author') || $row->addNewChild(undef, 'author')->setDa +ta("Freddy Krueger"); ############################## ### Searching ### # Smart = meh... my $book = $sXML->{book}('title', 'eq', 'XML Schema'); # LibXML = better, but... my $book = $pXML->find('book[title="XML Schema"]'); # ...this would be really cool my $book = $sXML->{book}('[title="XML Schema"]'); my $book = $sXML->('book[title="XML Schema"]'); ##############################

Plus, the documentation for XML::LibXML is horrid!  The method reference is spread all over creation, there's too many methods, and there's no XML::LibXML::Tutorial that would actually tell you exactly what you should be doing.

Isn't there's a module to help link these together?  Give me both hash trees and the methods from LibXML.  Allow me to do something like $XML->{book}('$%').  Provide me with shorter sub names than "getElementsByLocalName".  (For god's sake, jQuery was created because people were sick of typing "document.getElementById" all the damn time.)  Make it so I don't get error messages like "Undefined subroutine &XML::LibXML::NodeList::value called at xml_parse.pl line 263".  (And this was the OTHER reason why jQuery was invented...)

Where is XML::LibXML::Smart || XML::LibXML::Tree || XML::LibXML::jQuery?


In reply to XML::LibXML - WHAR HASH TREES WHAR?! by SineSwiper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.