Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A cute little query language for converting XML to Perl

tag: 0.6000

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 CuteQueries
Octocat-spinner-32 lib
Octocat-spinner-32 t
Octocat-spinner-32 .perlcriticrc
Octocat-spinner-32 .todo
Octocat-spinner-32 Changes
Octocat-spinner-32 CuteQueries.pm
Octocat-spinner-32 MANIFEST
Octocat-spinner-32 MANIFEST.SKIP
Octocat-spinner-32 Makefile.PL
Octocat-spinner-32 README
Octocat-spinner-32 example.xml
Octocat-spinner-32 example2.xml
README
# This module produces results rather like L<XML::Simple>, but
# without the ambiguity problems inherent in going from XML to
# Perl.

use strict;
use warnings;
use XML::CuteQueries;

my $CQ = XML::CuteQueries->new;
   $CQ->parse(<<"EOXML");

<root>
    <result>OK</result>
    <data>
        <row><f1> 7</f1><f2>11</f2><f3>13</f3></row>
        <row><f1>17</f1><f2>19</f2><f3>23</f3></row>
        <row><f1>29</f1><f2>31</f2><f3>37</f3></row>
    </data>
</root>

EOXML

my $arrayref_of_hashrefs = $CQ->cute_query(
    # the top level query is for the <data> elements
    # the shape of the only top level query is [],
    # so it returns one [] -- for the one <data> element

    "data" => [

        # the contents of the top level [] is a sub query for row elements.
        # Each row element should be a hashref, so the data-[] will contain
        # three row-{} hashrefs

        row => {

            # the contents of those hashrefs is a subquery for any tag found
            # there.  The tag names are preserved as keys because we're
            # sitting in the context of a hashref.
            # the shape of each match result is '', so it just returns the
            # contents of each tag as a string.

            '*' => '',
        }
    ],
);

# [ {f1=> 7, f2=>11, f3=>13},
#   {f1=>17, f2=>19, f3=>23},
#   {f1=>29, f2=>31, f3=>37}, ]
Something went wrong with that request. Please try again.