Is there a native tool for parsing xml files available on RedHat?

9

3

Are there any utilities similar to xpath for parsing XML files that would be natively available on a RedHat server?

Similar questions have been answered elsewhere, but none of the tools listed are on the server.

update: xmllint is installed, and man xmllint indicates that it can parse xml files, but it is not clear that this gives me the ability to extract a string from a specific node.

David LeBauer

Posted 2011-04-05T00:25:30.530

Reputation: 700

Answers

5

Try xmllint and the --xpath option:

<xml>
  <hello>world!</hello>
</xml>

$ xmllint --xpath '//hello/text()'
world!

lambshaanxy

Posted 2011-04-05T00:25:30.530

Reputation: 166

1Too bad that the "--xpath" option is only available on RedHat 7, which was not around when this question was posted. – chutz – 2015-01-03T14:50:53.970

Workaround for earlier versions of xmllint: http://stackoverflow.com/a/38199367/185196

– Joel Beckham – 2016-11-04T19:20:40.573

4

If, given this XML

$ cat a.xml
<a>
  <b>Hello</b>
  <b>World</b>
</a>

You want to be able to do

$ ./xpath //a/b a.xml
Hello
World

then you could just cut & paste this:

$ cat xpath
#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;

my $parser = XML::LibXML->new();
my $document = $parser->parse_file($ARGV[1]);
my @nodes = $document->findnodes($ARGV[0]);
for my $node (@nodes) {
  print $node->textContent, "\n";
}

You should be able to install the XML::LibXML module using perl -MCPAN -e 'install XML::LibXML'

RedGrittyBrick

Posted 2011-04-05T00:25:30.530

Reputation: 70 632

1Or just yum install 'perl(XML::LibXML)'. – Ignacio Vazquez-Abrams – 2011-04-05T14:28:57.490

4

xsltproc (command line interface to libxslt) is always available on RHEL.
usage: xsltproc xsl_stylesheet xml_file.

fpmurphy

Posted 2011-04-05T00:25:30.530

Reputation: 1 260

3

Ignacio Vazquez-Abrams

Posted 2011-04-05T00:25:30.530

Reputation: 100 516

0

On RHEL 7

yum install libxml2

gives you

xmllint

and it can parse XML files

# xmllint 
Usage : xmllint [options] XMLfiles ...
        Parse the XML files and output the result of the parsing

user7610

Posted 2011-04-05T00:25:30.530

Reputation: 336