[DCPM] shorthand required

Neil Williams linux at codehelp.co.uk
Thu Feb 15 07:50:14 PST 2007


http://search.cpan.org/dist/XML-QOFQSF/lib/XML/QOFQSF.pm

This module is getting quite laborious. I'm wondering if there is a shorthand for handling structs of unknown composition.

Current code for parsing just one struct from XML.

			if ($g->{type} eq 'pilot_address')
			{
				my $c = new Contact;
				$c->entryCity($g->{'string'}->[0]->{content});
				$c->entryCustom4($g->{'string'}->[1]->{content});
				$c->entryPhone1($g->{'string'}->[2]->{content});
				$c->entryZip($g->{'string'}->[3]->{content});
				$c->entryLastname($g->{'string'}->[4]->{content});
				$c->entryPhone2($g->{'string'}->[5]->{content});
				$c->entryNote($g->{'string'}->[6]->{content});
				$c->category($g->{'string'}->[7]->{content});
				$c->entryFirstname($g->{'string'}->[8]->{content});
				$c->entryPhone3($g->{'string'}->[9]->{content});
				$c->entryTitle($g->{'string'}->[10]->{content});
				$c->entryPhone4($g->{'string'}->[11]->{content});
				$c->entryCompany($g->{'string'}->[12]->{content});
				$c->entryPhone5($g->{'string'}->[13]->{content});
				$c->entryState($g->{'string'}->[14]->{content});
				$c->entryCustom1($g->{'string'}->[15]->{content});
				$c->entryAddress($g->{'string'}->[16]->{content});
				$c->entryCustom2($g->{'string'}->[17]->{content});
				$c->entryCountry($g->{'string'}->[18]->{content});
				$c->entryCustom3($g->{'string'}->[19]->{content});
				$c->guid($g->{'guid'}->[0]->{content});
				push @contacts, $c;
			}

to parse the XML, now:

	foreach my $a (@_)
	{
		$count++;
		$writer->startTag("object", 'type' => 'pilot_address', 'count' => "$count");
		$writer->startTag('string', 'type' => 'entryCity');
		$writer->characters($a->entryCity) if ($a->entryCity);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryCustom4');
		$writer->characters($a->entryCustom4) if ($a->entryCustom4);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryPhone1');
		$writer->characters($a->entryPhone1) if ($a->entryPhone1);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryZip');
		$writer->characters($a->entryZip) if ($a->entryZip);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryLastname');
		$writer->characters($a->entryLastname) if ($a->entryLastname);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryPhone2');
		$writer->characters($a->entryPhone2) if ($a->entryPhone2);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryNote');
		$writer->characters($a->entryNote) if ($a->entryNote);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'category');
		$writer->characters($a->category) if ($a->category);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryFirstname');
		$writer->characters($a->entryFirstname) if ($a->entryFirstname);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryPhone3');
		$writer->characters($a->entryPhone3) if ($a->entryPhone3);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryTitle');
		$writer->characters($a->entryPhone3) if ($a->entryTitle);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryPhone4');
		$writer->characters($a->entryPhone4) if ($a->entryPhone4);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryCompany');
		$writer->characters($a->entryCompany) if ($a->entryCompany);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryPhone5');
		$writer->characters($a->entryPhone5) if ($a->entryPhone5);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryState');
		$writer->characters($a->entryState) if ($a->entryState);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryCustom1');
		$writer->characters($a->entryCustom1) if ($a->entryCustom1);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryAddress');
		$writer->characters($a->entryPhone3) if ($a->entryAddress);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryCustom2');
		$writer->characters($a->entryCustom2) if ($a->entryCustom2);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryCountry');
		$writer->characters($a->entryCountry) if ($a->entryCountry);
		$writer->endTag('string');
		$writer->startTag('string', 'type' => 'entryCustom3');
		$writer->characters($a->entryCustom3) if ($a->entryCustom3);
		$writer->endTag('string');
		$writer->startTag('guid', 'type' => 'guid');
		$writer->characters($a->guid) if ($a->guid);
		$writer->endTag('guid');
		$writer->endTag('object');
	}

to write new XML. (New routine, planned for v0.03 which is not in CPAN yet.)

Now this is one of the simpler structs that contains nearly all string values. Others include booleans, integers and key-value pairs, amongst others. There are currently over a dozen structs to be supported by this module with lots more in the future.

I also need to convert ALL these structs to and from other formats - like SQLite.

Is there some way of iterating through:

struct (Contact => {
	"entryCity" => '$',
	"entryCustom4" => '$',
	"entryPhone1" => '$',
	"entryZip" => '$',
	"entryLastname" => '$',
	"entryPhone2" => '$',
	"entryNote" => '$',
	"category" => '$',
	"entryFirstname" => '$',
	"entryPhone3" => '$',
	"entryTitle" => '$',
	"entryPhone4" => '$',
	"entryCompany" => '$',
	"entryPhone5" => '$',
	"entryState" => '$',
	"entryCustom1" => '$',
	"entryAddress" => '$',
	"entryCustom2" => '$',
	"entryCountry" => '$',
	"entryCustom3" => '$',
	"guid" => '$',
});

(along with some form of relation between the fields in the struct and the TYPE of data contained within?) Some structs also contain references to instances of other structs.

Or some way of encoding the above struct, WITH the 'type' data, so that the structs, the parser and the writer can be generated on-the-fly?

This module could get very large if I can't find a way to shorten the setup.

Any ideas?

--

Neil Williams
=============
http://www.data-freedom.org/
http://www.nosoftwarepatents.com/
http://www.linux.codehelp.co.uk/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/devoncornwall-pm/attachments/20070215/4376daf0/attachment.bin 


More information about the Devoncornwall-pm mailing list