Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am looking for a good tool to perform grep-like operations on XML - for example, extract certain attributes only.

Grep itself can't handle it - any DFA-equivalent tool can handle only non recursive matches, and mine may be recursive.

I have tried xgrep, but it is quite unstable, and I want a stable and reliable tool.

Any recommendations?

EDIT: I prefer open source tools that work well under Linux.

share|improve this question
add comment

5 Answers

up vote 15 down vote accepted

xmlstarlet is a command line tool which comes close to grep. It is open source software (MIT license) and works well on linux and windows.

The xmlstarlet website http://xmlstar.sourceforge.net describes it as follows.

XMLStarlet is a set of command line utilities (tools) which can be used to transform, query, validate, and edit XML documents and files using simple set of shell commands in similar way it is done for plain text files using UNIX grep, sed, awk, diff, patch, join, etc commands.

The debian/ubuntu package is named xmlstarlet. But beware: Contrary to what the manpage says the binary is named xmlstarlet in debian/ubuntu and not xml.

There are also windows binaries on sourceforge.

For a nice little introduction see IBMs Start working with XMLStarlet.

share|improve this answer
    
Remove the trailing slash from the first link. –  Bkkbrad Aug 23 '09 at 20:27
1  
+1^, I was looking for such a tool for months! –  Doc Brown Apr 28 '10 at 11:35
    
I can't get it to work... It never matches on any xpath except '/' (the whole document), which is pretty worthless :( –  Hendy Irawan Jan 13 '12 at 16:29
    
@HendyIrawan - Are you sure it's not how you're trying to use xpath? (Like your XML has a default namespace that you're not accounting for?) –  Daniel Haley Mar 2 '12 at 16:15
add comment

The XPath syntax in various languages is best for finding things in xml. In fact one of the tools recommended by the makers of xgrep is basically a Perl XML parser that accepts XPath input.

share|improve this answer
add comment

A tool that works under Linux is xml_grep. It fully understands XML and is not a line-by-line tool.

xml_grep is included as a stand-alone tool in the XML::Twig package. The grepping functionality is quite powerful as it supports XPath specifications.

Sample command-line (extracting posts edited after the middle of February from the triology Data Dump):

xml_grep -p --cond="row[@LastEditDate>'2010-02-14']"  posts.xml  > lateEditedPosts.xml

Installation is easy. Either

  • sudo cpan -i "XML::Twig", as described in the xml_grep cookbook referenced below.

or


More information:

The best introduction I have found for xml_grep is xml_grep cookbook, about two pages. Other:

share|improve this answer
    
xmltwig.com (used in some of links above) may be broken now. On 2012-03-02, xmltwig.com could be pinged, but the web server did not respond (tracert indicates hosting in Italy, tiscali.it). –  Peter Mortensen Mar 2 '12 at 12:07
add comment

XMLSpy is an amazing tool, if a bit spendy.

share|improve this answer
add comment

I would advise NOT to use a grep-like tool on XML, but use a library to parse XML in stead.
What exactly do you need it for? Any programming language? I think the .NET built-in XML parser would fit the job easily if you're willing to write a program for it though.

Update: for Linux, a well known XML parser library is libxml2.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.