Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

extracting attribute values which might have multiple occurences from xml file

by tinkuPerlMonk2 (Initiate)
on Aug 19, 2013 at 20:45 UTC ( #1050087=perlquestion: print w/ replies, xml ) Need Help??
tinkuPerlMonk2 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Experts, I need help in extracting some information from xml file using perl.

1) test.xml file

How can I extract hostname1.xxx.com(hostname) and key and env and then put it in one file ( a text file ) using perl scripts

Contents of xml file:

<httpsRoute hostname="abchostname1.xxx.com" port="443" environment="QA +"> <key>6924</key> </httpsRoute> <httpsRoute hostname="hostname3.xxx.com" port="7416" environment="CAT" +> <key>9990068</key> </httpsRoute>

Contents of text file should be:

Route #1 details:

Host=abchostname1.xxx.com

Environment=QA

key=6924

Route #2 details:

Host=hostname3.xxx.com

Environment=CAT

key=9990068

2) I'll have certficate.xml. Using perl how can i extract DN( subjectDN), CN, env and key and put them in a file using PERL Content of xml file:
<certDet env="QA" subjectDN="/C=US/ST=NC/L=Raleigh/O=DotNet/OU=ETD +;D/CN=test1.xxxx.com"> <key>1234</key> </certDet> <certDet env="dev" subjectDN="/C=US/ST=NC/L=Charlotte/O=xxxxx/OU=W +AS/CN=dev1.xxx.com"> <key>27166</key> </certDet>
Contents of files should be:

Entry1:

DN="/C=US/ST=NC/L=Raleigh/O=DotNet/OU=ET&D/CN=test1.xxxx.com"

CN=test1.xxxx.com

env=QA

key=1234

Entry2:

DN="/C=US/ST=NC/L=Charlotte/O=xxxxx/OU=WAS/CN=dev1.xxx.com"

CN=dev1.xxx.com

env=QA

key=1234

Comment on extracting attribute values which might have multiple occurences from xml file
Select or Download Code
Replies are listed 'Best First'.
Re: extracting attribute values which might have multiple occurences from xml file
by derekstucki (Sexton) on Aug 19, 2013 at 21:30 UTC

    I asked a similar question a while back, you might benefit from the answers I got HERE

      I'll go through the threads ( URL's ) you shared. Thank you derekstucki!!!
Re: extracting attribute values which might have multiple occurences from xml file
by choroba (Chancellor) on Aug 20, 2013 at 09:28 UTC
    Using XML::XSH2, a wrapper around XML::LibXML:
    open file.xml ; my $count = 0 ; for //certDet { $count = $count + 1 ; echo :s Entry $count ':' ; echo :s 'DN="' xsh:subst(@subjectDN, ';', '&') '"' ; echo :s CN= xsh:match(@subjectDN, 'CN=(.*)') ; echo :s env= @env ; echo :s key= (key) ; }
    The output is a bit different from what you posted:
    2c2 < DN="/C=US/ST=NC/L=Raleigh/O=DotNet/OU=ET&D/CN=test1.xxxx.com" --- > DN="/C=US/ST=NC/L=Raleigh/O=DotNet/OU=ETD&D/CN=test1.xxxx.com" 9,10c9,10 < env=QA < key=1234 --- > env=dev > key=27166
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks you choroba for your input!!!!!!
Re: extracting attribute values which might have multiple occurences from xml file
by poj (Parson) on Aug 20, 2013 at 09:07 UTC
    #!perl use strict; use warnings; use XML::Simple; use Data::Dump 'pp'; # input my $xml1 = infile('test.xml'); # process my $data = XMLin($xml1,keyattr => []); pp $data; # output for (@{$data->{'httpsRoute'}}){ print << "EOF"; Host = $_->{'hostname'} Environment = $_->{'environment'} Key = $_->{'key'} EOF } sub infile { my $filename = shift; local $/; #open IN,'<',$filename or die "Could not open $filename $!"; my $xml = <DATA>; # or IN return $xml; } __DATA__ <xml> <httpsRoute hostname="abchostname1.xxx.com" port="443" environment="QA +"> <key>6924</key> </httpsRoute> <httpsRoute hostname="hostname3.xxx.com" port="7416" environment="CAT" +> <key>9990068</key> </httpsRoute> </xml>
    poj
      thank you very much poj!. This helps to a great extent to get started for the folks like me who are starters of perl scripting. Thank you again very much!!!!!!!!!!
Re: extracting attribute values which might have multiple occurences from xml file
by Anonymous Monk on Aug 20, 2013 at 07:47 UTC

    Hello Experts,

    This is the ghost of perlmonks urging you to imagine

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050087]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2016-05-03 08:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?