| 1 | package XML::MyXML; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use XML::MyXML::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use XML::MyXML::Util 'trim', 'strip_ns'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Scalar::Util 'weaken'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 6 | require Exporter; | |||||||||||||||||||||||||||
| 15 | 6 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 16 | 6 | our @EXPORT_OK = qw(tidy_xml object_to_xml xml_to_object simple_to_xml xml_to_simple check_xml xml_escape); | |||||||||||||||||||||||||||
| 17 | 6 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 6 | our $VERSION = "1.11"; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 6 | my $DEFAULT_INDENTSTRING = ' ' x 4; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 NAME | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | XML::MyXML - A simple-to-use XML module, for parsing and creating XML documents | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | use XML::MyXML qw(tidy_xml xml_to_object); | ||||||||||||||||||||||||||||
| 33 | use XML::MyXML qw(:all); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | my $xml = "<item><name>Table</name><price><usd>10.00</usd><eur>8.50</eur></price></item>"; | ||||||||||||||||||||||||||||
| 36 | print tidy_xml($xml); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | my $obj = xml_to_object($xml); | ||||||||||||||||||||||||||||
| 39 | print "Price in Euros = " . $obj->path('price/eur')->text; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | $obj->simplify is hashref { item => { name => 'Table', price => { usd => '10.00', eur => '8.50' } } } | ||||||||||||||||||||||||||||
| 42 | $obj->simplify({ internal => 1 }) is hashref { name => 'Table', price => { usd => '10.00', eur => '8.50' } } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 EXPORTABLE | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | xml_escape, tidy_xml, xml_to_object, object_to_xml, simple_to_xml, xml_to_simple, check_xml | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 FEATURES & LIMITATIONS | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | This module can parse XML comments, CDATA sections, XML entities (the standard five and numeric ones) and | ||||||||||||||||||||||||||||
| 51 | simple non-recursive C<< <!ENTITY> >>s | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | It will ignore (won't parse) C<< <!DOCTYPE...> >>, C<< <?...?> >> and other C<< <!...> >> special markup | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | All strings (XML documents, attribute names, values, etc) produced by this module or passed as parameters | ||||||||||||||||||||||||||||
| 56 | to its functions, are strings that contain characters, rather than bytes/octets. Unless you use the C<bytes> | ||||||||||||||||||||||||||||
| 57 | function flag (see below), in which case the XML documents (and just the XML documents) will be byte/octet | ||||||||||||||||||||||||||||
| 58 | strings. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | XML documents to be parsed may not contain the C<< > >> character unencoded in attribute values | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 OPTIONAL FUNCTION FLAGS | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Some functions and methods in this module accept optional flags, listed under each function in the | ||||||||||||||||||||||||||||
| 65 | documentation. They are optional, default to zero unless stated otherwise, and can be used as follows: | ||||||||||||||||||||||||||||
| 66 | S<C<< function_name( $param1, { flag1 => 1, flag2 => 1 } ) >>>. This is what each flag does: | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | C<strip> : the function will strip initial and ending whitespace from all text values returned | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | C<file> : the function will expect the path to a file containing an XML document to parse, instead of an | ||||||||||||||||||||||||||||
| 71 | XML string | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | C<complete> : the function's XML output will include an XML declaration (C<< <?xml ... ?> >>) in the | ||||||||||||||||||||||||||||
| 74 | beginning | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | C<internal> : the function will only return the contents of an element in a hashref instead of the | ||||||||||||||||||||||||||||
| 77 | element itself (see L</SYNOPSIS> for example) | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | C<tidy> : the function will return tidy XML | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | C<indentstring> : when producing tidy XML, this denotes the string with which child elements will be | ||||||||||||||||||||||||||||
| 82 | indented (Default is a string of 4 spaces) | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | C<save> : the function (apart from doing what it's supposed to do) will also save its XML output in a | ||||||||||||||||||||||||||||
| 85 | file whose path is denoted by this flag | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | C<strip_ns> : strip the namespaces (characters up to and including ':') from the tags | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | C<xslt> : will add a <?xml-stylesheet?> link in the XML that's being output, of type 'text/xsl', | ||||||||||||||||||||||||||||
| 90 | pointing to the filename or URL denoted by this flag | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | C<arrayref> : the function will create a simple arrayref instead of a simple hashref (which will preserve | ||||||||||||||||||||||||||||
| 93 | order and elements with duplicate tags) | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | C<bytes> : the XML document string which is parsed and/or produced by this function, should contain | ||||||||||||||||||||||||||||
| 96 | bytes/octets rather than characters | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 91 | ▶ | sub _encode { | ||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 103 | 91 | my $string = shift; | |||||||||||||||||||||||||||
| 104 | 91 | ▶ | defined $string or $string = ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 91 | my %replace = ( | |||||||||||||||||||||||||||
| 106 | '<' => '<', | ||||||||||||||||||||||||||||
| 107 | '>' => '>', | ||||||||||||||||||||||||||||
| 108 | '&' => '&', | ||||||||||||||||||||||||||||
| 109 | '\'' => ''', | ||||||||||||||||||||||||||||
| 110 | '"' => '"', | ||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||
| 112 | 91 | my $keys = "(".join("|", sort {length($b) <=> length($a)} keys %replace).")"; | |||||||||||||||||||||||||||
| 113 | 91 | $string =~ s/$keys/$replace{$1}/g; | |||||||||||||||||||||||||||
| 114 | 91 | return $string; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub check_xml; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head2 xml_escape($string) | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Returns the same string, but with the C<< < >>, C<< > >>, C<< & >>, C<< " >> and C<< ' >> characters | ||||||||||||||||||||||||||||
| 122 | replaced by their XML entities (e.g. C<< & >>). | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | ▶ | sub xml_escape { | ||||||||||||||||||||||||||
|
Subroutine
xml_escape: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 1 | my ($string) = @_; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | return _encode($string); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 195 | ▶ | sub _decode { | ||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 133 | 195 | my $string = shift; | |||||||||||||||||||||||||||
| 134 | 195 | ▶ | my $entities = shift || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 135 | 195 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 136 | 195 | ▶ | defined $string or $string = ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 195 | my %replace = ( | |||||||||||||||||||||||||||
| 138 | %$entities, | ||||||||||||||||||||||||||||
| 139 | '<' => '<', | ||||||||||||||||||||||||||||
| 140 | '>' => '>', | ||||||||||||||||||||||||||||
| 141 | '&' => '&', | ||||||||||||||||||||||||||||
| 142 | ''' => "'", | ||||||||||||||||||||||||||||
| 143 | '"' => '"', | ||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||
| 145 | 195 | my @capture = map quotemeta, keys %replace; | |||||||||||||||||||||||||||
| 146 | 195 | push @capture, '\&\#x([0-9A-Fa-f]+)\;', '\&\#([0-9]+)\;'; | |||||||||||||||||||||||||||
| 147 | 195 | my $capture = "(".join("|", @capture).")"; | |||||||||||||||||||||||||||
| 148 | 195 | $string =~ s| | |||||||||||||||||||||||||||
| 149 | $capture | ||||||||||||||||||||||||||||
| 150 | | | ||||||||||||||||||||||||||||
| 151 | 10 | my $reference = $1; | |||||||||||||||||||||||||||
| 152 | 10 | my $hex = $2; | |||||||||||||||||||||||||||
| 153 | 10 | my $decimal = $3; | |||||||||||||||||||||||||||
| 154 | $reference =~ /\&\#x/ ? chr(hex($hex)) | ||||||||||||||||||||||||||||
| 155 | : $reference =~ /\&\#/ ? chr($decimal) | ||||||||||||||||||||||||||||
| 156 | 10 | ▶ | : $replace{$reference}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 157 | |gex; | ||||||||||||||||||||||||||||
| 158 | 195 | return $string; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 tidy_xml($raw_xml) | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Returns the XML string in a tidy format (with tabs & newlines) | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Optional flags: C<file>, C<complete>, C<indentstring>, C<save>, C<bytes> | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 3 | ▶ | sub tidy_xml { | ||||||||||||||||||||||||||
|
Subroutine
tidy_xml: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 3 | my $xml = shift; | |||||||||||||||||||||||||||
| 172 | 3 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 3 | my $object = xml_to_object($xml, $flags); | |||||||||||||||||||||||||||
| 175 | 3 | ▶ | defined $object or return $object; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 3 | _tidy_object($object, undef, $flags); | |||||||||||||||||||||||||||
| 177 | 3 | my $return = $object->to_xml({ %$flags, tidy => 0 }) . "\n"; | |||||||||||||||||||||||||||
| 178 | 3 | return $return; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 xml_to_object($raw_xml) | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Creates an 'XML::MyXML::Object' object from the raw XML provided | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Optional flags: C<file>, C<bytes> | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 48 | ▶ | sub xml_to_object { | ||||||||||||||||||||||||||
|
Subroutine
xml_to_object: called CC=34 SCAR=36.8
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | 48 | my $xml = shift; | |||||||||||||||||||||||||||
| 192 | 48 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 48 | ▶ | if ($flags->{file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 1 | ▶ | open my $fh, '<', $xml or croak "Error: The file '$xml' could not be opened for reading: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 1 | $xml = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 197 | 1 | close $fh; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 48 | ▶ | if ($flags->{bytes} or $flags->{file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$flags->{"bytes"} or $flags->{"file"}
MC/DC100%2/2
$flags->{"bytes"} or $flags->{"file"}
$flags->{"bytes"}
$flags->{"file"}
Truth table100%3/3
A or B
A= $flags->{"bytes"}B= $flags->{"file"}
| |||||||||||||||||||||||||||||
| 201 | 5 | my (undef, undef, $encoding) = $xml =~ /<\?xml(\s[^>]+)?\sencoding=(['"])(.*?)\2/; | |||||||||||||||||||||||||||
| 202 | 5 | ▶ | $encoding = 'UTF-8' if ! defined $encoding or $encoding =~ /^utf\-?8\z/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $encoding or $encoding =~ /^utf\-?8\z/i
MC/DC0%0/2
not defined $encoding or $encoding =~ /^utf\-?8\z/i
not defined $encoding
$encoding =~ /^utf\-?8\z/i
Truth table33%1/3
not(A) or B
A= not defined $encodingB= $encoding =~ /^utf\-?8\z/i
| |||||||||||||||||||||||||||||
| 203 | 5 | ▶ | my $encoding_obj = find_encoding($encoding) or croak "Error: encoding '$encoding' not found"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 5 | ▶ | eval { $xml = $encoding_obj->decode($xml, Encode::FB_CROAK); 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | or croak "Error: Input string is invalid $encoding"; | ||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 47 | my $entities = {}; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # Parse CDATA sections | ||||||||||||||||||||||||||||
| 211 | 0 | $xml =~ s/\<\!\[CDATA\[(.*?)\]\]\>/_encode($1)/egs; | |||||||||||||||||||||||||||
| 212 | 47 | my @items = $xml =~ /(<!--.*?(?:-->|$)|<[^>]*?>|[^<>]+)/sg; | |||||||||||||||||||||||||||
| 213 | # Remove comments, special markup and initial whitespace | ||||||||||||||||||||||||||||
| 214 | { | ||||||||||||||||||||||||||||
| 215 | 47 | my $init_ws = 1; # whether we are inside initial whitespace | |||||||||||||||||||||||||||
| 216 | 47 | foreach my $item (@items) { | |||||||||||||||||||||||||||
| 217 | 383 | ▶ | if ($item =~ /\A<!--/) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 218 | 0 | ▶ | if ($item !~ /-->\z/) { croak encode_utf8("Error: unclosed XML comment block - '$item'"); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 0 | undef $item; | |||||||||||||||||||||||||||
| 220 | } elsif ($item =~ /\A<\?/) { # like <?xml?> or <?target?> | ||||||||||||||||||||||||||||
| 221 | 0 | ▶ | if ($item !~ /\?>\z/) { croak encode_utf8("Error: Erroneous special markup - '$item'"); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 1 | undef $item; | |||||||||||||||||||||||||||
| 223 | } elsif (my ($entname, undef, $entvalue) = $item =~ /^<!ENTITY\s+(\S+)\s+(['"])(.*?)\2\s*>\z/) { | ||||||||||||||||||||||||||||
| 224 | 2 | $entities->{"&$entname;"} = _decode($entvalue); | |||||||||||||||||||||||||||
| 225 | 2 | undef $item; | |||||||||||||||||||||||||||
| 226 | } elsif ($item =~ /<!/) { # like <!DOCTYPE> or <!ELEMENT> or <!ATTLIST> | ||||||||||||||||||||||||||||
| 227 | 0 | undef $item; | |||||||||||||||||||||||||||
| 228 | } elsif ($init_ws) { | ||||||||||||||||||||||||||||
| 229 | 51 | ▶ | if ($item =~ /\S/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 47 | $init_ws = 0; | |||||||||||||||||||||||||||
| 231 | } else { | ||||||||||||||||||||||||||||
| 232 | 4 | undef $item; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | 47 | ▶ | @items = grep defined, @items or croak "Error: No elements in the XML document"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | 47 | my @stack; | |||||||||||||||||||||||||||
| 239 | 47 | my $object = bless ({ | |||||||||||||||||||||||||||
| 240 | content => [], | ||||||||||||||||||||||||||||
| 241 | full_ns_info => {}, | ||||||||||||||||||||||||||||
| 242 | ns_data => {}, | ||||||||||||||||||||||||||||
| 243 | }, 'XML::MyXML::Object'); | ||||||||||||||||||||||||||||
| 244 | 47 | my $pointer = $object; | |||||||||||||||||||||||||||
| 245 | 47 | foreach my $item (@items) { | |||||||||||||||||||||||||||
| 246 | 376 | ▶ | if ($item =~ /^\<\/?\>\z/) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 247 | 0 | croak encode_utf8("Error: Strange tag: '$item'"); | |||||||||||||||||||||||||||
| 248 | } elsif ($item =~ /^\<\/([^\s>]+)\>\z/) { | ||||||||||||||||||||||||||||
| 249 | 115 | my ($el_name) = $1; | |||||||||||||||||||||||||||
| 250 | 115 | ▶ | $stack[-1]{el_name} eq $el_name | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | or croak encode_utf8("Error: Incompatible stack element: stack='$stack[-1]{el_name}' item='$item'"); | ||||||||||||||||||||||||||||
| 252 | 115 | my $stack_entry = pop @stack; | |||||||||||||||||||||||||||
| 253 | 115 | ▶ | delete $stack_entry->{content} if ! @{$stack_entry->{content}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 115 | $pointer = $stack_entry->{parent}; | |||||||||||||||||||||||||||
| 255 | } elsif ($item =~ /^\<[^>]+?(\/)?\>\z/) { | ||||||||||||||||||||||||||||
| 256 | 129 | my $is_self_closing = defined $1; | |||||||||||||||||||||||||||
| 257 | 129 | my ($el_name) = $item =~ /^<([^\s>\/]+)/; | |||||||||||||||||||||||||||
| 258 | 129 | ▶ | defined $el_name or croak encode_utf8("Error: Strange tag: '$item'"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 129 | $item =~ s/^\<\Q$el_name\E//; | |||||||||||||||||||||||||||
| 260 | 129 | $item =~ s/\/>\z//; | |||||||||||||||||||||||||||
| 261 | 129 | my @attrs = $item =~ /\s+([^\s=]+\s*=\s*(['"]).*?\2)/sg; | |||||||||||||||||||||||||||
| 262 | 129 | my $i = 0; | |||||||||||||||||||||||||||
| 263 | 129 | @attrs = grep {++$i % 2} @attrs; | |||||||||||||||||||||||||||
| 264 | 129 | my %attr; | |||||||||||||||||||||||||||
| 265 | 129 | foreach my $attr (@attrs) { | |||||||||||||||||||||||||||
| 266 | 58 | my ($attr_name, undef, $attr_value) = $attr =~ /^(\S+?)\s*=\s*(['"])(.*?)\2\z/s; | |||||||||||||||||||||||||||
| 267 | 58 | ▶ | defined $attr_name or croak encode_utf8("Error: Strange attribute: '$attr'"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 58 | $attr{$attr_name} = _decode($attr_value, $entities); | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | 129 | ▶ | my $entry = bless { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | el_name => $el_name, | ||||||||||||||||||||||||||||
| 272 | attrs => \%attr, | ||||||||||||||||||||||||||||
| 273 | $is_self_closing ? () : (content => []), | ||||||||||||||||||||||||||||
| 274 | parent => $pointer, | ||||||||||||||||||||||||||||
| 275 | }, 'XML::MyXML::Object'; | ||||||||||||||||||||||||||||
| 276 | 129 | weaken $entry->{parent}; | |||||||||||||||||||||||||||
| 277 | 129 | $entry->_apply_namespace_declarations; | |||||||||||||||||||||||||||
| 278 | 129 | ▶ | push @stack, $entry unless $is_self_closing; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 129 | push @{$pointer->{content}}, $entry; | |||||||||||||||||||||||||||
| 280 | 129 | ▶ | $pointer = $entry unless $is_self_closing; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | } elsif ($item =~ /^[^<>]*\z/) { | ||||||||||||||||||||||||||||
| 282 | 132 | my $entry = bless { | |||||||||||||||||||||||||||
| 283 | text => _decode($item, $entities), | ||||||||||||||||||||||||||||
| 284 | parent => $pointer, | ||||||||||||||||||||||||||||
| 285 | }, 'XML::MyXML::Object'; | ||||||||||||||||||||||||||||
| 286 | 132 | weaken $entry->{parent}; | |||||||||||||||||||||||||||
| 287 | 132 | push @{$pointer->{content}}, $entry; | |||||||||||||||||||||||||||
| 288 | } else { | ||||||||||||||||||||||||||||
| 289 | 0 | croak encode_utf8("Error: Strange element: '$item'"); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | 47 | ▶ | ! @stack or croak encode_utf8("Error: The <$stack[-1]{el_name}> element has not been closed in the XML document"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 46 | $object = $object->{content}[0]; | |||||||||||||||||||||||||||
| 294 | 46 | $object->{parent} = undef; | |||||||||||||||||||||||||||
| 295 | 46 | return $object; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 60 | ▶ | sub _objectarray_to_xml { | ||||||||||||||||||||||||||
|
Subroutine
_objectarray_to_xml: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 299 | 60 | my $object = shift; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 60 | my $xml = ''; | |||||||||||||||||||||||||||
| 302 | 60 | foreach my $stuff (@$object) { | |||||||||||||||||||||||||||
| 303 | 78 | ▶ | if (! defined $stuff->{el_name} and defined $stuff->{text}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $stuff->{"el_name"} and defined $stuff->{"text"}
MC/DC50%1/2
not defined $stuff->{"el_name"} and defined $stuff->{"text"}
not defined $stuff->{"el_name"}
defined $stuff->{"text"}
Truth table66%2/3
not(A) and B
A= not defined $stuff->{"el_name"}B= defined $stuff->{"text"}
| |||||||||||||||||||||||||||||
| 304 | 32 | $xml .= _encode($stuff->{text}); | |||||||||||||||||||||||||||
| 305 | } else { | ||||||||||||||||||||||||||||
| 306 | 46 | $xml .= "<".$stuff->{el_name}; | |||||||||||||||||||||||||||
| 307 | 46 | foreach my $attrname (keys %{$stuff->{attrs}}) { | |||||||||||||||||||||||||||
| 308 | 1 | $xml .= " ".$attrname.'="'._encode($stuff->{attrs}{$attrname}).'"'; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | 46 | ▶ | if (! defined $stuff->{content} or ! @{ $stuff->{content} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $stuff->{'content'} && @{$stuff->{'content'};}
MC/DC50%1/2
defined $stuff->{'content'} && @{$stuff->{'content'};}
defined $stuff->{'content'}
@{$stuff->{'content'};}
Truth table66%2/3
A && B
A= defined $stuff->{'content'}B= @{$stuff->{'content'};}
| |||||||||||||||||||||||||||||
| 311 | 7 | $xml .= "/>" | |||||||||||||||||||||||||||
| 312 | } else { | ||||||||||||||||||||||||||||
| 313 | 39 | $xml .= ">"; | |||||||||||||||||||||||||||
| 314 | 39 | $xml .= _objectarray_to_xml($stuff->{content}); | |||||||||||||||||||||||||||
| 315 | 39 | $xml .= "</".$stuff->{el_name}.">"; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | 60 | return $xml; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =head2 object_to_xml($object) | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | Creates an XML string from the 'XML::MyXML::Object' object provided | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | Optional flags: C<complete>, C<tidy>, C<indentstring>, C<save>, C<bytes> | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | =cut | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 0 | ▶ | sub object_to_xml { | ||||||||||||||||||||||||||
|
Subroutine
object_to_xml: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 332 | 0 | my $object = shift; | |||||||||||||||||||||||||||
| 333 | 0 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 0 | return $object->to_xml( $flags ); | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 19 | ▶ | sub _tidy_object { | ||||||||||||||||||||||||||
|
Subroutine
_tidy_object: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 339 | 19 | my $object = shift; | |||||||||||||||||||||||||||
| 340 | 19 | ▶ | my $tabs = shift || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 341 | 19 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 19 | ▶ | my $indentstring = exists $flags->{indentstring} ? $flags->{indentstring} : $DEFAULT_INDENTSTRING; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 19 | ▶ | return unless defined $object->{content} and @{$object->{content}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $object->{"content"} and @{$object->{"content"};}
MC/DC50%1/2
defined $object->{"content"} and @{$object->{"content"};}
defined $object->{"content"}
@{$object->{"content"};}
Truth table66%2/3
A and B
A= defined $object->{"content"}B= @{$object->{"content"};}
| |||||||||||||||||||||||||||||
| 346 | 9 | my $hastext; | |||||||||||||||||||||||||||
| 347 | 9 | my @children = @{$object->{content}}; | |||||||||||||||||||||||||||
| 348 | 9 | foreach my $i (0..$#children) { | |||||||||||||||||||||||||||
| 349 | 11 | my $child = $children[$i]; | |||||||||||||||||||||||||||
| 350 | 11 | ▶ | if (defined $child->{text} and $child->{text} =~ /\S/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $child->{"text"} and $child->{"text"} =~ /\S/
MC/DC50%1/2
defined $child->{"text"} and $child->{"text"} =~ /\S/
defined $child->{"text"}
$child->{"text"} =~ /\S/
Truth table66%2/3
A and B
A= defined $child->{"text"}B= $child->{"text"} =~ /\S/
| |||||||||||||||||||||||||||||
| 351 | 5 | $hastext = 1; | |||||||||||||||||||||||||||
| 352 | 5 | last; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | 9 | ▶ | return if $hastext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 4 | ▶ | @{$object->{content}} = grep { ! defined $_->{text} or $_->{text} =~ /\S/ } @{$object->{content}}; | ||||||||||||||||||||||||||
|
Condition33%1/3
!defined($_->{'text'}) || $_->{'text'} =~ /\S/
MC/DC0%0/2
!defined($_->{'text'}) || $_->{'text'} =~ /\S/
!defined($_->{'text'})
$_->{'text'} =~ /\S/
Truth table33%1/3
not(A) || B
A= !defined($_->{'text'})B= $_->{'text'} =~ /\S/
| |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 4 | @children = @{$object->{content}}; | |||||||||||||||||||||||||||
| 360 | 4 | $object->{content} = []; | |||||||||||||||||||||||||||
| 361 | 4 | for my $i (0..$#children) { | |||||||||||||||||||||||||||
| 362 | 6 | my $whitespace = bless { | |||||||||||||||||||||||||||
| 363 | text => "\n".($indentstring x ($tabs+1)), | ||||||||||||||||||||||||||||
| 364 | parent => $object, | ||||||||||||||||||||||||||||
| 365 | }, 'XML::MyXML::Object'; | ||||||||||||||||||||||||||||
| 366 | 6 | weaken $whitespace->{parent}; | |||||||||||||||||||||||||||
| 367 | 6 | push @{$object->{content}}, $whitespace; | |||||||||||||||||||||||||||
| 368 | 6 | push @{$object->{content}}, $children[$i]; | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | 4 | my $whitespace = bless { | |||||||||||||||||||||||||||
| 371 | text => "\n".($indentstring x $tabs), | ||||||||||||||||||||||||||||
| 372 | parent => $object, | ||||||||||||||||||||||||||||
| 373 | }, 'XML::MyXML::Object'; | ||||||||||||||||||||||||||||
| 374 | 4 | weaken $whitespace->{parent}; | |||||||||||||||||||||||||||
| 375 | 4 | push @{$object->{content}}, $whitespace; | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 4 | for my $i (0..$#{$object->{content}}) { | |||||||||||||||||||||||||||
| 378 | 16 | _tidy_object($object->{content}[$i], $tabs+1, $flags); | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | =head2 simple_to_xml($simple_array_ref) | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | Produces a raw XML string from either an array reference, a hash reference or a mixed structure such as these examples: | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | { thing => { name => 'John', location => { city => 'New York', country => 'U.S.A.' } } } | ||||||||||||||||||||||||||||
| 388 | # <thing><name>John</name><location><country>U.S.A.</country><city>New York</city></location></thing> | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | [ thing => [ name => 'John', location => [ city => 'New York', country => 'U.S.A.' ] ] ] | ||||||||||||||||||||||||||||
| 391 | # <thing><name>John</name><location><country>U.S.A.</country><city>New York</city></location></thing> | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | { thing => { name => 'John', location => [ city => 'New York', city => 'Boston', country => 'U.S.A.' ] } } | ||||||||||||||||||||||||||||
| 394 | # <thing><name>John</name><location><city>New York</city><city>Boston</city><country>U.S.A.</country></location></thing> | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | Here's a mini-tutorial on how to use this function, in which you'll also see how to set attributes. | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | The simplest invocations are these: | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | simple_to_xml({target => undef}) | ||||||||||||||||||||||||||||
| 401 | # <target/> | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | simple_to_xml({target => 123}) | ||||||||||||||||||||||||||||
| 404 | # <target>123</target> | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | Every set of sibling elements (such as the document itself, which is a single top-level element, or a pack of | ||||||||||||||||||||||||||||
| 407 | 5 elements all children to the same parent element) is represented in the $simple_array_ref parameter as | ||||||||||||||||||||||||||||
| 408 | key-value pairs inside either a hashref or an arrayref (you can choose which). | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | Keys represent tags+attributes of the sibling elements, whereas values represent the contents of those elements. | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | Eg: | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | [ | ||||||||||||||||||||||||||||
| 415 | first => 'John', | ||||||||||||||||||||||||||||
| 416 | last => 'Doe,' | ||||||||||||||||||||||||||||
| 417 | ] | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | ...and... | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | { | ||||||||||||||||||||||||||||
| 422 | first => 'John', | ||||||||||||||||||||||||||||
| 423 | last => 'Doe', | ||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | both translate to: | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | <first>John</first><last>Doe</last> | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | A value can either be undef (to denote an empty element), or a string (to denote a string), or another | ||||||||||||||||||||||||||||
| 431 | hashref/arrayref to denote a set of children elements, like this: | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | { | ||||||||||||||||||||||||||||
| 434 | person => { | ||||||||||||||||||||||||||||
| 435 | name => { | ||||||||||||||||||||||||||||
| 436 | first => 'John', | ||||||||||||||||||||||||||||
| 437 | last => 'Doe' | ||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | ...becomes: | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | <person> | ||||||||||||||||||||||||||||
| 445 | <name> | ||||||||||||||||||||||||||||
| 446 | <first>John</first> | ||||||||||||||||||||||||||||
| 447 | <last>Doe</last> | ||||||||||||||||||||||||||||
| 448 | </name> | ||||||||||||||||||||||||||||
| 449 | </person> | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | The only difference between using an arrayref or using a hashref, is that arrayrefs preserve the | ||||||||||||||||||||||||||||
| 453 | order of the elements, and allow repetition of identical tags. So a person with many addresses, should choose to | ||||||||||||||||||||||||||||
| 454 | represent its list of addresses under an arrayref, like this: | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | { | ||||||||||||||||||||||||||||
| 457 | person => [ | ||||||||||||||||||||||||||||
| 458 | name => { | ||||||||||||||||||||||||||||
| 459 | first => 'John', | ||||||||||||||||||||||||||||
| 460 | last => 'Doe', | ||||||||||||||||||||||||||||
| 461 | }, | ||||||||||||||||||||||||||||
| 462 | address => { | ||||||||||||||||||||||||||||
| 463 | country => 'Malta', | ||||||||||||||||||||||||||||
| 464 | }, | ||||||||||||||||||||||||||||
| 465 | address => { | ||||||||||||||||||||||||||||
| 466 | country => 'Indonesia', | ||||||||||||||||||||||||||||
| 467 | }, | ||||||||||||||||||||||||||||
| 468 | address => { | ||||||||||||||||||||||||||||
| 469 | country => 'China', | ||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||
| 471 | ] | ||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | ...which becomes: | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | <person> | ||||||||||||||||||||||||||||
| 477 | <name> | ||||||||||||||||||||||||||||
| 478 | <last>Doe</last> | ||||||||||||||||||||||||||||
| 479 | <first>John</first> | ||||||||||||||||||||||||||||
| 480 | </name> | ||||||||||||||||||||||||||||
| 481 | <address> | ||||||||||||||||||||||||||||
| 482 | <country>Malta</country> | ||||||||||||||||||||||||||||
| 483 | </address> | ||||||||||||||||||||||||||||
| 484 | <address> | ||||||||||||||||||||||||||||
| 485 | <country>Indonesia</country> | ||||||||||||||||||||||||||||
| 486 | </address> | ||||||||||||||||||||||||||||
| 487 | <address> | ||||||||||||||||||||||||||||
| 488 | <country>China</country> | ||||||||||||||||||||||||||||
| 489 | </address> | ||||||||||||||||||||||||||||
| 490 | </person> | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | Finally, to set attributes to your elements (eg id="12") you need to replace the key with either | ||||||||||||||||||||||||||||
| 493 | a string containing attributes as well (eg: C<'address id="12"'>), or replace it with a reference, as the many | ||||||||||||||||||||||||||||
| 494 | items in the examples below: | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | {thing => [ | ||||||||||||||||||||||||||||
| 497 | 'item id="1"' => 'chair', | ||||||||||||||||||||||||||||
| 498 | [item => {id => 2}] => 'table', | ||||||||||||||||||||||||||||
| 499 | [item => [id => 3]] => 'door', | ||||||||||||||||||||||||||||
| 500 | [item => id => 4] => 'sofa', | ||||||||||||||||||||||||||||
| 501 | {item => {id => 5}} => 'bed', | ||||||||||||||||||||||||||||
| 502 | {item => [id => 6]} => 'shirt', | ||||||||||||||||||||||||||||
| 503 | [item => {id => 7, other => 8}, [more => 9, also => 10, but_not => undef]] => 'towel' | ||||||||||||||||||||||||||||
| 504 | ]} | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | ...which becomes: | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | <thing> | ||||||||||||||||||||||||||||
| 509 | <item id="1">chair</item> | ||||||||||||||||||||||||||||
| 510 | <item id="2">table</item> | ||||||||||||||||||||||||||||
| 511 | <item id="3">door</item> | ||||||||||||||||||||||||||||
| 512 | <item id="4">sofa</item> | ||||||||||||||||||||||||||||
| 513 | <item id="5">bed</item> | ||||||||||||||||||||||||||||
| 514 | <item id="6">shirt</item> | ||||||||||||||||||||||||||||
| 515 | <item id="7" other="8" more="9" also="10">towel</item> | ||||||||||||||||||||||||||||
| 516 | </thing> | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | As you see, attributes may be represented in a great variety of ways, so you don't need to remember | ||||||||||||||||||||||||||||
| 519 | the "correct" one. | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | Of course if the "simple structure" is a hashref, the key cannot be a reference (because hash keys are always | ||||||||||||||||||||||||||||
| 522 | strings), so if you want attributes on your elements, you either need the enclosing structure to be an | ||||||||||||||||||||||||||||
| 523 | arrayref as in the example above, to allow keys to be refs which contain the attributes, or you need to | ||||||||||||||||||||||||||||
| 524 | represent the key (=tag+attrs) as a string, like this (also in the previous example): C<'item id="1"'> | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | This concludes the mini-tutorial of the simple_to_xml function. | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | All the strings in C<$simple_array_ref> need to contain characters, rather than bytes/octets. The C<bytes> | ||||||||||||||||||||||||||||
| 529 | optional flag only affects the produced XML string. | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | Optional flags: C<complete>, C<tidy>, C<indentstring>, C<save>, C<xslt>, C<bytes> | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | =cut | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | 22 | ▶ | sub simple_to_xml { | ||||||||||||||||||||||||||
|
Subroutine
simple_to_xml: called CC=17 SCAR=31.8
Pod
documented
| |||||||||||||||||||||||||||||
| 536 | 22 | my $arref = shift; | |||||||||||||||||||||||||||
| 537 | 22 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | 22 | my $xml = ''; | |||||||||||||||||||||||||||
| 540 | 22 | ▶ | my ($key, $value, @residue) = (ref $arref eq 'HASH') ? %$arref : @$arref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 541 | 22 | $key = _key_to_string($key); | |||||||||||||||||||||||||||
| 542 | 22 | ▶ | ! @residue or croak "Error: the provided simple ref contains more than 1 top element"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 543 | 22 | my ($el_name) = $key =~ /^(\S+)/; | |||||||||||||||||||||||||||
| 544 | 22 | ▶ | defined $el_name or croak encode_utf8 "Error: Strange key: $key"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | 22 | ▶ | if (! ref $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 547 | 12 | ▶ | if ($key eq '!as_is') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 548 | 0 | ▶ | check_xml $value or croak "invalid xml: $value"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 549 | 0 | $xml .= $value; | |||||||||||||||||||||||||||
| 550 | ▶ | } elsif (defined $value and length $value) { | |||||||||||||||||||||||||||
|
Condition33%1/3
defined $value and length $value
MC/DC0%0/2
defined $value and length $value
defined $value
length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||
| 551 | 12 | $xml .= "<$key>"._encode($value)."</$el_name>"; | |||||||||||||||||||||||||||
| 552 | } else { | ||||||||||||||||||||||||||||
| 553 | 0 | $xml .= "<$key/>"; | |||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | } else { | ||||||||||||||||||||||||||||
| 556 | 10 | $xml .= "<$key>@{[ _arrayref_to_xml($value, $flags) ]}</$el_name>"; | |||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||
| 558 | 22 | ▶ | if ($flags->{tidy}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 559 | $xml = tidy_xml($xml, { | ||||||||||||||||||||||||||||
| 560 | 0 | ▶ | exists $flags->{indentstring} ? (indentstring => $flags->{indentstring}) : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 561 | }); | ||||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||
| 563 | 22 | my $decl = ''; | |||||||||||||||||||||||||||
| 564 | 22 | ▶ | $decl .= qq'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n' if $flags->{complete}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 565 | 22 | ▶ | $decl .= qq'<?xml-stylesheet type="text/xsl" href="$flags->{xslt}"?>\n' if $flags->{xslt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 566 | 22 | $xml = $decl . $xml; | |||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | 22 | ▶ | if (defined $flags->{save}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 569 | 1 | ▶ | open my $fh, '>', $flags->{save} or croak "Error: Couldn't open file '$flags->{save}' for writing: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 570 | 1 | binmode $fh, ':encoding(UTF-8)'; | |||||||||||||||||||||||||||
| 571 | 1 | print $fh $xml; | |||||||||||||||||||||||||||
| 572 | 1 | close $fh; | |||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | 22 | ▶ | $xml = encode_utf8($xml) if $flags->{bytes}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 576 | 22 | return $xml; | |||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | 127 | ▶ | sub _flatten { | ||||||||||||||||||||||||||
|
Subroutine
_flatten: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 580 | 127 | my ($thing) = @_; | |||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | 127 | ▶ | if (!ref $thing) { return $thing; } | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 583 | 13 | elsif (ref $thing eq 'HASH') { return map _flatten($_), %$thing; } | |||||||||||||||||||||||||||
| 584 | 24 | elsif (ref $thing eq 'ARRAY') { return map _flatten($_), @$thing; } | |||||||||||||||||||||||||||
| 585 | 0 | else { croak 'Error: reference of invalid type in simple_to_xml: '.(ref $thing); } | |||||||||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | 38 | ▶ | sub _key_to_string { | ||||||||||||||||||||||||||
|
Subroutine
_key_to_string: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 589 | 38 | my ($key) = @_; | |||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | 38 | ▶ | if (! ref $key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 592 | 20 | return $key; | |||||||||||||||||||||||||||
| 593 | } else { | ||||||||||||||||||||||||||||
| 594 | 18 | my ($tag, %attrs) = _flatten($key); | |||||||||||||||||||||||||||
| 595 | 18 | return $tag . join('', map " $_=\""._encode($attrs{$_}).'"', grep {defined $attrs{$_}} keys %attrs); | |||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | 12 | ▶ | sub _arrayref_to_xml { | ||||||||||||||||||||||||||
|
Subroutine
_arrayref_to_xml: called CC=10 SCAR=24.9
| |||||||||||||||||||||||||||||
| 600 | 12 | my $arref = shift; | |||||||||||||||||||||||||||
| 601 | 12 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | 12 | my $xml = ''; | |||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | 12 | ▶ | if (ref $arref eq 'HASH') { return _hashref_to_xml($arref, $flags); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | 7 | ▶ | foreach (my $i = 0; $i <= $#$arref; ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 608 | 16 | my $key = $arref->[$i++]; | |||||||||||||||||||||||||||
| 609 | 16 | $key = _key_to_string($key); | |||||||||||||||||||||||||||
| 610 | 16 | my ($el_name) = $key =~ /^(\S+)/; | |||||||||||||||||||||||||||
| 611 | 16 | ▶ | defined $el_name or croak encode_utf8 "Error: Strange key: $key"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 612 | 16 | my $value = $arref->[$i++]; | |||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | 16 | ▶ | if ($key eq '!as_is') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 615 | 0 | ▶ | check_xml $value or croak "invalid xml: $value"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 616 | 0 | $xml .= $value; | |||||||||||||||||||||||||||
| 617 | } elsif (! ref $value) { | ||||||||||||||||||||||||||||
| 618 | 14 | ▶ | if (defined $value and length $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $value and length $value
MC/DC0%0/2
defined $value and length $value
defined $value
length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||
| 619 | 14 | $xml .= "<$key>@{[ _encode($value) ]}</$el_name>"; | |||||||||||||||||||||||||||
| 620 | } else { | ||||||||||||||||||||||||||||
| 621 | 0 | $xml .= "<$key/>"; | |||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||
| 623 | } else { | ||||||||||||||||||||||||||||
| 624 | 2 | $xml .= "<$key>@{[ _arrayref_to_xml($value, $flags) ]}</$el_name>"; | |||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||
| 627 | 7 | return $xml; | |||||||||||||||||||||||||||
| 628 | } | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | 5 | ▶ | sub _hashref_to_xml { | ||||||||||||||||||||||||||
|
Subroutine
_hashref_to_xml: called CC=9 SCAR=22.5
| |||||||||||||||||||||||||||||
| 631 | 5 | my $hashref = shift; | |||||||||||||||||||||||||||
| 632 | 5 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | 5 | my $xml = ''; | |||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | 5 | ▶ | while (my ($key, $value) = each %$hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 637 | 5 | my ($el_name) = $key =~ /^(\S+)/; | |||||||||||||||||||||||||||
| 638 | 5 | ▶ | defined $el_name or croak encode_utf8 "Error: Strange key: $key"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | 5 | ▶ | if ($key eq '!as_is') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 641 | 1 | ▶ | check_xml $value or croak "invalid xml: $value"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 642 | 1 | $xml .= $value; | |||||||||||||||||||||||||||
| 643 | } elsif (! ref $value) { | ||||||||||||||||||||||||||||
| 644 | 4 | ▶ | if (defined $value and length $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and length $value
MC/DC100%2/2
defined $value and length $value
defined $value
length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||
| 645 | 2 | $xml .= "<$key>@{[ _encode($value) ]}</$el_name>"; | |||||||||||||||||||||||||||
| 646 | } else { | ||||||||||||||||||||||||||||
| 647 | 2 | $xml .= "<$key/>"; | |||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||
| 649 | } else { | ||||||||||||||||||||||||||||
| 650 | 0 | $xml .= "<$key>@{[ _arrayref_to_xml($value, $flags) ]}</$el_name>"; | |||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||
| 653 | 5 | return $xml; | |||||||||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | =head2 xml_to_simple($raw_xml) | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | Produces a very simple hash object from the raw XML string provided. An example hash object created thusly is this: | ||||||||||||||||||||||||||||
| 660 | S<C<< { thing => { name => 'John', location => { city => 'New York', country => 'U.S.A.' } } } >>> | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | B<WARNING:> This function only works on very simple XML strings, i.e. children of an element may not consist of both | ||||||||||||||||||||||||||||
| 663 | text and elements (child elements will be discarded in that case). Also attributes in tags are ignored. | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | Since the object created is a hashref (unless used with the C<arrayref> optional flag), duplicate keys will be | ||||||||||||||||||||||||||||
| 666 | discarded. | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | All strings contained in the output simple structure will always contain characters rather than octets/bytes, | ||||||||||||||||||||||||||||
| 669 | regardless of the C<bytes> optional flag. | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | Optional flags: C<internal>, C<strip>, C<file>, C<strip_ns>, C<arrayref>, C<bytes> | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | =cut | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | 7 | ▶ | sub xml_to_simple { | ||||||||||||||||||||||||||
|
Subroutine
xml_to_simple: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 676 | 7 | my $xml = shift; | |||||||||||||||||||||||||||
| 677 | 7 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | 7 | my $object = xml_to_object($xml, $flags); | |||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | 7 | ▶ | $object = $object->simplify($flags) if defined $object; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | 7 | return $object; | |||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | 57 | ▶ | sub _objectarray_to_simple { | ||||||||||||||||||||||||||
|
Subroutine
_objectarray_to_simple: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||
| 687 | 57 | my $object = shift; | |||||||||||||||||||||||||||
| 688 | 57 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | 57 | ▶ | defined $object or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | return $flags->{arrayref} | ||||||||||||||||||||||||||||
| 693 | 57 | ▶ | ? _objectarray_to_simple_arrayref($object, $flags) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 694 | : _objectarray_to_simple_hashref($object, $flags); | ||||||||||||||||||||||||||||
| 695 | } | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | 57 | ▶ | sub _objectarray_to_simple_hashref { | ||||||||||||||||||||||||||
|
Subroutine
_objectarray_to_simple_hashref: called CC=10 SCAR=23.6
| |||||||||||||||||||||||||||||
| 698 | 57 | my $object = shift; | |||||||||||||||||||||||||||
| 699 | 57 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | 57 | ▶ | defined $object or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||
| 703 | 57 | my $hashref = {}; | |||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | 57 | foreach my $stuff (@$object) { | |||||||||||||||||||||||||||
| 706 | 102 | ▶ | if (defined(my $key = $stuff->{el_name})) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 707 | 44 | ▶ | $key = strip_ns $key if $flags->{strip_ns}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 708 | 44 | $hashref->{ $key } = _objectarray_to_simple($stuff->{content}, $flags); | |||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||
| 710 | elsif (defined(my $value = $stuff->{text})) { | ||||||||||||||||||||||||||||
| 711 | 58 | ▶ | $value = trim $value if $flags->{strip}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 712 | 58 | ▶ | return $value if $value =~ /\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 713 | } | ||||||||||||||||||||||||||||
| 714 | } | ||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||
| 716 | 32 | ▶ | return %$hashref ? $hashref : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 717 | } | ||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | 0 | ▶ | sub _objectarray_to_simple_arrayref { | ||||||||||||||||||||||||||
|
Subroutine
_objectarray_to_simple_arrayref: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 720 | 0 | my $object = shift; | |||||||||||||||||||||||||||
| 721 | 0 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | 0 | ▶ | defined $object or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||
| 725 | 0 | my $arrayref = []; | |||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||
| 727 | 0 | foreach my $stuff (@$object) { | |||||||||||||||||||||||||||
| 728 | 0 | ▶ | if (defined(my $key = $stuff->{el_name})) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 729 | 0 | ▶ | $key = strip_ns $key if $flags->{strip_ns}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 730 | 0 | push @$arrayref, ( $key, _objectarray_to_simple($stuff->{content}, $flags) ); | |||||||||||||||||||||||||||
| 731 | } elsif (defined(my $value = $stuff->{text})) { | ||||||||||||||||||||||||||||
| 732 | 0 | ▶ | $value = trim $value if $flags->{strip}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 733 | 0 | ▶ | return $value if $value =~ /\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||
| 735 | } | ||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||
| 737 | 0 | ▶ | return @$arrayref ? $arrayref : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 738 | } | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||
| 741 | =head2 check_xml($raw_xml) | ||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||
| 743 | Returns true if the $raw_xml string is valid XML (valid enough to be used by this module), and false otherwise. | ||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||
| 745 | Optional flags: C<file>, C<bytes> | ||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||
| 747 | =cut | ||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||
| 749 | 3 | ▶ | sub check_xml { | ||||||||||||||||||||||||||
|
Subroutine
check_xml: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 750 | 3 | my $xml = shift; | |||||||||||||||||||||||||||
| 751 | 3 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||
| 753 | 3 | my $ok = eval { xml_to_object($xml, $flags); 1 }; | |||||||||||||||||||||||||||
| 754 | 3 | return !!$ok; | |||||||||||||||||||||||||||
| 755 | } | ||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||
| 758 | 6 | 1; # End of XML::MyXML | |||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||
| 760 | __END__ | ||||||||||||||||||||||||||||