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?
An attribute is one type of child.
By calling createAttribute
or createElement
, you create a new node. By calling addChild
, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With