SPUG: Generating XML

Parr, Ryan Ryan.Parr at wwireless.com
Tue Nov 20 22:58:17 CST 2001


What I ended up doing in my own project was working up a module that
traversed a data structure and created the XML from that. For instance:

{
	"PS" => [
	{
		"PROCESS" => [
        	{
			"TIME" => "0:00.56",
			"TTY" => "??",
			"STIME" => "2Nov01",
			"C" => 0,
			"COMMAND" => "(swapper)",
			"UID" => 0,
			"PID" => 0,
			"PPID" => 0
		},
	}
	];
}

becomes:

<?xml version="1.0" ?> 
<PS>
	<PROCESS>
  		<TIME>0:00.56</TIME> 
  		<TTY>??</TTY> 
  		<STIME>2Nov01</STIME> 
  		<C>0</C> 
  		<COMMAND>(swapper)</COMMAND> 
  		<UID>0</UID> 
  		<PID>0</PID> 
  		<PPID>0</PPID> 
  	</PROCESS>
</PS>

Easy to use, and not terribly slow. It encodes all non-word characters
properly and indents for human readibility. It doesn't support tag meta data
or parameters, but that would be easy enough to implement I think. If you
think this could help you let me know and I'll send the mod.

-- Ryan


-----Original Message-----
From: Asim Jalis [mailto:asimjalis at yahoo.com]
Sent: Tuesday, November 20, 2001 3:27 PM
To: spug-list at pm.org
Subject: Re: SPUG: Generating XML


A fourth style is to just use string
concatenation:

my $xml = "<abc>"; 
for my $x (@list) { 
  $xml .= "<data>$x</data>"; 
} 
$xml .= "</abc>";

The advantages are: it is simple, easy to
understand, and probably faster than the other
alternatives.

Separating code from formatting using templates is
useful with HTML because the formatting changes so
much. With XML layout is usually pretty stable. If
your schema or DTD changes you will probably have
to change your code anyway.


__________________________________________________
Do You Yahoo!?
Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/





More information about the spug-list mailing list