Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

As others have already mentioned, simple regular expressions aren't the best tool for extracting data from SGML documents, but there are other parts of your code that are interesting and could be improved.

After I wrote this I refreshed the page and noticed that a few others had already done the same, but I think there are a few changes and comments here that aren't covered in the other posts. Sorry if I'm redundant.

This code compiles but has not been tested.

#!/usr/bin/perl use warnings; use strict; # if the argument to die ends with newline, die # won't print the line number: # open (READ, "test.xml") || die "ERROR: $!\n"; # you probably want to know which file the error # relates to and the mode (read or write): open READ, "< test.xml" or die "ERROR opening test.xml for reading: $! +"; # NOTE that we use 'or', a lower-precedence logical # operator so that we can leave off the parentheses # around the arguments to open() my @array = <READ>; # close can fail. if it does, you probably want to know # about it: close READ or die "ERROR closing READ filehandle: $!"; # see above comments # open (WRITE, ">new.xml") || die "ERROR: $!\n"; open WRITE, "> new.xml" or die "ERROR creating new.xml: $!"; # make WRITE the default filehandle select WRITE; foreach (@array) { # '<' and '!' are not meta-characters so there's no # need to escape them: if (/<!-- Testing XML -->/) { # use a here-document (see the perlop man page) print <<_EOF_; <bar> <name> TEST </name> <type> Foo </type> <!-- PDP Status --> <unknown_sec> 0 </unknown_sec> </bar> _EOF_ } # in /PATTERN/, the '/' character is your delimiter. # if the pattern delimiter occurs in the pattern, # instead of escaping the delimiter, use a different # delimiter with the explicit m operator. here we # use '!' as our delimiter: if (m!</ Test Tag>!) { print "<bar><value> TEST </value></bar>\n"; } # \n$ : this is redundant. the $ anchor matches an optional newlin +e if (m!</v></row>$!) { # note the use of '@' instead of '/' as the delimiter: s@</row>$@@; $_ .= "<v> UnKnown </v></row>\n"; print; # prints $_ by default } else { print; } } close WRITE or die "ERROR closing WRITE filehandle: $!";

In reply to Re: Code efficiency ? by converter
in thread Efficiently inserting XML data by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found