XML Creation Documentation

admin | IMS
28 Apr 2010

In the previous lab, I had to document how to go about doing the XML creation. I took a look at all different functions in Core.php pertaining to XML, and I experimented with code before I completed the documentation. I created the documentation in readme style so that anyone can easily understand what’s going on. Here is what I came up with:

To add the XML, first you should how your XML tree needs to look like.

There are three functions to create/modify XML

1)createTag – This function will create an XML tag, and if passed, put an item inside it.

For example: if you say $surveyTag = $this->core->createTag(‘Survey’, ‘Item 1′);

it will return an XML tree like:

Item 1

2)putTagInside – This function will put an XML tag inside the other.

For example: if you say

$outer=$this->core->createTag('Outer');
$inner = $this->core->createTag('Inner');
$this->core->putTagInside($outer, $inner);

it will return an XML tree like:

<Outer><Inner/></Outer>

3)createTagInside – This function merges above two functions, and creates an XML tag, puts an item inside it (if passed) and adds the created tag to the outer tag.

For example: if you say

$parentTag = $this->core->createTag('Parent');
$surveyTag =  $this->core->createTagInside($parentTag, 'Survey', 'Survey 1');

it will return an XML tree like:

<Parent>
<Survey>Survey 1</Survey>
</Parent>

Application Example

For example: If you are adding a Survey tag to the XML your tree will be something like,

<Survey>
<Child>
<ID>1</ID>
<URL>localhost</URL>
<Text>This is some text</Text>
</Child>
<Child>
<ID>2</ID>
<URL>google.com</URL>
<Text>This is some text2</Text>
</Child>
</Survey>

First you will be creating the Survey tag and adding it to the XML like:

$surveyTag = $this->core->createTagInside($this->getXml(), 'Survey');

In the above line of code, you are creating a new tag called Survey, and storing the reference in $surveyTag. Then you should collect your data, create the next child node.

$childTag = $this->core->createTagInside($surveyTag, 'Child');

Just like previously, you’re creating a new tag called Child and storing it in $childTag.

then you would do,

$this->core->createTagInside($childTag, 'ID', 1);

In the above line of code you don’t need the reference anymore, but you can still store it.

So to create that kind of XML your final code would be:

$surveyTag = $this->core->createTagInside($this->getXml(), 'Survey');
$childTag = $this->core->createTagInside($surveyTag, 'Child');
$this->core->createTagInside($childTag, 'ID', 1);
$this->core->createTagInside($childTag, 'URL', 'localhost');
$this->core->createTagInside($childTag, 'Text', 'This is some text');
$childTag = $this->core->createTagInside($surveyTag, 'Child');
$this->core->createTagInside($childTag, 'ID', 2);
$this->core->createTagInside($childTag, 'URL', 'google.com');
$this->core->createTagInside($childTag, 'Text', 'This is some text2');

Leave a Reply