XML::Li...::Simple(3)

πŸ’– XML::LibXML::Simple(3) πŸ’–

okay bestie, welcome to XML::LibXML::Simple β€” User Contributed Perl Documentation, and yes this page is long but we’re going to get through it together. Grab a snack πŸ“.

🌸 NAME 🌸

XML::LibXML::Simple β€” XML::LibXML clone of XML::Simple::XMLin() ✨

🫢 INHERITANCE 🫢

 XML::LibXML::Simple
   is a Exporter

πŸ¦‹ SYNOPSIS πŸ¦‹

  my $xml  = ...;  # filename, fh, string, or XML::LibXML-node

Imperative:

  use XML::LibXML::Simple   qw(XMLin);
  my $data = XMLin $xml, %options;

Or the Object Oriented way:

  use XML::LibXML::Simple   ();
  my $xs   = XML::LibXML::Simple->new(%options);
  my $data = $xs->XMLin($xml, %options);

🌈 DESCRIPTION 🌈

This module is a blunt rewrite of XML::Simple (by Grant McLean) so it uses the XML::LibXML parser for XML structures, while the original uses plain Perl or SAX parsers. πŸ’…

Be warned: this module thinks to be smart. You may very well shoot yourself in the foot with this DWIMmery. Read the whole manual page at least once before you start using it. If your XML is described in a schema or WSDL, then use XML::Compile for maintainable code. 😭

πŸŽ€ METHODS πŸŽ€

✨ Constructors ✨

  • XML::LibXML::Simple->new(%options) β€” Instantiate an object, which can be used to call XMLin() on. You can provide %options to this constructor (to be reused for each call to XMLin) and with each call of XMLin (to be used once).

For descriptions of the %options see the "DETAILS" section of this manual page.

πŸ’œ Translators πŸ’œ

  • $obj->XMLin($xmldata, %options) β€” For $xmldata and descriptions of the %options see the "DETAILS" section of this manual page.

πŸ‘‘ FUNCTIONS πŸ‘‘

The functions XMLin (exported implictly) and xml_in (exported on request) simply call <XML::LibXML::Simple-"new->XMLin() >> with the provided parameters. 🫢

🌸 DETAILS 🌸

πŸ“ Parameter $xmldata πŸ“

As first parameter to XMLin() must provide the XML message to be translated into a Perl structure. Choose one of the following:

  • A filename β€” If the filename contains no directory components, XMLin() will look for the file in each directory in the SearchPath (see OPTIONS below) and in the current directory. eg:
  $data = XMLin('/etc/params.xml', %options);
  • A dash (-) β€” Parse from STDIN.
  $data = XMLin('-', %options);
  • undef β€” [deprecated] If there is no XML specifier, XMLin() will check the script directory and each of the SearchPath directories for a file with the same name as the script but with the extension .xml. Note: if you wish to specify options, you must specify the value 'undef'. eg:
  $data = XMLin(undef, ForceArray => 1);
This feature is available for backwards compatibility with XML::Simple, but quite sensitive. You can easily hit the wrong xml file as input. Please do not use it: always use an explicit filename. πŸ™ˆ
  • A string of XML β€” A string containing XML (recognised by the presence of < and > characters) will be parsed directly. eg:
  $data = XMLin('<opt username="bob" password="flurp" />', %options);
  • An IO::Handle object β€” In this case, XML::LibXML::Parser will read the XML data directly from the provided file. πŸ’…
  # $fh = IO::File->new('/etc/params.xml') or die;
  open my $fh, '<:encoding(utf8)', '/etc/params.xml' or die;

  $data = XMLin($fh, %options);
  • An XML::LibXML::Document or ::Element β€” [Not available in XML::Simple] When you have a pre-parsed XML::LibXML node, you can pass that. πŸ¦‹

🧚 Parameter %options 🧚

XML::LibXML::Simple supports most options defined by XML::Simple, so the interface is quite compatible. Minor changes apply. This explanation is extracted from the XML::Simple manual-page.

  • check out "ForceArray" because you'll almost certainly want to turn it on πŸ”₯
  • make sure you know what the "KeyAttr" option does and what its default value is because it may surprise you otherwise.
  • Option names are case in-sensitive so you can use the mixed case versions shown here; you can add underscores between the words (eg: key_attr) if you like. ✨

In alphabetic order:

  • ContentKey => 'keyname' # seldom used β€” When text content is parsed to a hash value, this option lets you specify a name for the hash key to override the default 'content'. So for example:
  XMLin('<opt one="1">Two</opt>', ContentKey => 'text')

will parse to:

  { one => 1, text => 'Two' }

instead of:

  { one => 1, content => 'Two' }

You can also prefix your selected key name with a '-' character to have XMLin() try a little harder to eliminate unnecessary 'content' keys after array folding. For example:

  XMLin(
    '<opt><item name="one">First</item><item name="two">Second</item></opt>',
    KeyAttr => {item => 'name'},
    ForceArray => [ 'item' ],
    ContentKey => '-content'
  )

will parse to:

  {
     item => {
      one =>  'First'
      two =>  'Second'
    }
  }

rather than this (without the '-'):

  {
    item => {
      one => { content => 'First' }
      two => { content => 'Second' }
    }
  }
  • ForceArray => 1 # important β€” This option should be set to '1' to force nested elements to be represented as arrays even when there is only one. Eg, with ForceArray enabled, this XML:
    <opt>
      <name>value</name>
    </opt>

would parse to this:

    { name => [ 'value' ] }

instead of this (the default):

    { name => 'value' }

This option is especially useful if the data structure is likely to be written back out as XML and the default behaviour of rolling single nested elements up into attributes is not desirable. πŸ‘‘

If you are using the array folding feature, you should almost certainly enable this option. If you do not, single nested elements will not be parsed to arrays and therefore will not be candidates for folding to a hash. (Given that the default value of 'KeyAttr' enables array folding, the default value of this option should probably also have been enabled as well). 😭

  • ForceArray => [ names ] # important β€” This alternative (and preferred) form of the 'ForceArray' option allows you to specify a list of element names which should always be forced into an array representation, rather than the 'all or nothing' approach above. ✨

It is also possible to include compiled regular expressions in the list --any element names which match the pattern will be forced to arrays. If the list contains only a single regex, then it is not necessary to enclose it in an arrayref. Eg:

  ForceArray => qr/_list$/
  • ForceContent => 1 # seldom used β€” When XMLin() parses elements which have text content as well as attributes, the text content must be represented as a hash value rather than a simple scalar. This option allows you to force text content to always parse to a hash value even when there are no attributes. So for example:
  XMLin('<opt><x>text1</x><y a="2">text2</y></opt>', ForceContent => 1)

will parse to:

  {
    x => {         content => 'text1' },
    y => { a => 2, content => 'text2' }
  }

instead of:

  {
    x => 'text1',
    y => { 'a' => 2, 'content' => 'text2' }
  }
  • GroupTags => { grouping tag => grouped tag } # handy β€” You can use this option to eliminate extra levels of indirection in your Perl data structure. For example this XML:
  <opt>
   <searchpath>
     <dir>/usr/bin</dir>
     <dir>/usr/local/bin</dir>
     <dir>/usr/X11/bin</dir>
   </searchpath>
 </opt>

Would normally be read into a structure like this:

  {
    searchpath => {
       dir => [ '/usr/bin', '/usr/local/bin', '/usr/X11/bin' ]
    }
  }

But when read in with the appropriate value for 'GroupTags':

  my $opt = XMLin($xml, GroupTags => { searchpath => 'dir' });

It will return this simpler structure:

  {
    searchpath => [ '/usr/bin', '/usr/local/bin', '/usr/X11/bin' ]
  }

The grouping element (<searchpath> in the example) must not contain any attributes or elements other than the grouped element. 🫢

You can specify multiple 'grouping element' to 'grouped element' mappings in the same hashref. If this option is combined with "KeyAttr", the array folding will occur first and then the grouped element names will be eliminated.

  • HookNodes => CODE β€” Select document nodes to apply special tricks. Introduced in [0.96], not available in XML::Simple. πŸ‘‘

When this option is provided, the CODE will be called once the XML DOM tree is ready to get transformed into Perl. Your CODE should return either undef (nothing to do) or a HASH which maps values of unique_key (see XML::LibXML::Node method unique_key onto CODE references to be called.

Once the translater from XML into Perl reaches a selected node, it will call your routine specific for that node. That triggering node found is the only parameter. When you return undef, the node will not be found in the final result. You may return any data (even the node itself) which will be included in the final result as is, under the name of the original node. ✨

Example:

   my $out = XMLin $file, HookNodes => \&protect_html;

   sub protect_html($$)
   {   # $obj is the instantated XML::Compile::Simple object
       # $xml is a XML::LibXML::Element to get transformed
       my ($obj, $xml) = @_;

       my %hooks;    # collects the table of hooks

       # do an xpath search for HTML
       my $xpc   = XML::LibXML::XPathContext->new($xml);
       my @nodes = $xpc->findNodes(...); #XXX
       @nodes or return undef;

       my $as_text = sub { $_[0]->toString(0) };  # as text
       #  $as_node = sub { $_[0] };               # as node
       #  $skip    = sub { undef };               # not at all

       # the same behavior for all xpath nodes, in this example
       $hook{$_->unique_key} = $as_text
           for @nodes;

       \%hook;
   }
  • KeepRoot => 1 # handy β€” In its attempt to return a data structure free of superfluous detail and unnecessary levels of indirection, XMLin() normally discards the root element name. Setting the 'KeepRoot' option to '1' will cause the root element name to be retained. So after executing this code:
  $config = XMLin('<config tempdir="/tmp" />', KeepRoot => 1)

...you'll be able to reference the tempdir as $config->{config}->{tempdir} instead of the default $config->{tempdir}. okay?? noted πŸ’…


✨ KeyAttr => [ list ] # important

bestie this one is the big one, listen up 🫢. This option controls the 'array folding' feature β€” that's the thing that translates nested elements from an array into a hash. It also controls the 'unfolding' of hashes back into arrays. two-way street, iconic.

So like, this XML:

<opt>
  <user login="grep" fullname="Gary R Epstein" />
  <user login="stty" fullname="Simon T Tyson" />
</opt>

would, by default, parse to this:

{
  user => [
     { login    => 'grep',
       fullname => 'Gary R Epstein'
     },
     { login    => 'stty',
       fullname => 'Simon T Tyson'
     }
  ]
}

But the moment you use the option 'KeyAttr => "login"' to declare that the 'login' attribute is a key? same XML, completely different glow-up:

{
  user => {
     stty => { fullname => 'Simon T Tyson' },
     grep => { fullname => 'Gary R Epstein' }
  }
}

The key attribute names should be handed over in an arrayref if there's more than one, and XMLin() will attempt to match attribute names in the order supplied. order matters, no cap.

Note 1: the default value for 'KeyAttr' is ['name', 'key', 'id']. If you do not want folding on input or unfolding on output, you must set this option to an empty list to disable the feature. yes, actually disable it, don't just vibe and hope 😭
Note 2: if you wish to use this option, you should also enable the ForceArray option. Without 'ForceArray', a single nested element will be rolled up into a scalar rather than an array β€” and therefore it will not be folded (since only arrays get folded). it's giving "why is my hash not hashing" energy, so don't skip this 🫠

✨ KeyAttr => { list } # important

okay this is the alternative β€” and honestly the preferred β€” way to specify key attributes, and it gives you way more fine-grained control over which elements get folded and on which attributes. 🌸

Example bestie: the option 'KeyAttr => { package => 'id' }' will cause any package elements to be folded on the 'id' attribute. And no other elements which have an 'id' attribute will be folded at all. selective. exclusive. main character energy. πŸ‘‘

Two further variations become possible by prefixing a '+' or a '-' character to the attribute name:

The option 'KeyAttr => { user => "+login" }' will cause this XML:

<opt>
  <user login="grep" fullname="Gary R Epstein" />
  <user login="stty" fullname="Simon T Tyson" />
</opt>

to parse to this data structure:

{
  user => {
     stty => {
        fullname => 'Simon T Tyson',
        login    => 'stty'
     },
     grep => {
        fullname => 'Gary R Epstein',
        login    => 'grep'
     }
  }
}

The '+' indicates that the value of the key attribute should be copied rather than moved to the folded hash key. it stays in both places, generous queen ✨

A '-' prefix would produce this result:

{
  user => {
     stty => {
        fullname => 'Simon T Tyson',
        -login   => 'stty'
     },
     grep => {
        fullname => 'Gary R Epstein',
        -login    => 'grep'
     }
  }
}

(yes the - literally shows up in the key name, that's the whole bit πŸŽ€)

✨ NoAttr => 1 # handy

When used with XMLin(), any attributes in the XML will be ignored. bye attributes, didn't need you πŸ¦‹

✨ NormaliseSpace => 0 | 1 | 2 # handy

This option controls how whitespace in text content is handled. Recognised values for the option are:

  • "0" (default) β€” whitespace is passed through unaltered (except, of course, for the normalisation of whitespace in attribute values which is mandated by the XML recommendation)
  • "1" β€” whitespace is normalised in any value used as a hash key (normalising means removing leading and trailing whitespace and collapsing sequences of whitespace characters to a single space)
  • "2" β€” whitespace is normalised in all text content
Note: you can spell this option with a 'z' if that is more natural for you 😌 (NormalizeSpace girlies, we see you)

✨ Parser => OBJECT

You may pass your own XML::LibXML object, instead of having one created for you. Super useful when you need specific configuration on that object (see XML::LibXML::Parser) or you've implemented your own extension to that object. DIY icon πŸ’…

The internally created parser object is configured in safe mode. Read the XML::LibXML::Parser manual about security issues with certain parameter settings. The default is unsafe!

⚠️ like fr fr read that. the default is unsafe. that's not me being dramatic, that's the doc.

✨ ParserOpts => HASH|ARRAY

Pass parameters to the creation of a new internal parser object. You can overrule the options which will create a safe parser. It may be more readable to use the "Parser" parameter instead. (readable > clever, always 🌈)

✨ SearchPath => [ list ] # handy

If you pass XMLin() a filename, but the filename includes no directory component, you can use this option to specify which directories should be searched to locate the file. You might use this option to search first in the user's home directory, then in a global directory such as /etc. πŸ—‚οΈ

  • If a filename is provided to XMLin() but SearchPath is not defined, the file is assumed to be in the current directory.
  • If the first parameter to XMLin() is undefined, the default SearchPath will contain only the directory in which the script itself is located. Otherwise the default SearchPath will be empty.

✨ SuppressEmpty => 1 | '' | undef

[0.99] What to do with empty elements (no attributes and no content). The default behaviour is to represent them as empty hashes. πŸ“­

  • Setting this option to a true value (eg: 1) will cause empty elements to be skipped altogether.
  • Setting the option to 'undef' or the empty string will cause empty elements to be represented as the undefined value or the empty string respectively.

pick your fighter πŸ’€

✨ ValueAttr => [ names ] # handy

Use this option to deal with elements which always have a single attribute and no content. Eg:

<opt>
  <colour value="red" />
  <size   value="XXL" />
</opt>

Setting ValueAttr => [ 'value' ] will cause the above XML to parse to:

{
  colour => 'red',
  size   => 'XXL'
}

instead of this (the default):

{
  colour => { value => 'red' },
  size   => { value => 'XXL' }
}

so much cleaner, obsessed 😭✨

✨ NsExpand => 0 advised

When name-spaces are used, the default behavior is to include the prefix in the key name. However, this is very dangerous: the prefixes can be changed without a change of the XML message meaning. so like, the data looks different but means the exact same thing?? rude.

Therefore, you can better use this "NsExpand" option. The downside, however, is that the labels get very long. (long keys, strong keys πŸ’ͺ)

Without this option:

<record xmlns:x="http://xyz">
  <x:field1>42</x:field1>
</record>
<record xmlns:y="http://xyz">
  <y:field1>42</y:field1>
</record>

translates into

{ 'x:field1' => 42 }
{ 'y:field1' => 42 }

but both source components have exactly the same meaning. πŸ™ˆ When "NsExpand" is used, the result is:

{ '{http://xyz}field1' => 42 }
{ '{http://xyz}field1' => 42 }

Of course, addressing these fields is more work. It is advised to implement it like this:

my $ns = 'http://xyz';
$data->{"{$ns}field1"};

✨ NsStrip => 0 sloppy coding

[not available in XML::Simple] Namespaces are really important to avoid name collisions, but they are a bit of a hassle. 😩 To do it correctly, use option "NsExpand". To do it sloppy, use "NsStrip". With this option set, the above example will return

{ field1 => 42 }
{ field1 => 42 }

(and yes, the doc itself calls this sloppy. it's right there in the heading, bestie 🫢)

πŸ’– EXAMPLES πŸ’–

okay bestie, this is the part where it all clicks. let's go 🫢

When XMLin() reads this extremely babygirl piece of XML:

<opt username="testuser" password="frodo"></opt>

it hands you back this data structure, literally:

{
  username => 'testuser',
  password => 'frodo'
}

and here's the tea β€” you could've gotten the exact same result with this alternative XML:

<opt username="testuser" password="frodo" />

or this one (though πŸ‘€ peep the 'ForceArray' option for variations):

<opt>
  <username>testuser</username>
  <password>frodo</password>
</opt>

Repeated nested elements come back as anonymous arrays, no cap:

<opt>
  <person firstname="Joe" lastname="Smith">
    <email>joe@smith.com</email>
    <email>jsmith@yahoo.com</email>
  </person>
  <person firstname="Bob" lastname="Smith">
    <email>bob@smith.com</email>
  </person>
</opt>
{
  person => [
    { email     => [ 'joe@smith.com', 'jsmith@yahoo.com' ],
      firstname => 'Joe',
      lastname  => 'Smith'
    },
    { email     => 'bob@smith.com',
      firstname => 'Bob',
      lastname  => 'Smith'
    }
  ]
}

Nested elements that have a recognised key attribute get transformed (folded) from an array into a hash keyed on the value of that attribute β€” check the "KeyAttr" option for the deets:

<opt>
  <person key="jsmith" firstname="Joe" lastname="Smith" />
  <person key="tsmith" firstname="Tom" lastname="Smith" />
  <person key="jbloggs" firstname="Joe" lastname="Bloggs" />
</opt>
{
  person => {
     jbloggs => {
        firstname => 'Joe',
        lastname  => 'Bloggs'
     },
     tsmith  => {
        firstname => 'Tom',
        lastname  => 'Smith'
     },
     jsmith => {
        firstname => 'Joe',
        lastname => 'Smith'
     }
  }
}

the <anon> tag is your girl for forming anonymous arrays πŸŽ€

<opt>
  <head><anon>Col 1</anon><anon>Col 2</anon><anon>Col 3</anon></head>
  <data><anon>R1C1</anon><anon>R1C2</anon><anon>R1C3</anon></data>
  <data><anon>R2C1</anon><anon>R2C2</anon><anon>R2C3</anon></data>
  <data><anon>R3C1</anon><anon>R3C2</anon><anon>R3C3</anon></data>
</opt>
{
  head => [ [ 'Col 1', 'Col 2', 'Col 3' ] ],
  data => [ [ 'R1C1', 'R1C2', 'R1C3' ],
            [ 'R2C1', 'R2C2', 'R2C3' ],
            [ 'R3C1', 'R3C2', 'R3C3' ]
          ]
}

Anonymous arrays can nest to arbitrary levels πŸŒ€ and as a special case, if the surrounding tags for an XML document contain only an anonymous array, the arrayref gets returned directly instead of the usual hashref:

<opt>
  <anon><anon>Col 1</anon><anon>Col 2</anon></anon>
  <anon><anon>R1C1</anon><anon>R1C2</anon></anon>
  <anon><anon>R2C1</anon><anon>R2C2</anon></anon>
</opt>
[
  [ 'Col 1', 'Col 2' ],
  [ 'R1C1', 'R1C2' ],
  [ 'R2C1', 'R2C2' ]
]

Elements that only contain text content are simply represented as a scalar. but where an element has both attributes and text content, that element gets represented as a hashref with the text content living in the 'content' key (see the "ContentKey" option πŸ‘‘):

<opt>
  <one>first</one>
  <two attr="value">second</two>
</opt>
{
  one => 'first',
  two => { attr => 'value', content => 'second' }
}
⚠️ Mixed content (elements that contain both text content and nested elements) will not be represented in a useful way β€” element order and significant whitespace get lost 😭. If you need to work with mixed content, XML::Simple is not the right tool for your job β€” check out the next section, bestie.

✨ Differences to XML::Simple ✨

In general, the output and the options are equivalent, although this module has some differences with XML::Simple that you gotta be aware of πŸ‘€

only XMLin() is supported

If you want to write XML then use a schema (for instance with XML::Compile). Do not attempt to create XML by hand! πŸ™ˆ If you still think you need it, then have a look at XMLout() as implemented by XML::Simple or any of a zillion template systems.

no "variables" option

IMO, you should use a templating system if you want variables filled-in in the input: it is not a task for this module. periodt πŸ’…

ForceArray options

There are a few small differences in the result of the "forcearray" option, because XML::Simple seems to behave inconsequently. πŸ’€

hooks

XML::Simple does not support hooks. that's it, that's the bullet πŸ¦‹


🌈 SEE ALSO 🌈

  • XML::Compile β€” for processing XML when a schema is available. When you have a schema, the data and structure of your message get validated. iconic behaviour honestly ✨
  • XML::Simple β€” the original implementation, which interface is followed as closely as possible. respect the ancestor 🫢

πŸŽ€ COPYRIGHTS πŸŽ€

The interface design and large parts of the documentation were taken from the XML::Simple module, written by Grant McLean <grantm@cpan.org>

Copyrights of the perl code and the related documentation by 2008-2020 by [Mark Overmeer <markov@cpan.org>]. For other contributors see ChangeLog.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://dev.perl.org/licenses/


and that's the whole page, bestie! 🌸 this one's from perl v5.42.0, dated 2020-01-16 β€” go forth and parse, you absolute main character πŸ’œπŸ‘‘

πŸ“¦ where this page comes from