Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: XML parsing

by Anonymous Monk
on Oct 02, 2014 at 18:23 UTC ( [id://1102665]=note: print w/replies, xml ) Need Help??


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
    Thank you, this is really helpful. I have two questions about the code. First of all:  $el->textContent eq 'baz' Is there a way to say find the content that ISN'T baz? Is there a neq, basically? Secondly, is there any quick way through LibXML to print back into an XML file, or do I just need to use normal perl file output for that?
        Yes, figured that out shortly after I asked it, I thought for some reason it was using something special to the module. Ne works!
      Secondly, is there any quick way through LibXML to print back into an XML file, or do I just need to use normal perl file output for that?

      XML::LibXML::Document has the methods toFile and toFH, or you can use toString and print it to a file yourself.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1102665]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-28 18:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found