• Blogs (9)
    • šŸ“± 236 - 992 - 3846

      šŸ“§ jxjwilliam@gmail.com

    • Version: ā€šŸš€ 1.1.0
  • Perl: install XML::LibXML

    Blogs20112011-05-29


    According to http://perl-xml.sourceforge.net/faq/, For XML Parsing, there are many parser modules (DOM-based, SAX-based, or XSLT) to choose because no one solution will be appropriate in ALL cases.

    (1) First of all, make sure to have XML::Parser installed - but don’t plan to use it. Other modules provide layers on top of XML::Parser - use them. If you’re looking for a more powerful tree based approach, try XML::LibXML for a standards compliant DOM or XML::Twig for a more ā€˜Perl-like’ API. Both of these modules support XPath. XML::LibXML is very fast, complete and stable. It can run in validating or non-validating modes and offers a DOM with XPath support.

    According to ā€˜select a parser module’ section of the web, for general purpose XML processing with Perl, The Quick Answer is, XML::LibXML is usually the best choice. It is stable, fast and powerful. To make the most of the module you need to learn and use XPath expressions.

    I used XML::Twig before, which is suitable for large XML document to parse (e.g., 60MB). This time I am going to use XML::LibXML.

    (2) Installing XML::LibXML is easy to be failure. The following are my steps for it, which has to add extra manul processing to make it finally work.

    # cd $HOME/
    # cpan
    cpan> install XML::LibXML
    get Error:
    looking for -lxml2... no
    looking for -llibxml2... libxml2 not found
    Try setting LIBS and INC values on the command line
    Or get libxml2 from http://www.libxml.org/

    (3) XML::LibXML needs libxml2.so supporting. By default, the system has no libxml.so. So I have to install it first. From http://git.gnome.org/browse/libxml2/ to download the latest version: libxml2-git-snapshot.tar.gz, In linux,we can do like this:

    # wget ftp://xmlsoft.org/libxml2/libxml2-git-snapshot.tar.gz
    # tar xzvf libxml2-git-snapshot.tar.gz
    # cd libxml2-2.7.8/
    # ./configure; make; make install;

    If everything is fine, the libxml2.so is installed to /usr/local/lib/. To check: # find /usr/local/lib/libxml2.so The ā€˜libxml2.so’ should be there.

    (4) Since the libxml.so is installed, continue processing XML::LibXML

    # cd /root/.cpan/build/XML-LibXML-1.70/
    # vi Makefile.PL to add $config{LIBS} path:
      $config{LIBS} = '-L/usr/local/lib -L/usr/lib -lxml2 -lm';
    then:
    # Perl Makefile.PL
    # make; make install;
    # perldoc XML::LibXML

    to check it is successfully installed.

    That’s the processing. The manually adding $config{LIBS} is important, coz without it, perl can’t find libxml.so to associate.