| 1 | # ----------------------------------------------------------------------------- | ||
| 2 | |||
| 3 | =encoding utf8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Quiq::Xml - Allgemeine XML-Operationen | ||
| 8 | |||
| 9 | =head1 BASE CLASS | ||
| 10 | |||
| 11 | L<Quiq::Object> | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | # ----------------------------------------------------------------------------- | ||
| 16 | |||
| 17 | package Quiq::Xml; | ||
| 18 | 2 | ▶ | use base qw/Quiq::Object/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | |||
| 20 | 2 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | |||
| 24 | our $VERSION = '1.240'; | ||
| 25 | |||
| 26 | 0 | ▶ | use XML::Twig (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 27 | use Quiq::Path; | ||
| 28 | use XML::Compile::Schema (); | ||
| 29 | use XML::LibXML (); | ||
| 30 | use XML::Compile::Util (); | ||
| 31 | |||
| 32 | # ----------------------------------------------------------------------------- | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =head2 Klassenmethoden | ||
| 37 | |||
| 38 | =head3 print() - Formatiere XML | ||
| 39 | |||
| 40 | =head4 Synopsis | ||
| 41 | |||
| 42 | $xmlFormatted = $this->print($xml); | ||
| 43 | |||
| 44 | =head4 Returns | ||
| 45 | |||
| 46 | XML als formatierte Zeichenkette | ||
| 47 | |||
| 48 | =head4 Description | ||
| 49 | |||
| 50 | Liefere XML-Code $xml als formtierte Zeichenkette mit Einrückung. | ||
| 51 | |||
| 52 | =head4 Example | ||
| 53 | |||
| 54 | say Quiq::Xml->print($xml); | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | # ----------------------------------------------------------------------------- | ||
| 59 | |||
| 60 | sub print { | ||
| 61 | my ($this,$xml) = @_; | ||
| 62 | |||
| 63 | my $twg = XML::Twig->new(pretty_print=>'indented'); | ||
| 64 | $twg->parsestring($xml); | ||
| 65 | |||
| 66 | return $twg->sprint; | ||
| 67 | } | ||
| 68 | |||
| 69 | # ----------------------------------------------------------------------------- | ||
| 70 | |||
| 71 | =head3 xmlToTree() - Wandele XML in Baum | ||
| 72 | |||
| 73 | =head4 Synopsis | ||
| 74 | |||
| 75 | $tree = $this->xmlToTree($xml,%opt); | ||
| 76 | $tree = $this->xmlToTree($file,%opt); | ||
| 77 | |||
| 78 | =head4 Arguments | ||
| 79 | |||
| 80 | =over 4 | ||
| 81 | |||
| 82 | =item $xml | ||
| 83 | |||
| 84 | XML Code als Zeichenkette | ||
| 85 | |||
| 86 | =item $file | ||
| 87 | |||
| 88 | Datei mit XML Code | ||
| 89 | |||
| 90 | =back | ||
| 91 | |||
| 92 | =head4 Options | ||
| 93 | |||
| 94 | =over 4 | ||
| 95 | |||
| 96 | =item -xsdDir => $xsdDir (Default: $ENV{'XSD_DIR'} // '.'): | ||
| 97 | |||
| 98 | Verzeichnis mit XML Schema Definitionsdateien (.xsd). | ||
| 99 | |||
| 100 | =back | ||
| 101 | |||
| 102 | =head4 Returns | ||
| 103 | |||
| 104 | (Perl Datenstruktur) Baum | ||
| 105 | |||
| 106 | =head4 Description | ||
| 107 | |||
| 108 | Wandele den XML Code $xml in eine hierarchische Perl-Datenstruktur | ||
| 109 | (Baum) und liefere eine Referenz auf diese Struktur zurück. | ||
| 110 | |||
| 111 | =head4 Example | ||
| 112 | |||
| 113 | $ perl -MQuiq::Xml -E 'Quiq::Xml->xmlToTree("02-taxifahrt-orig.xml",-xsdDir=>"~/dat/zugferd")' | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | # ----------------------------------------------------------------------------- | ||
| 118 | |||
| 119 | sub xmlToTree { | ||
| 120 | my ($class,$xml) = splice @_,0,2; | ||
| 121 | # @_: %opt | ||
| 122 | |||
| 123 | # Optionen | ||
| 124 | |||
| 125 | my $xsdDir = $ENV{'XSD_DIR'} // '.'; | ||
| 126 | |||
| 127 | $class->parameters(\@_, | ||
| 128 | -xsdDir => \$xsdDir, | ||
| 129 | ); | ||
| 130 | |||
| 131 | # Operation ausführen | ||
| 132 | |||
| 133 | if ($xml !~ /</) { | ||
| 134 | $xml = Quiq::Path->read($xml,-decode=>'utf-8'); | ||
| 135 | } | ||
| 136 | |||
| 137 | # #-Kommentarzeilen entfernen | ||
| 138 | $xml =~ s/^\s*#.*$//gm; | ||
| 139 | |||
| 140 | # Ermittele die .xsd-Dateien | ||
| 141 | my @xsdFiles = Quiq::Path->find($xsdDir,-pattern=>'\.xsd$'); | ||
| 142 | |||
| 143 | # Instantiiere das Schema-Objekt | ||
| 144 | |||
| 145 | my $sch = XML::Compile::Schema->new; | ||
| 146 | for my $file (@xsdFiles) { | ||
| 147 | $sch->importDefinitions($file); | ||
| 148 | } | ||
| 149 | |||
| 150 | my $doc = XML::LibXML->load_xml( | ||
| 151 | string => $xml, | ||
| 152 | no_blanks => 1, | ||
| 153 | ); | ||
| 154 | my $top = $doc->documentElement; | ||
| 155 | my $rootType = XML::Compile::Util::type_of_node($top); | ||
| 156 | |||
| 157 | # Instantiiere XML-Reader | ||
| 158 | my $rdr = $sch->compile(READER=>$rootType, | ||
| 159 | sloppy_floats => 1, # Wir wollen keine BigFloat-Elemente | ||
| 160 | sloppy_integers => 1, # Wir wollen keine BigInt-Elemente | ||
| 161 | ); | ||
| 162 | |||
| 163 | # Erzeuge Baum | ||
| 164 | my $tree = $rdr->($doc); | ||
| 165 | |||
| 166 | return $tree; | ||
| 167 | } | ||
| 168 | |||
| 169 | # ----------------------------------------------------------------------------- | ||
| 170 | |||
| 171 | =head1 VERSION | ||
| 172 | |||
| 173 | 1.240 | ||
| 174 | |||
| 175 | =head1 AUTHOR | ||
| 176 | |||
| 177 | Frank Seitz, L<http://fseitz.de/> | ||
| 178 | |||
| 179 | =head1 COPYRIGHT | ||
| 180 | |||
| 181 | Copyright (C) 2026 Frank Seitz | ||
| 182 | |||
| 183 | =head1 LICENSE | ||
| 184 | |||
| 185 | This code is free software; you can redistribute it and/or modify | ||
| 186 | it under the same terms as Perl itself. | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | # ----------------------------------------------------------------------------- | ||
| 191 | |||
| 192 | 1; | ||
| 193 | |||
| 194 | # eof | ||