in reply to XML parsing
XML::Simple's name is misleading, it sounds like "a simple module for complete XML handling", but it's not - it's more like "simplistic XML handling for simple XML". I find it's great for reading simple XML config files that have been designed to work with XML::Simple, but it is not an all-purpose solution, and in your case is very likely not appropriate because, among several other things, it very often doesn't maintain an XML document's structure when reading a file and writing it back.
Anyway, I'm inclined to agree with the other monks' suggestions for XML::Twig, which is great when you want to process a file piece by piece. For the case you describe, if the file isn't so big that loading it into memory is too expensive, then XML::LibXML is fine too. For example, the following deletes the <bar> element if its child <quz>'s text content is "baz".
use warnings; use strict; use XML::LibXML; my $dom = XML::LibXML->load_xml(IO=>\*DATA); print "### Before:\n", $dom->toString; for my $el ($dom->findnodes('/foo/bar/quz')) { $el->textContent eq 'baz' and $el->parentNode->unbindNode; } print "### After:\n", $dom->toString; __DATA__ <foo> <bar> <quz>baz</quz> </bar> <x/> <bar> <quz>abc</quz> <y/> </bar> </foo>
Output:
### Before: <?xml version="1.0"?> <foo> <bar> <quz>baz</quz> </bar> <x/> <bar> <quz>abc</quz> <y/> </bar> </foo> ### After: <?xml version="1.0"?> <foo> <x/> <bar> <quz>abc</quz> <y/> </bar> </foo>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML parsing
by mading0 (Initiate) on Oct 03, 2014 at 15:35 UTC | |
by toolic (Bishop) on Oct 03, 2014 at 15:46 UTC | |
by mading0 (Initiate) on Oct 03, 2014 at 15:59 UTC | |
by Anonymous Monk on Oct 03, 2014 at 19:22 UTC |