Thread Attributname sprengt Formatierung (12 answers)
Opened by Commu at 2016-08-23 13:44

Linuxer
 2016-08-24 23:50
#185290 #185290
User since
2006-01-27
3870 Artikel
HausmeisterIn

user image
Auf die archaische Tour:

Man kann sich die eingelesene Struktur mit CPAN:Data::Dumper ausgeben lassen und erfährt so, wie die interne Struktur ausschaut.
Danach passt man den Code an, um weitere Attribute hinzuzufügen.

more (1.4kb):

Code: (dl )
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
#!/usr/bin/env perl

use strict;
use warnings;

use XML::Simple;

my $in_file = "in.xml";
my $xml_file = 'out.xml';

my $xml = XMLin(
$in_file,
KeepRoot => 1,
ForceArray => 1,
);

# show me what the data look like
use Data::Dumper;
warn Dumper $xml;


### Save a Reference to the first "attribut" tag
my $ref = $xml->{importrecord}->[0]
->{aktionen}->[0]
->{transaktion}->[0]
->{aktion}->[0]
->{parameter}->[0]
->{attribut}->[0];

# add new additional attribute into the referenced tag
$ref->{foo} = 'bar';

# add content via ->{content}, so now attributes are preserved and stay
$ref->{content} = 'Anlegen';


XMLout(
$xml,
KeepRoot => 1,
NoAttr => 0,
OutputFile => $xml_file,
);



Neues Resultat:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<importrecord>
<aktionen>
<transaktion>
<aktion>
<parameter>
<attribut attributname="aktion" foo="bar">Anlegen</attribut>
<attribut attributname="bereich" />
<attribut attributname="dokumenttyp" />
<attribut attributname="voname" />
<attribut attributname="gz" />
<attribut attributname="betreff" />
</parameter>
</aktion>
</transaktion>
</aktionen>
<dateiname>24845_2874846.fmd</dateiname>
</importrecord>

Last edited: 2016-08-25 14:56:36 +0200 (CEST)
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Attributname sprengt Formatierung