[xml_tree]

Description

Link: [xml_tree]
Author: Jason Huck
Category: XML
Version: 8.x
License:
Posted: May. 08, 2006
Updated: Jun. 17, 2006
More by this author...
This type provides additional functionality for the built-in XML type, making it easier to retrieve values from XML documents when the structure is known. It adds the following new member tags:

->atts - Returns a map of the attributes for the current node, instead of an array of pairs.

->attribute(string) - Returns the value of the given attribute for the current node.

->nodename(index) - Returns the given child node by name. If there are multiple nodes of the same name, you can return a specific node by passing an index. If no matching child nodes are found, it will look for an attribute by that name.

->getnode(string) - Same as ->nodename above. Useful if the node name conflicts with an existing member tag, such as "name."

->getnodes - Returns the children of the current node, minus the empty ones that ->children generates on its own.

Parameters

none


Sample Usage

var('testxml') = '\
<?xml version="1.0" ?>
<root>
	<record>
		<thing foo="bar">blah</thing>
		<thing foo="meow">moo</thing>
	</record>
</root>';

var('test') = xml_tree($testxml);

$test->record->thing(2)->contents;

-> moo
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
define_type(
	'tree', 'xml',
	-namespace='xml_',
	-description='Extends and simplifies the built-in XML type.'
);
	define_tag('atts');
		local('out' = map);
		
		iterate(self->attributes, local('i'));
			#out->insert(@#i->first = @#i->second);
		/iterate;
		
		return(@#out);
	/define_tag;
	
	define_tag('attribute', -req='name');
		if(self->attributes->size && self->attributes->find(#name)->size);
			return(@self->attributes->find(#name)->first->second);
		else;
			return('');
		/if;
	/define_tag;
	
	define_tag('getNode', -req='nodename', -opt='count');
		local('matches') = @self->extract('*[translate(local-name(), \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\') = translate(\'' + #nodename + '\', \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\', \'abcdefghijklmnopqrstuvwxyz\')]');
		
		if(!#matches->size);
			return(@self->attribute(#nodename));
		else(#matches->size == 1);
			return(@#matches->first);
		else;
			if(local_defined('count'));
				protect;
					return(@#matches->get(integer(#count)));
					
					handle_error;
						return;
					/handle_error;
				/protect;
			else;
				return(@#matches);
			/if;
		/if;
	/define_tag;
	
	define_tag('getnodes');
		local('out') = @self->children;
		
		iterate(#out, local('i'));
			string(#i)->iswhitespace ? #out->remove(loop_count);
		/iterate;
		
		return(@#out);
	/define_tag;
	
	define_tag('_unknowntag');
		if(params->size);
			return(@self->getnode(tag_name, @params->first));
		else;
			return(@self->getnode(tag_name));
		/if;
	/define_tag;
/define_type;

 

Related Tags



Comments

06/17/2006, Jason Huck
Update
Modified ->getnode() member tag to use an XPath expression instead of ->children for better performance/memory management, per a suggestion by Kyle Jessup.
06/01/2006, Jason Huck
Update
The _unknowntag callback now searches for matching attributes if no matching child nodes are found.
05/30/2006, Jason Huck
Updates
Added ->getnodes member tag and made more things return via reference.
05/17/2006, Jason Huck
Added ->getnode() member tag.
You can now use ->getnode() to access nodes with otherwise conflicting names.
05/08/2006, Jason Huck
Bug Fix
Added error trapping on ->attribute member tag.
Email:


Password:



Newest

Most Popular