XML::S...torial(3pm)
π XML::S...torial(3pm) β the XML::Smart::Tutorial era, from User Contributed Perl Documentation π
πΈ NAME πΈ
XML::Smart::Tutorial β tutorial and examples for XML::Smart. periodt β¨
π SYNOPSIS π
Okay so this doc is a tutorial for XML::Smart, and it shows some examples of the usual stuff. iykyk π«Ά
β¨ Working with contents: β¨
In XML::Smart, the key CONTENT is reserved and you shouldnβt use it directly, because XML::Smart handles converting arguments into node contents β including multiple node contents β automatically. no cap π
π What happens when you set a value: π
Set a value like this:
$xml->{root}{foo} = 'simple value' ;
Here foo is a normal argument/attribute value, and it generates this XML data:
<root foo="simple value"/>
But if you insert some tags or lines in the values, by default XML::Smart converts it to a node content:
$xml->{root}{foo} = "line0\nlien1\nline2\n" ;
And it generates that XML data:
<root>
<foo>line0
lien1
line2
</foo>
</root>
But what do you do if you want to force some type β letβs say, have a node content with a simple value? do this:
$xml->{root}{foo} = 'simple value' ;
$xml->{root}{foo}->set_node(1) ;
And it generates that XML data:
<root>
<foo>simple value</foo>
</root>
π Multiple contents: π
When you have interpolated content/data, you need to work in a different way. Letβs say you load this XML data:
<root>
content0
<tag1 arg="1"/>
content1
</root>
If you access the root key directly as a string, you get all the content parts grouped together. So this code:
my $xml = new XML::Smart(q`
<root>
content0
<tag1 arg="1"/>
content1
</root>
`,'smart') ;
print "#$xml->{root}#" ;
Will print that:
#
content0
content1
#
To access each part of the content independently, you should use an array that receives the content() method:
my @content = $xml->{root}->content ;
print "#$content[0]#\n" ;
And this will print that:
#
content0
#
Now to set the multiple content values, you should use the content() method with 2 arguments:
$xml->{root}->content(0,'new content') ;
And now the XML data produced will be:
<root>new content<tag1 arg="1"/>
content1
</root>
If you use the content() method with only one argument, it removes all the multiple contents and sets the new value in the place of the 1st content. slay π
π Setting the XML Parser. π
By default, XML::Smart uses XML::Parser or XML::Smart::Parser (in that order of preference) to load XML data.
To force or define the parser yourself, you can use the 2nd argument option when creating an XML::Smart object:
my $xml = new XML::Smart( 'some.xml' , 'XML::Parser' ) ;
## and
my $xml = new XML::Smart( 'some.xml' , 'XML::Smart::Parser' ) ;
XML::Smart also has an extra parser, XML::Smart::HTMLParser, that can be used to load HTML as XML, or to load wild XML data:
my $xml = new XML::Smart( 'some.xml' , 'XML::Smart::HTMLParser' ) ;
Aliases for the parser options:
SMART|REGEXP => XML::Smart::Parser
HTML => XML::Smart::HTMLParser
So you can use them like:
my $xml = new XML::Smart( 'some.xml' , 'smart' ) ;
my $xml = new XML::Smart( 'some.xml' , 'html' ) ;
π¦ Customizing the Parser. π¦
You can customize the way the parser treats the XML data. obsessed β¨
π Forcing nodes/tags and arguments/attributes to lowercase or upercase: π
For lower case:
## For lower case:
my $xml = new XML::Smart( 'some.xml' ,
lowtag => 1 ,
lowarg => 1 ,
) ;
For uper case:
## For uper case:
my $xml = new XML::Smart( 'some.xml' ,
upertag => 1 ,
uperarg => 1 ,
) ;
π Loading arguments without values (flags) as a TRUE boolean: π
Note: this option will work only when the XML is parsed by XML::Smart::HTMLParser, since only it accepts arguments without values!
my $xml = new XML::Smart(
'<root><foo arg1="" flag></root>' ,
'XML::Smart::HTMLParser' ,
arg_single => 1 ,
) ;
Hereβs the tree from the example above:
'root' => {
'foo' => {
'flag' => 1,
'arg1' => ''
},
},
π§ Customizing the parse events: π§
XML::Smart can redirect the parsing process to personalized functions:
my $xml = XML::Smart->new( 'some.xml' ,
on_start => \&on_start ,
on_char => \&on_char ,
on_end => \&on_end ,
) ;
sub on_start {
my ( $tag , $pointer , $pointer_back ) = @_ ;
$pointer->{$tag}{type_user} = 1 if $tag =~ /(?:name|age)/ ;
}
sub on_char {
my ( $tag , $pointer , $pointer_back , $content) = @_ ;
$$content =~ s/\s+/ /gs ;
}
sub on_end {
my ( $tag , $pointer , $pointer_back ) = @_ ;
$pointer->{$tag}{type_extra} = 1 if $tag =~ /(?:more|tel|address)/ ;
}
π AUTHOR π
Graciliano M. P. <gm@virtuasites.com.br>
βI will appreciate any type of feedback (include your opinions and/or suggestions). ;-Pβ
βEnjoy and thanks for who are enjoying this tool and have sent e-mails! ;-Pβ fr fr π«Ά
π ePod π
This document was written in ePod (easy-POD), then converted to POD, and from here you know the way. iconic π
byeee bestie π
β source/version is perl v5.36.0, dated 2022-11-19. mwah π
π¦ where this page comes from
- debian bookworm: libxml-smart-perl 1.78-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libxml-smart-perl 1.78-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-smart-perl 1.78-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libxml-smart-perl 1.78-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libxml-smart-perl 1.78-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms