In xml file, & I have to substitute in <cc>Some text </cc>
So when i read the file, the <cc> tag is with other tags also. Even there also & is replaced with '-'.
In the example below:
if($line =~ /<cc>([\s\S]+?)<\/cc>/){
#$line = $1;
$line =~ s/&amp;/-/g;
$line =~ s/&/-/g;
$line =~ s/-$//g;
}
if I assign $line to $1, then in the output rest of the strings are removed and only $1 is visible. | [reply] [d/l] |
This seems to be a new problem, only partly related to your original post. I'm sorry, but I don't understand what your current problem is. Please provide a self-contained program and the input and output data, and what you expect. Please also put all your &, & and &amp; in between <code>...</code> tags, so we can understand when you mean & and when you mean &.
Judging from your description, my imagination is that you want to replace & with - except within <cc>...</cc> tags. If that's true, then regular expressions are the wrong tool for the general case, and you should use an XML parser like XML::LibXML or XML::Twig.
| [reply] [d/l] [select] |
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'library.xml';
use XML::LibXML;
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($filename);
#my $book;
foreach my $title ($doc->findnodes('/library/book/title')) {
my $name= $title->to_literal ;
print $name;
}
Can I print just the title without giving "'/library/book/title'".
There are many XML files and each have a different structure. How can I restrict to find the only the <cc> tag and get the value inside it.
| [reply] [d/l] |