Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How can I keep or discard certain blocks of an XML file based on first line of block?

by Anonymous Monk
on Jun 26, 2014 at 11:38 UTC ( [id://1091340]=note: print w/replies, xml ) Need Help??


in reply to How can I keep or discard certain blocks of an XML file based on first line of block?

Have a look at XML::Twig, especially the section "XML::Twig 101". That has code examples and should get you started on how to parse the XML file, select the elements to handle, and only print those that you want to the output.

As for parsing the date, here's one of several ways to do that, using the DateTime and DateTime::Format::Strptime modules.

use DateTime; use DateTime::Format::Strptime; my $strp = DateTime::Format::Strptime->new(on_error=>'croak', pattern => '%Y%m%d%H%M%S %z'); my $input = "20140623203000 -0400"; my $dt = $strp->parse_datetime($input); print $dt->iso8601, "\n"; print "Weekday: ", $dt->day_of_week, " (", $dt->day_name, ")\n";

Another thing to note: In your description you talk about line-based parsing, which is a dangerous thing with XML. Many, many applications that work with XML don't consider some or all whitespace, including newlines, to be significant. That means that you may suddenly find two tags that were previously on two lines to show up on the same line, or have a tag and its attributes split over several lines, etc. That's one of the reasons XML should always be parsed with a real XML parser. Fortunately, there are lots available, with lots of different interfaces to suit different applications and different coding styles.

  • Comment on Re: How can I keep or discard certain blocks of an XML file based on first line of block?
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-18 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found