Arenas has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

please how i can get singl node from URL?

I startet with:

#!/usr/bin/perl use strict; use warnings; use XML::Simple; my $filename = 'http://www.geocities.ws/arenas/xml/find.xml';
?????

============================= # start with powershell $xt = [string] "#text" $xmlUrl = "http://www.geocities.ws/arenas/xml/find.xml" $xWeb = New-Object System.Net.WebClient $xWeb.Encoding = [System.Text.Encoding]::UTF8 $xxml = $xWeb.DownloadString($xmlUrl) $xa = ([xml]$xxml).selectNode("find/find/work[@type="eu"]"/a).$xt # -f = force $xa_1 = "{0:## ###.###}" -f ([int]$xa * 2) # or moor complex $xval # end # now save to html $xpath = "C:\studio\xml\val.html" $Create = [system.IO.File]::CreateText($xpath) <# or $Create = [System.IO.File]::Exists($xpath) #> $Create.WriteLine("<!DOCTYPE .... # end html $Create.Flush() $Create.close() clear-variable x* -scope global

Thanks for your help
@r

..(\(°°)/)..

Replies are listed 'Best First'.
Re: Select Node
by poj (Abbot) on Feb 20, 2016 at 14:18 UTC

    From what I can decipher from your powershell code you want to extract the work node with attribute eu and write out to an html file.

    One way would be using XML::Twig::XPath to process the xml and HTML::Template to create the html file

    For example :

    #!perl use strict; use XML::Twig::XPath; use LWP::Simple; use HTML::Template; use Data::Dump 'pp'; # XML parser my $twig = XML::Twig::XPath->new( twig_roots => { '/find/find/work[@eu]' => \&work, }, ); # get and process xml my @rows = (); my $URL = 'http://www.geocities.ws/arenas/xml/find.xml'; #$twig->parse( *DATA ); #test with __DATA__ records $twig->parse( LWP::Simple::get( $URL ) ); pp \@rows; # prepare html template and parameters my $template = get_template(); my $t = HTML::Template->new( scalarref => \$template ); $t->param( EU => \@rows ); # create html open my $fh_out,'>','val.html' or die "$!"; print $t->output( print_to => $fh_out ); close $fh_out; # extract data from work node sub work { my ($twig,$el) = @_; for my $ch ($el->children){ my %rec = ( 'name' => $ch->name ); $rec{$_} = $ch->att($_) for ('a'..'f'); push @rows,\%rec; } }
    poj
      Hello Sir

      Many thank for you help!
      Great!
      Just one gestion: How do add mathe formula?

      Your scripte:

      twig_roots => { '/find/find/work[@eu]' Get the new value (exemple?): Selectet node : ??? [@eu] = a * 2 ???
      Is it possible to do also with the other items?

      I need sub chapter?
      Thanks!
      @r

      ..(\(°°)/)..

        You can build the search path using variables like this

        my $x = 2; my $y = $x * 2; my $xpath = "/find/find/work[\@eu='$y']"; print "Searching for $xpath\n"; my $twig = XML::Twig::XPath->new( twig_roots => { $xpath => \&work, }, );

        I don't see sub chapter in the XML

        poj