#!/usr/bin/perl -w

open (F,"data.txt");
@lines=<F>;
close(F);

use XML::DOM;
$doc = XML::DOM::Document->new();

$root = $doc->createElement('customers');
$doc->appendChild($root);

foreach $line (@lines) {
	chomp $line;
	my ($id,$name,$addr,@orders) = split(/;/,$line);
	
	$custE = $doc->createElement('customer');
	$root->appendChild($custE);
	$custE->setAttribute("id",$id);
	
	$nameE = $doc->createElement('name');
	$custE->appendChild($nameE);
	$text = $doc->createTextNode($name);
	$nameE->appendChild($text);
	
	$addrE = $doc->createElement('address');
	$custE->appendChild($addrE);
	$text = $doc->createTextNode($addr);
	$addrE->appendChild($text);
	
	$ordersE = $doc->createElement('orders');
	$custE->appendChild($ordersE);
	
	foreach $order (@orders) {
		my ($id,$item,$qty) = split(/:/,$order,3);
		
		$orderE = $doc->createElement('order');
		$ordersE->appendChild($orderE);
		
		$orderE->setAttribute("id",$id);
		$orderE->setAttribute("item",$item);
		$orderE->setAttribute("quantity",$qty);
	}
}

open (OUT,">customers.xml");
print OUT $doc->createXMLDecl('1.0')->toString(),"\n";
print OUT $doc->toString();
close(OUT);

$doc->dispose();
