How to get all the xml tags in perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get all the xml tags in perl?
# 1  
Old 05-11-2011
How to get all the xml tags in perl?

Hi,

I have 2 questions:
a) Does getElementsByTagName in xml takes more time?
b) If it takes more time what are the other alternatives used to get the tag names?

Code:
For example:
<Student>
<Studname>aaa</Studname>
<Studno>123</Studno>
</Student>


This is just a sample data. The file will be very huge in practical applications
loads of tags will be there.

Are there any other methods available in parsers to get all the tag names?


Regards
Vanitha
# 2  
Old 05-11-2011
Code:
 #!/usr/bin/perl

use XML::Simple;

use strict;
use warnings;

my $xml_src=XMLin("data.xml");
my @tags = extract_tags($xml_src);
print "@tags\n";


sub extract_tags{
    my $xml_src=shift;
    my (@tags, %tags);
    for my $key (keys %{$xml_src}){
        $tags{$key}++;
        if (ref($xml_src->{$key}) eq 'HASH'){
            map {$_++;} @tags{extract_tags($xml_src->{$key})}
        }
    }
    push @tags , keys %tags;
    return @tags;
}

The above works as a way of extracting a list of tags used in data.xml

Last edited by Skrynesaver; 05-11-2011 at 07:59 AM..
# 3  
Old 05-11-2011
If you are using DOM to process your XML document, then the size of the document directly influences the processing time as DOM parses the entire document into memory before any output is produced.

The alternative, and better option for large XML documents, is stream processing using a SAX or StAX processor.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. Shell Programming and Scripting

Splitting xml file into several xml files using perl

Hi Everyone, I'm new here and I was checking this old post: /shell-programming-and-scripting/180669-splitting-file-into-several-smaller-files-using-perl.html (cannot paste link because of lack of points) I need to do something like this but understand very little of perl. I also check... (4 Replies)
Discussion started by: mcosta
4 Replies

3. Shell Programming and Scripting

How to add Xml tags to an existing xml using shell or awk?

Hi , I have a below xml: <ns:Body> <ns:result> <Date Month="June" Day="Monday:/> </ns:result> </ns:Body> i have a lookup abc.txtt text file with below details Month June July August Day Monday Tuesday Wednesday I need a output xml with below tags <ns:Body> <ns:result>... (2 Replies)
Discussion started by: Nevergivup
2 Replies

4. Shell Programming and Scripting

Perl : to split the tags from xml file

I do have an xml sheet as below where I need the perl script to filter only the hyperlink tags. <cols><col min="1" max="1" width="30.5703125" customWidth="1"/><col min="2" max="2" width="7.140625" bestFit="1" customWidth="1"/> <col min="3" max="3" width="32.28515625" bestFit="1"... (3 Replies)
Discussion started by: scriptscript
3 Replies

5. Shell Programming and Scripting

Compare two xml files while ignoring some xml tags

I've got two different files and want to compare them. File 1 : <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record id="38,557"><columns><column><name>orge... (2 Replies)
Discussion started by: Shaishav Shah
2 Replies

6. Shell Programming and Scripting

Shell Command to compare two xml lines while ignoring xml tags

I've got two different files and want to compare them. File 1 : HTML Code: <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record... (1 Reply)
Discussion started by: Shaishav Shah
1 Replies

7. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

8. Shell Programming and Scripting

How to track the opened tags in xml using perl?

Hi, I have an xml file like this: I am using xml::dom also and libxml. <Nodes> <Node> <NodeName>MainContent</NodeName> <data>1.csv</data> <Node> <NodeName>Status</NodeName> <IsWrapper/> ... (3 Replies)
Discussion started by: vanitham
3 Replies

9. Shell Programming and Scripting

Perl + xml

Hi there, I have need create and read (Handle) XML in perl, a lib with oop support. are you can present suitable library for this? Thanks in advance. (1 Reply)
Discussion started by: Zaxon
1 Replies

10. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies
Login or Register to Ask a Question