I am learning to use XML:LibXML for a project in Perl and I saw this example.
The goal is to build 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 addChild to create story under assets:
my $story = $dom->createElement('story');
and he then also uses addChild (in combination with createAttribute) to specify the attributes for story:
$story->addChild( $dom->createAttribute( id => 1234));
Looking at the XML example above (without knowing much about XML), id="1234" is not a child of story but rather an attribute of it, so why do we use addChild in this last line?
By calling
createAttributeorcreateElement, you create a new node. By callingaddChild, you attach such a node into its parent. There are several types of nodes in XML: elements, attributes, but also text, comments, or processing instructions.