Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

XML Examples (and a call for participation)

by mirod (Canon)
on Nov 19, 2002 at 10:18 UTC ( #214066=perlmeditation: print w/replies, xml ) Need Help??

Parsing XML??? is amongst several posts I have seen on PerlMonks asking how to extract information from an XML file.

To have an idea on how to perform that kind of common task, you might want to have a look at Ways to Rome, an article I maintain that lists a whole bunch (16 at the time of writing) of ways to do this kind of extraction/reporting.

And please, _DO NOT USE THE REGEXP METHOD_ (was that strong enough?). For your own good.

Note that the article does not include XML::Simple, as the XML document (the XML Spec itself) includes mixed content (and no, I did not choose the document on purpose, in fact I inherited the article from Ingo Macherius ;--). If XML::Simple can process your document, then by all means use it.

Contributions are welcome BTW: a version using an as-generic-as-possible entity pre-processor, or a patch to the module, would allow me to include XML::SAX::PurePerl for example, XML::GDOME and XML::SAX::Machines, or princepawn's favorite could also be added.

  • Comment on XML Examples (and a call for participation)

Replies are listed 'Best First'.
Re: XML Examples (and a call for participation)
by PodMaster (Abbot) on Nov 19, 2002 at 14:41 UTC
    I love XML::Twig, but here's what I still do with HTML, and did with XML before I learned about XML::Twig ;)
    use XML::TokeParser; my $file = 'REC-xml-19980210.xml'; my $i = 0; my $p = XML::TokeParser->new($file); my $Ret = ""; while(defined(my $t = $p->get_token() )){ if($t->[0] eq 'S' and $t->[1] eq 'lhs') { $i++; $Ret = join '', "[$i] ", $p->get_text('/lhs'), " ::= "; }elsif( $t->[0] eq 'S' and $t->[1] eq 'rhs'){ $Ret .= $p->get_text('/rhs'); }elsif($t->[0] eq 'E' and $t->[1] eq 'prod'){ print clean($Ret),"\n"; $Ret = ""; } } undef $Ret; undef $p; ## mirod already did this, so I'm borrowing sub prod { my( $twig, $prod)= @_; my $lhs= $prod->field( 'lhs'); my $rhs= join '', map {$_->text} $prod->children( 'rhs'); $i++; my $prod_text = "[$i] $lhs ::= $rhs"; print clean( $prod_text) . "\n"; } sub clean { my( $string)= @_; $string =~ s/\xc2\xa0/ /sg; $string =~ s/\s+/ /g; $string=~ s{\s$}{}g; return $string; }

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      Excellent, PodMaster++.

      I did not even know that XML::TokeParser existed, but the code is pretty simple, I like it.

      The code (minus the prod function which is not needed) has been added to the article. That's now a lot more than one way to do it (17!)

Re: XML Examples (and a call for participation)
by ajt (Prior) on Nov 19, 2002 at 13:26 UTC

    mirod Way to go!

    I don't know if I'm much help, I'm willing, but I'd love to see a Perl/XML cookbook, similar but expanded on "Ways to Rome".

    Perhaps a project on SourceFourge (or similar) like the Perl XML FAQ?

    Recently brian d foy has started to work on updating XML::RSS, and this has made me think that perhaps Perl needs a XML site where all the fine Perl/XML projects can be linked and listed. There are plenty of modules in CPAN, Yahoo groups, SourceForge projects, newsgroups, and related technologies, yet there doesn't seem to be a simple front page to the lot - if there is I'd like a link to it please!


    --
    ajt
      My original vision for the Perl-XML FAQ was that it should be packaged as a distribution available via CPAN. The Q&A part would be available in the source XML format as well as a POD version, but I also envisaged that the distribution would include an 'examples' directory. Unfortunately, I've been busy on other stuff and not progressed that side of things at all.

      The way I saw the examples working is to define a small number of 'problems' (3-5) and then provide one 'solution' for each problem using each available module. One or more of the problems would use mixed content (the XML as a document model) and some would not (the XML as data model). Some possible problems might include:

      • Read a Purchase Order XML document and extract some specific fields.
      • Read an XML document add, remove and modify specified elements then write it back out as XML.
      • Take a Perl data structure and output it as an XML document in a specified schema (small 's')
      • Transform an XML document into an HTML version

      The Perl-XML FAQ is under CVS on SourceForge and I'd be more than happy to accept contributions, but I think the first step before starting to cut code is to define the problems.

      http://www.perlxml.net/ was envisaged as a home for Perl XML stuff such as a directory of projects. I'm sure Ilya would welcome contributions - the Perl-XML mailing list might be a good place to discuss it.

Re: XML Examples (and a call for participation)
by princepawn (Parson) on Nov 19, 2002 at 14:55 UTC
    I'm ready. List the examples and I will do them in XML::TreeBuilder

    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://214066]
Approved by jmcnamara
Front-paged by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2022-05-17 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (68 votes). Check out past polls.

    Notices?