XML::S...torial(3pm)

πŸ“¦
version 1.78 Β· 5 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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