Re: XML::Simple question
- Original Message - From: Jenda Krynicky je...@krynicky.cz To: Perl List beginners@perl.org Sent: Tuesday, December 22, 2009 6:44 PM Subject: Re: XML::Simple question From: Mike Blezien mick...@frontiernet.net Hello, were using the XML/Simple module to process a XML response using the code below. But for some reason the module can't read the $xmlresponse data unless we create a temp file first to store the data then pass that to the module. Is there a way to do this without having to create a temp file first. ?? Everything does work, the response is return. We used the Data/Dumper module to verify the data first. = my $ua = new LWP::UserAgent; $ua-timeout(10); $ua-agent(HTTP/1.1); my $req = new HTTP::Request 'POST' = $xmlscript; DO NOT QUOTE VARIABLES! If the $variable already contains a string, you just unnecessary make a copy. If it contains a number (well it can contain both, but I mean the case when it contains just the number), you force Perl to convert the number to string (and store that string alongside the number in the variable) and make a copy of the string. And possibly later it will have to convert the string back to number. And these are the cases when it actually works, even if it's inefficient. As soon as you quote like this a variable that contains a reference or an object (a reference bless()ed to a package), you end up with useles string, that will just look like a reference when printed out. Do not quote variables! my $req = new HTTP::Request 'POST' = $xmlscript; is enough! my $simple = new XML::Simple(KeyAttr=[]); # read XML file my $xmldata = $simple-XMLin($xmlresponse); OK, you are safe from automatic key related transformations ... what about tags with optional attributes? Or tags that are sometimes but not always repeated? See http://www.perlmonks.org/?node_id=697036 Jenda Jenda, thanks for the info, I nomarlly don't quote variables like that, just didn't catch that one. Mike(mickalo)Blezien =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Thunder Rain Internet Publishing http://www.thunder-rain.com/ =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
XML::Simple question
Hello, were using the XML/Simple module to process a XML response using the code below. But for some reason the module can't read the $xmlresponse data unless we create a temp file first to store the data then pass that to the module. Is there a way to do this without having to create a temp file first. ?? Everything does work, the response is return. We used the Data/Dumper module to verify the data first. = my $ua = new LWP::UserAgent; $ua-timeout(10); $ua-agent(HTTP/1.1); my $req = new HTTP::Request 'POST' = $xmlscript; $req-content_type(application/x-www-form-urlencoded); $req-content($xmlrequest); my $res = $ua-request($req); my $error = $res-is_error(); my $success = $res-is_success(); # Data returned in XML Format my $xmlresponse = $res-content(); my $simple = new XML::Simple(KeyAttr=[]); # read XML file my $xmldata = $simple-XMLin($xmlresponse); # can't read the response in this manner === Thanks and happy holidays, Mike(mickalo)Blezien =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Thunder Rain Internet Publishing http://www.thunder-rain.com/ =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: XML::Simple question
On Tue, Dec 22, 2009 at 9:54 AM, Mike Blezien mick...@frontiernet.netwrote: were using the XML/Simple module to process a XML response using the code below. But for some reason the module can't read the $xmlresponse data unless we create a temp file first to store the data then pass that to the module. Is there a way to do this without having to create a temp file first. ?? Everything does work, the response is return. We used the Data/Dumper module to verify the data first. = my $ua = new LWP::UserAgent; $ua-timeout(10); $ua-agent(HTTP/1.1); my $req = new HTTP::Request 'POST' = $xmlscript; $req-content_type(application/x-www-form-urlencoded); $req-content($xmlrequest); my $res = $ua-request($req); my $error = $res-is_error(); my $success = $res-is_success(); # Data returned in XML Format my $xmlresponse = $res-content(); my $simple = new XML::Simple(KeyAttr=[]); # read XML file my $xmldata = $simple-XMLin($xmlresponse); # can't read the response in this manner === Try the parse_string method instead. The documentation says that XMLin guesses based on the presence of and characters. It appears that XMLin guesses wrong in your case. parse_string explicitly expects an XML string. So there's no logic to confuse. -- Robert Wohlfarth
Re: XML::Simple question
- Original Message - From: Robert Wohlfarth rbwohlfa...@gmail.com To: Perl List beginners@perl.org Sent: Tuesday, December 22, 2009 5:47 PM Subject: Re: XML::Simple question On Tue, Dec 22, 2009 at 9:54 AM, Mike Blezien mick...@frontiernet.netwrote: were using the XML/Simple module to process a XML response using the code below. But for some reason the module can't read the $xmlresponse data unless we create a temp file first to store the data then pass that to the module. Is there a way to do this without having to create a temp file first. ?? Everything does work, the response is return. We used the Data/Dumper module to verify the data first. = my $ua = new LWP::UserAgent; $ua-timeout(10); $ua-agent(HTTP/1.1); my $req = new HTTP::Request 'POST' = $xmlscript; $req-content_type(application/x-www-form-urlencoded); $req-content($xmlrequest); my $res = $ua-request($req); my $error = $res-is_error(); my $success = $res-is_success(); # Data returned in XML Format my $xmlresponse = $res-content(); my $simple = new XML::Simple(KeyAttr=[]); # read XML file my $xmldata = $simple-XMLin($xmlresponse); # can't read the response in this manner === Try the parse_string method instead. The documentation says that XMLin guesses based on the presence of and characters. It appears that XMLin guesses wrong in your case. parse_string explicitly expects an XML string. So there's no logic to confuse. -- Robert Wohlfarth Thanks Robert, that did the trick :) Mike(mickalo)Blezien =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Thunder Rain Internet Publishing Custom Programming Web Hosting Services http://www.thunder-rain.com/ =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: XML::Simple question
From: Mike Blezien mick...@frontiernet.net Hello, were using the XML/Simple module to process a XML response using the code below. But for some reason the module can't read the $xmlresponse data unless we create a temp file first to store the data then pass that to the module. Is there a way to do this without having to create a temp file first. ?? Everything does work, the response is return. We used the Data/Dumper module to verify the data first. = my $ua = new LWP::UserAgent; $ua-timeout(10); $ua-agent(HTTP/1.1); my $req = new HTTP::Request 'POST' = $xmlscript; DO NOT QUOTE VARIABLES! If the $variable already contains a string, you just unnecessary make a copy. If it contains a number (well it can contain both, but I mean the case when it contains just the number), you force Perl to convert the number to string (and store that string alongside the number in the variable) and make a copy of the string. And possibly later it will have to convert the string back to number. And these are the cases when it actually works, even if it's inefficient. As soon as you quote like this a variable that contains a reference or an object (a reference bless()ed to a package), you end up with useles string, that will just look like a reference when printed out. Do not quote variables! my $req = new HTTP::Request 'POST' = $xmlscript; is enough! my $simple = new XML::Simple(KeyAttr=[]); # read XML file my $xmldata = $simple-XMLin($xmlresponse); OK, you are safe from automatic key related transformations ... what about tags with optional attributes? Or tags that are sometimes but not always repeated? See http://www.perlmonks.org/?node_id=697036 Jenda = je...@krynicky.cz === http://Jenda.Krynicky.cz = When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
xml simple question
Hi All. When using XML::Sample, I have a question, who can help me. First I have output.xml file: ?xml version=1.0? memo applications multimedia applicationMp3 converter/application applicationReal player/application /multimedia office applicationMicrosoft project/application applicationPoedit/application /office /applications games gameMetrix/game gameLine/game gameSudoku/game /games /memo I use XML::Sample to read, after that I save to output.xml $data= XMLin(input.xml); print XMLout($data, xmldecl = '?xml version=1.0?', RootName = 'memo', OutputFile = 'output.xml'); Content of output.xml file: ?xml version=1.0? config applications namemultimedia/name !-- different -- applicationMp3 converter/application applicationReal player/application /applications applications nameoffice/name!-- different -- applicationMicrosoft project/application applicationPoedit/application /applications games gameMetrix/game gameLine/game gameSudoku/game /games /config The content of 2 files output.xml and input.xml are different. How can I create the output file its content the same with input.xml. Cheers -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: xml simple question
getget wrote: The content of 2 files output.xml and input.xml are different. How can I create the output file its content the same with input.xml. Use a different parser than XML::Simple. I suggest XML::Twig. XML::Simple does not distinguish between attributes and content. Because of this, it does not reproduce XML accurately. It's good if all you want to do is read XML but if use want to write it, use something different. -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: xml simple question
On Friday 23 Oct 2009 15:16:15 Shawn H Corey wrote: getget wrote: The content of 2 files output.xml and input.xml are different. How can I create the output file its content the same with input.xml. Use a different parser than XML::Simple. I suggest XML::Twig. XML::Simple does not distinguish between attributes and content. Because of this, it does not reproduce XML accurately. It's good if all you want to do is read XML but if use want to write it, use something different. I can second that. XML::Simple has inherent philosophical design problems and I never use it or waste my time to support people using it. Risking making the use qmail instead syndrome (see: http://www.shlomifish.org/philosophy/computers/web/use-qmail-instead/ ), I hereby tell you - don't use XML::Simple if you value your sanity. It does not truly handle XML and is anything but simple. Regards, Shlomi Fish, who now maintains XML-RSS which has many XML-Simple'isms, despite the fact that it doesn't use it, and that some people want even more XML-Simple'isms to support the various brain-damaged RSS extensions that one can find in the wild. -- - Shlomi Fish http://www.shlomifish.org/ Rethinking CPAN - http://shlom.in/rethinking-cpan Chuck Norris read the entire English Wikipedia in 24 hours. Twice. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: XML::Simple question
Richard Lee schreef: how do I parse out image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif; width=145 height=190 / I tried $book-{image}-{src}... but doesn't work.. use XML::Simple qw(:strict); my $library = XMLin($filename, ForceArray = 1, KeyAttr= {}, ); foreach my $book (@{$library-{book}}) { print $book-{title}-[0], \n } XML file library book titlePerl Best Practices/title authorDamian Conway/author [...] /book /library You can easily convert the above code to a small-but-complete Perl script, for example with the XML-data in the __DATA__ section, and use Data::Dumper at several stages to show you the data structures. -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: XML::Simple question
Richard Lee wrote: while trying to study the article on perlmonks.org, http://perlmonks.org/?node_id=490846 regarding XML parsing, I need bit of clarfication. how do I parse out image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif; width=145 height=190 / I tried $book-{image}-{src}... but doesn't work.. I need some understanding on how these information is stored. parsing code use XML::Simple qw(:strict); my $library = XMLin($filename, ForceArray = 1, KeyAttr= {}, ); foreach my $book (@{$library-{book}}) { print $book-{title}-[0], \n } XML file library book titlePerl Best Practices/title authorDamian Conway/author isbn0596001738/isbn pages542/pages image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif; width=145 height=190 / /book book titlePerl Cookbook, Second Edition/title authorTom Christiansen/author authorNathan Torkington/author isbn0596003137/isbn pages964/pages image src=http://www.oreilly.com/catalog/covers/perlckbk2.s.gi +f width=145 height=190 / /book book titleGuitar for Dummies/title authorMark Phillips/author authorJohn Chappell/author isbn076455106X/isbn pages392/pages image src=http://media.wiley.com/product_data/coverImage/6X/07 +645510/076455106X.jpg width=100 height=125 / /book /library I agree with Stewart that XML::Simple is far from simple in practice. For the selection of options for XMLin you have used, you can access the image data like this: foreach my $book (@{$library-{book}}) { my $title = $book-{title}[0]; my $image = $book-{image}[0]; print $title\n; print $image-{src}\n; print $image-{width}\n; print $image-{height}\n; } but the structure of the data will change depending on what options are set, and in general it is very difficult to use XML::Simple without also using Data::Dumper to inspect the data structure that has actually been generated. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: XML::Simple question
Rob Dixon wrote: Richard Lee wrote: while trying to study the article on perlmonks.org, http://perlmonks.org/?node_id=490846 regarding XML parsing, I need bit of clarfication. how do I parse out image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif; width=145 height=190 / I tried $book-{image}-{src}... but doesn't work.. I need some understanding on how these information is stored. parsing code use XML::Simple qw(:strict); my $library = XMLin($filename, ForceArray = 1, KeyAttr= {}, ); foreach my $book (@{$library-{book}}) { print $book-{title}-[0], \n } XML file library book titlePerl Best Practices/title authorDamian Conway/author isbn0596001738/isbn pages542/pages image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif; width=145 height=190 / /book book titlePerl Cookbook, Second Edition/title authorTom Christiansen/author authorNathan Torkington/author isbn0596003137/isbn pages964/pages image src=http://www.oreilly.com/catalog/covers/perlckbk2.s.gi +f width=145 height=190 / /book book titleGuitar for Dummies/title authorMark Phillips/author authorJohn Chappell/author isbn076455106X/isbn pages392/pages image src=http://media.wiley.com/product_data/coverImage/6X/07 +645510/076455106X.jpg width=100 height=125 / /book /library I agree with Stewart that XML::Simple is far from simple in practice. For the selection of options for XMLin you have used, you can access the image data like this: foreach my $book (@{$library-{book}}) { my $title = $book-{title}[0]; my $image = $book-{image}[0]; print $title\n; print $image-{src}\n; print $image-{width}\n; print $image-{height}\n; } but the structure of the data will change depending on what options are set, and in general it is very difficult to use XML::Simple without also using Data::Dumper to inspect the data structure that has actually been generated. HTH, Rob thanks all! Will try few others now. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
XML::Simple question
while trying to study the article on perlmonks.org, http://perlmonks.org/?node_id=490846 regarding XML parsing, I need bit of clarfication. how do I parse out image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif; width=145 height=190 / I tried $book-{image}-{src}... but doesn't work.. I need some understanding on how these information is stored. parsing code use XML::Simple qw(:strict); my $library = XMLin($filename, ForceArray = 1, KeyAttr= {}, ); foreach my $book (@{$library-{book}}) { print $book-{title}-[0], \n } XML file library book titlePerl Best Practices/title authorDamian Conway/author isbn0596001738/isbn pages542/pages image src=http://www.oreilly.com/catalog/covers/perlbp.s.gif; width=145 height=190 / /book book titlePerl Cookbook, Second Edition/title authorTom Christiansen/author authorNathan Torkington/author isbn0596003137/isbn pages964/pages image src=http://www.oreilly.com/catalog/covers/perlckbk2.s.gi +f width=145 height=190 / /book book titleGuitar for Dummies/title authorMark Phillips/author authorJohn Chappell/author isbn076455106X/isbn pages392/pages image src=http://media.wiley.com/product_data/coverImage/6X/07 +645510/076455106X.jpg width=100 height=125 / /book /library -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
xml simple question
I am trying to write a script to aggregate diverse xml documents into tables. The trick is that i dont know the exact stucture of the xml files in advance. I have a stripped down version of the script I am trying to create that I will include. The preprocessing part of the script is trying to create arrays of all of the possible child nodes, in case one form has more than the first. Then the proccessing part loops through and exports the values. I need to lose the dependence on the hard-coding of these nodes ($rd=$data-{RequiredData}, etc). I basically want every bit of data from every node in Data. Any ideas on how to do that? I am truely stuck. Thank you for your time and help. ~BJ #!/usr/bin/perl # use module use XML::Simple; use Time::Local; print Welcome to the xml aggregator\n; print *\n\n; print Enter the name of the directory to parse\n; $dir=STDIN; chomp $dir; print enter the name of the output file\n; $ofile=STDIN; chomp $ofile; @rdkeys=; @wskeys=; @dmkeys=; print Beginning preprocessing\n; # start preprocessor ($Second1) = localtime(time) ; opendir(C, $dir); my @ff = readdir(C); closedir(C); foreach my $file (@ff) { my $filename = $dir . '/' . $file; if ($file eq '.' || $file eq '..') { } else { # create object $xml = new XML::Simple; # read XML file $data = $xml-XMLin($filename); # extract keys, remove duplicates $rd=$data-{RequiredData}; $ws=$data-{WorksheetData}; $dm=$data-{DMan}; @keys=keys(%$rd); push(@rdkeys,@keys); %seen = (); @rdkeys = grep { ! $seen{$_} ++ } @rdkeys; @keys=keys(%$ws); push(@wskeys,@keys); %seen = (); @wskeys = grep { ! $seen{$_} ++ } @wskeys; @keys=keys(%$dm); push(@dmkeys,@keys); %seen = (); @dmkeys = grep { ! $seen{$_} ++ } @dmkeys;}} ($Second2) = localtime(time) ; $diff= ($Second2-$Second1); $diff=$diff/60; print Beginning proccessing, estimated time $diff min\n; #actual processing open(OUT, $ofile); $x=0; @rdkeys=grep $_ ne ,@rdkeys; foreach $item(@rdkeys){print OUT $item|}; @wskeys=grep $_ ne ,@wskeys; foreach $item(@wskeys){print OUT $item|}; @dmkeys=grep $_ ne ,@dmkeys; foreach $item(@dmkeys){print OUT $item|}; print OUT \n; # read XML file foreach my $file (@ff) { my $filename = $dir . '/' . $file; if ($file eq '.' || $file eq '..') { } else { $data = $xml-XMLin($filename); # extract keys, remove duplicates $rd=$data-{RequiredData}; $ws=$data-{WorksheetData}; $dm=$data-{DMan}; @keys=keys(%$rd); foreach $item(@rdkeys){ print OUT $rd-{$item}|;} @keys=keys(%$ws); foreach $item(@wskeys){ print OUT $ws-{$item}|;} @keys=keys(%$dm); foreach $item(@dmkeys){ print OUT $dm-{$item}|;} print OUT \n; }} close OUT; print All operations completed. File Exported to $ofile\n; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: xml simple question
On Fri, 15 Apr 2005, BJ wrote: I am trying to write a script to aggregate diverse xml documents into tables. The trick is that i dont know the exact stucture of the xml files in advance. [] I basically want every bit of data from every node in Data. Any ideas on how to do that? I am truely stuck. Thank you for your time and help. ~BJ In other words, this may be a job for XPath: [] The primary purpose of XPath is to address parts of an XML document. In support of this primary purpose, it also provides basic facilities for manipulation of strings, numbers and booleans. XPath uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values. XPath operates on the abstract, logical structure of an XML document, rather than its surface syntax. XPath gets its name from its use of a path notation as in URLs for navigating through the hierarchical structure of an XML document. http://www.w3.org/TR/xpath The main Perl module for this is XML::XPath: This module aims to comply exactly to the XPath specification at http://www.w3.org/TR/xpath and yet allow extensions to be added in the form of functions. Modules such as XSLT and XPointer may need to do this as they support functionality beyond XPath. http://search.cpan.org/dist/XML-XPath/XPath.pm There is a family of related modules as well: http://search.cpan.org/dist/XML-XPath/ Depending on what you want to do with the XML data, XSLT may also help: [A] language for transforming XML documents into other XML documents. http://www.w3.org/TR/xslt It, too, has a Perl module interface: http://search.cpan.org/dist/XML-XSLT/lib/XML/XSLT.pm I suspect that XPath and XSLT may be the tools you're looking for ... -- Chris Devers -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: xml simple question
Thank you very much, I will look into xpath and xlst. :-) Chris Devers wrote: On Fri, 15 Apr 2005, BJ wrote: I am trying to write a script to aggregate diverse xml documents into tables. The trick is that i dont know the exact stucture of the xml files in advance. [] I basically want every bit of data from every node in Data. Any ideas on how to do that? I am truely stuck. Thank you for your time and help. ~BJ In other words, this may be a job for XPath: [] The primary purpose of XPath is to address parts of an XML document. In support of this primary purpose, it also provides basic facilities for manipulation of strings, numbers and booleans. XPath uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values. XPath operates on the abstract, logical structure of an XML document, rather than its surface syntax. XPath gets its name from its use of a path notation as in URLs for navigating through the hierarchical structure of an XML document. http://www.w3.org/TR/xpath The main Perl module for this is XML::XPath: This module aims to comply exactly to the XPath specification at http://www.w3.org/TR/xpath and yet allow extensions to be added in the form of functions. Modules such as XSLT and XPointer may need to do this as they support functionality beyond XPath. http://search.cpan.org/dist/XML-XPath/XPath.pm There is a family of related modules as well: http://search.cpan.org/dist/XML-XPath/ Depending on what you want to do with the XML data, XSLT may also help: [A] language for transforming XML documents into other XML documents. http://www.w3.org/TR/xslt It, too, has a Perl module interface: http://search.cpan.org/dist/XML-XSLT/lib/XML/XSLT.pm I suspect that XPath and XSLT may be the tools you're looking for ... -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response