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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract xml tags

Status
Not open for further replies.

Bugee

MIS
Feb 11, 2005
1
US
Hi,

This might be a stupid question, but I'm new to perl and xml.

Is there a way to get the name of xml tags from a xml file using SML::SIMPLE.

e.g.

<root>
<root2>
<tag1>some value</tag1>
<tag2>some value</tag2>
<tag3>some value</tag3>
</root2>
</root>

I know inorder to get the values within the tags, I need to know the tags first. So is there an easy way to get the name of the tags

In the above example, I need to get "tag1, tag2 and tag3"

then I can use xml::simple to retrieve the data.

Thanks for the help..

 
[tt]
<root>
<root2>
<tag1>some value</tag1>
<tag2>some value</tag2>
<tag3>some value</tag3>
</root2>
</root>
[/tt]
becomes
[tt]
root
root2
tag1
tag2
tag3
[/tt]
Run with
[tt]
awk -f xmltags.awk test.xml
[/tt]
xmltags.awk is
Code:
BEGIN { RS="<"; FS=">" }
!/^\// { print $1 }
 
XML::Simple reads you XML file into a hash of arrays of hashes of arrays of ... depending on the level of nestedness (if that is a word) of your XML document. You can then walk through the structure, with the hash keys being the tags. So
Code:
use strict;
use XML::Simple;
use Data::Dumper;

my $xml = XMLin("some.xml", ForceArray => 1) or die "Can't read XML file";

print Dumper($xml);
will show you what you've got. I don't know how well it handles attributes, or how you can tell what's a tag and what's an attribute, but there seem to be some options you can set (see the documentation).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top