Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML::Simple -- starting out

Status
Not open for further replies.

gregaug

Programmer
Sep 9, 2005
61
US
I've never used XML::Simple before, and I'm a little confused on how to start. I have a level tag <Instance> (it's the highest level) and I want to see what the description is (i.e. <Instance description="blah">), but there could be more than just description listed in the tag. How do I pull out only the description? thanks for any help.
 
Hi

Can you post some of the xml file?

Cheers

DM

dmazzini
GSM System and Telecomm Consultant

 
<instance description="blah" id="123" user="usrid">
<listener port="1050" />
**other XML code I don't care about
</instance>

I'm interested in getting the description and the port (and then editing the port). Instance is the highest tag there is.
 
How about something like this:
Code:
use XML::Simple;
my $xs = new XML::Simple;
my $data = $xs->XMLin("sample.xml");

print "Description: ", $data->{'description'}, "\n";
print "Port Number: ", $data->{'listener'}->{'port'}, "\n";

And sample.xml looks like:
Code:
<?xml version='1.0' encoding='utf-8'?>
<instance description="blah" id="123" user="usrid">
	<listener port="1050" />
</instance>
 
Using Data dumper you could see the structure of the XML Document:



use XML::Simple;
use Data::Dumper;

my $cust_xml = XMLin('C:\\Documents and Settings\\Daniel Mazzini\\Desktop\\test.xml', forcearray=>0);


#print Dumper($cust_xml);

foreach my $key (keys (%{$cust_xml->{listener}})){
print "DESCRIPTION: ". $cust_xml->{description} . "\n";
print "ID: ". $cust_xml->{id} . "\n";
print "USER: ". $cust_xml->{user} . "\n";
print "PORT: ". $cust_xml->{listener}->{port} . "\n";
}


dmazzini
GSM System and Telecomm Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top