Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building XML file in Perl: Relationship between createAttribute and addChild

Tags:

xml

perl

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?

like image 364
Amelio Vazquez-Reina Avatar asked Sep 10 '12 19:09

Amelio Vazquez-Reina


2 Answers

An attribute is one type of child.

like image 85
stu42j Avatar answered Sep 30 '22 00:09

stu42j


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.

like image 40
choroba Avatar answered Sep 29 '22 23:09

choroba