Re: Need help

Tod Harter <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
Right. Well, you probably have one of the faster solutions. Honestly if I
were not going to do further processing on the input I'd stick with what you
have. It's not the most elegant solution but it works!

You could use LibXML and the DOM API, or even LibXSLT and an XSLT transform.
LibXML has various functions that can be used, like XPath etc. For a small
file like this though it may be like squashing an ant with a sledge hammer.
Just loading one of those modules is likely going to require a lot more
memory and CPU and disk I/O than the regex solution. It may have the virtue
of some reliability though in case you encounter a correct but differently
formatted document or something like that. If you need to go on and do more
complex stuff it may be better as well.

There are also a bunch of other XML APIs that perl supports. I suspect most
any of them can handle this task pretty easily. XML::Twig is a pretty easy
one to use, generally speaking for instance.

On Mon, Jun 21, 2010 at 9:22 AM, Chaitanya Yanamadala <
[email protected]> wrote:

>
> actually i have done it this way.
>
> $outputfile=shift;
> open(MYINPUT, "$outputfile"); # open for input
> my(@lines1) = <MYINPUT>; # read file into list
> my $value=5;
> foreach $line_new (@lines1) # loop thru list
> {
>     if ($line_new =~ /HN_article/)
>     {
>         my ($value) = $line_new =~ /value="(.*?)"/;
>         print $transaction."\n";
>         my ($note) = $line_new =~ /note="(.*?)"/;
>         print $reel_id."\n";
>
>     if($value ne '' && $note ne '')
>     {
>         $xml_text .= '<a value"'.$value.'"note="'.$note.'"
> tape_id="'.$value.'">';
>     }
>     else
>     {
>         $xml_text .= $line_new;
>     }
>     }
>     else{
>          $xml_text .= $line_new;
>     }
> }
> close MYINPUT;
> $outputfile_new="test.xml";
> open(MYOUTPUT,">$outputfile_new") or die $!;
> print MYOUTPUT $xml_text;
> close MYOUTPUT;
> use XML::DOM;
>
> my $file = "test.xml";
>
> # Read and parse the document
> my $parser = new XML::DOM::Parser;
> my $doc = $parser->parsefile ($file) or die $!;
>
> #print $xml_text;
>
>
> This is giving me my required output, but i want to do it using a XML
> module, i dont know which one to use.
>
> Chaitanya
>
>
>
>
> On Mon, Jun 21, 2010 at 6:34 PM, Tod Harter <
> [email protected]> wrote:
>
>> Which XML library are you using?
>>
>> On Mon, Jun 21, 2010 at 6:05 AM, Chaitanya Yanamadala <
>> [email protected]> wrote:
>>
>>> Hai
>>>  I have a small query regarding to xml reading and writng..
>>>
>>> i have an xml like this
>>> <a value="5" note=2 >
>>> <b><c>hello</c></b>
>>> </a>
>>>
>>> now i need to add a new attribute to node a.
>>>
>>> can any one help me how to do it..
>>>
>>> Chaitanya
>>>
>>>
>>> _______________________________________________
>>> Perl-XML mailing list
>>> [email protected]
>>> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>>>
>>>
>>
>>
>> --
>> The Wise adapts himself to the world. The Fool adapts the world to
>> himself. Therefore, all progress depends on the Fool.
>>
>
>


-- 
The Wise adapts himself to the world. The Fool adapts the world to himself.
Therefore, all progress depends on the Fool.

_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.