9

I know that any language is capable of parsing XML; I'm really just looking for advantages or drawbacks that you may have come across in your own experiences. Perl would be my standard go to here, but I'm open to suggestions.

Thanks!

UPDATE: I ended up going with XML::Simple which did a nice job, but I have one piece of advice if you plan to use it--research the forcearray option first. I had to rewrite a bunch of statements after learning that it is usually best practice to set forcearray. This page had the clearest explanation that I could find. Frankly, I'm surprised this isn't the default behavior.

3
  • It mostly depends on what you need to do besides parsing XML, and probably also on your being comfortable with such or such language. Both Python and C# have great support for parsing XML and are very flexible, C++ also has good libraries to do that, although the language in itself is more strict and will ask for more careful planning (not necessarily a bad thing).
    – RedGlyph
    Commented Oct 23, 2009 at 12:26
  • You're already asking about a proper parser, so disregard the warnings and talks about using regular expressions on this page, but at the bottom, it gives parser suggestions for a variety of languages: htmlparsing.icenine.ca
    – genio
    Commented Oct 23, 2009 at 12:54
  • 3
    I guess I am the only one who thinks this question has no one right answer without further specific criteria. Commented Oct 23, 2009 at 14:02

12 Answers 12

10

If you are using Perl then I would recommend XML::Simple:

As more and more Web sites begin using XML for their content, it's increasingly important for Web developers to know how to parse XML data and convert it into different formats. That's where the Perl module called XML::Simple comes in. It takes away the drudgery of parsing XML data, making the process easier than you ever thought possible.

2
  • 4
    XML::Simple is acceptable sometimes, but when it comes to complex, strictly-formatted data, it's often more trouble than it's worth. I would try XML::Twig or XML::LibXML instead.
    – hobbs
    Commented Oct 23, 2009 at 13:03
  • 1
    Or when XML is large - using ANY DOM parser, including XML::Simple, is a Very Bad Idea.
    – Aye
    Commented Oct 23, 2009 at 20:28
10

XML::Twig is very nice, especially because it’s not as awfully verbose as some of the other options.

2
  • 5
    A second for XML::Twig, specially if you have to handle enormous data sets.
    – squeeks
    Commented Oct 23, 2009 at 12:25
  • 1
    XML::Twig allows processing XML in mixed mode. Commented Oct 23, 2009 at 18:31
7

For pure XML parsing, I wouldn't use Java, C#, C++, C, etc. They tend to overcomplicate things, as in you want a banana and get the gorilla with it as well.

Higher-level and interpreted languages such as Perl, PHP, Python, Groovy are more suitable. Perl is included in virtually every Linux distro, as is PHP for the most part.

I've used Groovy recently for especially this and found it very easy. Mind you though that a C parser will be orders of magnitude faster than Groovy for instance.

6
  • 2
    Parsing XML is C# is straightforward, what have you used to report such a bad experience? Was it with something else than the standard libraries? And for the record, I would hardly place Perl and PHP as "higher language" in comparison, they are not true fully-fledgd object-oriented languages.
    – RedGlyph
    Commented Oct 23, 2009 at 12:21
  • 1
    I've used Mono (.net 2.0 compatible). And I said higher-level language, not higher, do some Googling about to see what that means.
    – NT_
    Commented Oct 23, 2009 at 12:23
  • Ah, your sentence looked like a comparative but you simply meant high-level language then - And no, I don't usually use Google or Wikipedia to check word definitions but I understand it's a common mistake others often do ;-) In any case, System.Xml is also in Mono and I didn't find anything overcomplicated about it. That just emphasizes another important criterion is that one must feel at ease with the programming language.
    – RedGlyph
    Commented Oct 23, 2009 at 15:28
  • @RedGlyph: Nothing much wrong with it -you can get work done. Just more complicated than the others I mention.
    – NT_
    Commented Oct 23, 2009 at 15:59
  • 1
    RedGlyph: Look at Moose for Perl. Commented Oct 24, 2009 at 9:45
6

It's all going to be in the libraries.

Python has great libraries for XML. My preference is lxml. It uses libxml/libxslt so it's fast, but the Python binding make it really easy to use. Perl may very well have equally awesome OO libraries.

4

I saw that people recommend XML::Simple if you decide on Perl.

While XML::Simple is, indeed, very simple to use and great, is a DOM parser. As such, it is, sadly, completely unsuitable to processing large XML files as your process would run out of memory (it's a common problem for any DOM parser, not limited to XML::Simple or Perl).

So, for large files, you must pick a SAX parser in whichever language you choose (there are many XML SAX parsers in Perl, or use another stream parser like XML::Twig that is even better than standard SAX parser. Can't speak for other languages).

1
  • actually XML::Twig is (emphatically!) NOT SAX. It is designed to be easier to use that SAX, while still being stream-oriented.
    – mirod
    Commented May 12, 2011 at 8:44
3

Not exactly a scripting language, but you could also consider Scala. You can start from here.

0
3

Scala's XML support is rather good, especially as XML can just be typed directly into Scala programs.

Microsoft also did some cool integrated stuff with their LINQ for XML

But I really like Elementtree and just that package alone is a good reason to use Python instead of Perl ;)

Here's an example:

import elementtree.ElementTree as ET

# build a tree structure
root = ET.Element("html")

head = ET.SubElement(root, "head")

title = ET.SubElement(head, "title")
title.text = "Page Title"

body = ET.SubElement(root, "body")
body.set("bgcolor", "#ffffff")

body.text = "Hello, World!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)
tree.write("page.xhtml")
2

It's not a scripting language, but Scala is great for working with XML natively. Also, see this book (draft) by Burak.

1

Python has some pretty good support for XML. From the standard library DOM packages to much more 'pythonic' libraries that parse XML directly into more usable object structures.

There isn't really a 'right' language though... there are good XML packages for most languages nowadays.

1

If you're going to use Ruby to do it then you're going to want to take a look at Nokogiri or Hpricot. Both have their strengths and weaknesses. The language and package selection really comes down to what you want to do with the data after you've parsed it.

0

Reading Data out of XML files is dead easy with C# and LINQ to XML!

Somehow, although I really love python, I found it hard to parse XML with the standard libraries.

0

I would say it depends like everything else. VB.NET 2008 uses XML literals, has IntelliSense for LINQ to XML, and a few power toys that help turn XML into XSD. So personally, if you are working in a .NET environment I think this is the best choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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