Building XML file in Perl: Relationship between createAttribute and addChild

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?

Answers


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.


An attribute is one type of child.


Need Your Help

Twitter API, Adding Hyperlinks

php html twitter

I'm using the Twitter API to grab the last 3 tweets of my timeline using "statuses/user_timeline" function. It grabs the tweets and sends the response, but I need to inject hyperlinks for the HTML...

How should error callback logic interact with retry using android Volley?

android android-volley

Not clear on what type of error handling logic should be in place when Volley retry policy is set to say 3 times. Lets say the network is down and Volley is set to retry 3 times. when will error

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.