Creating XML file in Perl: the relationship between createAttribute and addChild

I am studying usage XML:LibXMLfor a project in Perl, and I saw this example .

The goal is to create this XML file:

<?xml version="1.0" encoding="utf-8"?>
<assets xmlns="http://bricolage.sourceforge.net/assets.xsd">
  <story id="1234" type="story">
    <name>Catch as Catch Can</name>
  </story>
</assets>

The author uses addChildto create storyunder assets:

my $story = $dom->createElement('story');

and then also uses addChild(in conjunction with createAttribute) to specify attributes for story:

$story->addChild( $dom->createAttribute( id => 1234));

Looking at the XML example above (without knowing about XML) is id="1234"not a child story , but rather an attribute, so why do we use addChild in this last line?

+5
source share
3

createAttribute createElement, node. addChild, node . XML : , , , .

+2

- .

+3

So how $storyis the XML :: :: LibXML the Element , you may find it more natural to use the methodsetAttribute

my $store = $dom->createElement('story');
$store->setAttribute(id => '1234');

which is shorthand for longer code createAttributeand addChildwhich you do.

+1
source

All Articles