William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Perl: install XML::LibXML

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.

Advertisements

5 responses to “Perl: install XML::LibXML

  1. Phillip Amsbaugh 05/29/2011 at 10:27 am

    Wow, fantastic blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is fantastic, as well as the content!

  2. pthinker 07/19/2011 at 1:31 pm

    I followed exactly like your step but still failed to install XML::LibXML module. I added DEBUG=1 when executing perl Makefile.pl and got the following error:

    /usr/bin/ld: cannot find -lz

    Any clue?

    Thanks,
    David

  3. pthinker 07/19/2011 at 2:03 pm

    I figured it out. I need to install libz library…

    • williamjxj 07/20/2011 at 11:43 pm

      Yeah, installing the Perl modules actually a process to deal with Linux environment, instead of Perl itself, such as the libz library installation (should be configure, gcc or make & install… which is original c or c++).
      If you are good at Linux itself, it would be a not big problem, just a little more time consume.

  4. maxtomic 08/13/2013 at 9:32 pm

    I’ve done all according to the recipe but still not happy:
    # find /usr/local/lib/libxml2.so
    /usr/local/lib/libxml2.so

    # perl Makefile.PL
    enable native perl UTF8
    Checking for ability to link against xml2…no
    Checking for ability to link against libxml2…libxml2, zlib, and/or the Math library (-lm) have not been found.
    Try setting LIBS and INC values on the command line
    Or get libxml2 from
    http://xmlsoft.org/
    If you install via RPMs, make sure you also install the -devel
    RPMs, as this is where the headers (.h files) are.

    +++++ -L/usr/local/lib -L/usr/lib -lxml2 -lm -lz ++++++

    Also, you may try to run perl Makefile.PL with the DEBUG=1 parameter
    to see the exact reason why the detection of libxml2 installation
    failed or why Makefile.PL was not able to compile a test program.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: