/usr/share/perl5/XML/Struct.pm is in libxml-struct-perl 0.26-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | package XML::Struct;
use strict;
use XML::LibXML::Reader;
use XML::Struct::Reader;
use XML::Struct::Writer;
use XML::Struct::Simple;
our $VERSION = '0.26';
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(readXML writeXML simpleXML removeXMLAttr textValues);
sub readXML { # ( [$from], %options )
my (%options) = @_ % 2 ? (from => @_) : @_;
my %reader_options = (
map { $_ => delete $options{$_} }
grep { exists $options{$_} }
qw(attributes whitespace path stream simple micro root ns depth content deep)
);
if (%options) {
if (exists $options{from} and keys %options == 1) {
$reader_options{from} = $options{from};
} else {
$reader_options{from} = \%options;
}
}
XML::Struct::Reader->new( %reader_options )->readDocument;
}
sub writeXML {
my ($xml, %options) = @_;
XML::Struct::Writer->new(%options)->write($xml);
}
sub simpleXML {
my ($element, %options) = @_;
XML::Struct::Simple->new(%options)->transform($element);
}
*removeXMLAttr = *XML::Struct::Simple::removeXMLAttr;
# TODO: document (better name?)
sub textValues {
my ($element, $options) = @_;
# TODO: %options (e.g. join => " ")
my $children = $element->[2];
return "" if !$children;
return join "", grep { $_ ne "" } map {
ref $_ ? textValues($_, $options) : $_
} @$children;
}
1;
__END__
=encoding utf8
=head1 NAME
XML-Struct - Represent XML as data structure preserving element order
=begin markdown
# Status
[![Build Status](https://travis-ci.org/nichtich/XML-Struct.png)](https://travis-ci.org/nichtich/XML-Struct)
[![Coverage Status](https://coveralls.io/repos/nichtich/XML-Struct/badge.png)](https://coveralls.io/r/nichtich/XML-Struct)
[![Kwalitee Score](http://cpants.cpanauthors.org/dist/XML-Struct.png)](http://cpants.cpanauthors.org/dist/XML-Struct)
=end markdown
=head1 SYNOPSIS
use XML::Struct qw(readXML writeXML simpleXML removeXMLAttr);
my $xml = readXML( "input.xml" );
# [ root => { xmlns => 'http://example.org/' }, [ '!', [ x => {}, [42] ] ] ]
my $doc = writeXML( $xml );
# <?xml version="1.0" encoding="UTF-8"?>
# <root xmlns="http://example.org/">!<x>42</x></root>
my $simple = simpleXML( $xml, root => 'record' );
# { record => { xmlns => 'http://example.org/', x => 42 } }
my $xml2 = removeXMLAttr($xml);
# [ root => [ '!', [ x => [42] ] ] ]
=head1 DESCRIPTION
L<XML::Struct> implements a mapping between XML and Perl data structures. By
default, the mapping preserves element order, so it also suits for
"document-oriented" XML. In short, an XML element is represented as array
reference with three parts:
[ $name => \%attributes, \@children ]
This data structure corresponds to the abstract data model of
L<MicroXML|http://www.w3.org/community/microxml/>, a simplified subset of XML.
If your XML documents don't contain relevant attributes, you can also choose
to map to this format:
[ $name => \@children ]
Both parsing (with L<XML::Struct::Reader> or function C<readXML>) and
serializing (with L<XML::Struct::Writer> or function C<writeXML>) are fully
based on L<XML::LibXML>, so performance is better than L<XML::Simple> and
similar to L<XML::LibXML::Simple>.
=head1 MODULES
=over
=item L<XML::Struct::Reader>
Parse XML as stream into XML data structures.
=item L<XML::Struct::Writer>
Write XML data structures to XML streams for serializing, SAX processing, or
creating a DOM object.
=item L<XML::Struct::Writer::Stream>
Simplified SAX handler for XML serialization.
=item L<XML::Struct::Simple>
Transform XML data structure into simple form.
=back
=head1 FUNCTIONS
The following functions are exported on request:
=head2 readXML( $source [, %options ] )
Read an XML document with L<XML::Struct::Reader>. The type of source (string,
filename, URL, IO Handle...) is detected automatically. Options not known to
XML::Struct::Reader are passed to L<XML::LibXML::Reader>.
=head2 writeXML( $xml [, %options ] )
Write an XML document/element with L<XML::Struct::Writer>.
=head2 simpleXML( $element [, %options ] )
Transform an XML document/element into simple key-value format as known from
L<XML::Simple>. See L<XML::Struct::Simple> for configuration options.
=head2 removeXMLAttr( $element )
Transform XML structure with attributes to XML structure without attributes.
The function does not modify the passed element but creates a modified copy.
=head1 EXAMPLE
To give an example, with L<XML::Struct::Reader>, this XML document:
<root>
<foo>text</foo>
<bar key="value">
text
<doz/>
</bar>
</root>
is transformed to this structure:
[
"root", { }, [
[ "foo", { }, "text" ],
[ "bar", { key => "value" }, [
"text",
[ "doz", { }, [ ] ]
]
]
]
This module also supports a simple key-value (aka "data-oriented") format, as
used by L<XML::Simple>. With option C<simple> (or function C<simpleXML>) the
document given above would be transformed to this structure:
{
foo => "text",
bar => {
key => "value",
doz => {}
}
}
=head1 SEE ALSO
This module was first created to be used in L<Catmandu::XML> and turned out to
also become a replacement for L<XML::Simple>. See the former for more XML
processing.
L<XML::Twig> is another popular and powerfull module for stream-based
processing of XML documents.
See L<XML::Smart>, L<XML::Hash::LX>, L<XML::Parser::Style::ETree>,
L<XML::Fast>, and L<XML::Structured> for different representations of XML data
as data structures (feel free to implement converters from/to XML::Struct).
L<XML::GenericJSON> seems to be an outdated and incomplete attempt to capture
more parts of XML Infoset in another data structure.
See JSONx for a kind of reverse direction (JSON in XML).
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Jakob Voß.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
=cut
|