How to get the main node name in xml using perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the main node name in xml using perl?
# 1  
Old 05-23-2011
How to get the main node name in xml using perl?

Hi,

I am having an conf file like this:

Code:
<Main>
	<NODE>
			<NODENAME>FRUITS</NODENAME>
			<NAME>APPLE</NAME>
			<COLOUR>RED</COLOUR>
			<NODE>
			 <IsWrapper/>
			 <NODENAME>SEASONAL</NODENAME>
			 <NODE>
			 <NAME>MANGO</NAME>
			 <COLOUR>GREEN</COLOUR>
			 </NODE>
			 </NODE>
	 </NODE>
</MAIN>

There is tag called IsWrapper means the node name has to be considered in xml file. I am not able to get the node name i.e

Code:
if($node->getElementsByTagName('IsWrapper')) {

#Then need to get the NODENAME i.e "SEASONAL"

}

How can i get the node name ISWRAPPER (i.e SEASONAL)?

Output:
Code:
<FRUITS>
<NAME>APPLE</NAME>
<COLOUR>RED</COLOUR>
<SEASONAL>
<NAME>MANGO</NAME>
<COLOUR>GREEN</COLOUR>
</SEASONAL>
</FRUITS>

Regards
VAnitha

Last edited by pludi; 05-23-2011 at 09:06 AM..
# 2  
Old 05-23-2011
sed way Smilie
Code:
# cat file1
<Main>
<NODE>
<NODENAME>FRUITS</NODENAME>
<NAME>APPLE</NAME>
<COLOUR>RED</COLOUR>
<NODE>
<IsWrapper/>
<NODENAME>SEASONAL</NODENAME>
<NODE>
<NAME>MANGO</NAME>
<COLOUR>GREEN</COLOUR>
</NODE>
</NODE>
</NODE>
</MAIN>


Code:
# sed -ne '/SEASONAL\|FRUITS/{s/<[^>]*//g;s/^./</p;x;H;n;N;N;p;g; s|<|</|p }' file1|sed -e 's/<\/FRUITS>//' -e '1!s|<FR|</FR|;' -e '/^$\|NODE/d'
<FRUITS>
<NAME>APPLE</NAME>
<COLOUR>RED</COLOUR>
<SEASONAL>
<NAME>MANGO</NAME>
<COLOUR>GREEN</COLOUR>
</SEASONAL>
</FRUITS>

regards
ygemici
# 3  
Old 05-23-2011
Code:
#!/usr/bin/perl

use warnings;
use strict;

use XML::DOM;

my $parser = new XML::DOM::Parser;
my $xmlfile = shift @ARGV;
my $doc = $parser->parsefile ($xmlfile);

# print $doc->toString;

my $iswrappers = $doc->getElementsByTagName('IsWrapper');
if ($iswrappers) {
    for ( my $i = 0; $i < $iswrappers->getLength; $i++) {
        my $wrapper = $iswrappers->item($i);
        my $parent = $wrapper->getParentNode;
        foreach my $child ($parent->getChildNodes) {
           if ($child->getNodeType == ELEMENT_NODE &&
               $child->getNodeName eq 'NODENAME') {
               print $child->getNodeName, ":  ", $child->getFirstChild()->getData(), "\n";
           }
        }
    }
}

$doc->dispose;

For the example XML document you provided, this outputs
Code:
$ ./example.pl example.xml
NODENAME:  SEASONAL
$

# 4  
Old 05-23-2011
Code:
awk '/IsWrapper/{n++}n&&match($0,/<NODENAME>[^<]*/){print "NODENAME: " substr($0,(RSTART+10),(RLENGTH-10));n=z}' example.xml


Code:
# cat tst
<Main>
        <NODE>
                        <NODENAME>FRUITS</NODENAME>
                        <NAME>APPLE</NAME>
                        <COLOUR>RED</COLOUR>
                        <NODE>
                         <IsWrapper/>
                         <NODENAME>SEASONAL</NODENAME>
                         <NODE>
                         <NAME>MANGO</NAME>
                         <COLOUR>GREEN</COLOUR>
                         </NODE>
                         <IsWrapper/>
                         <NODENAME>ANOTHER_RANDOM_NODENAME</NODENAME>
                         </NODE>
         </NODE>
</MAIN>

Code:
# nawk '/IsWrapper/{n++}n&&match($0,/<NODENAME>[^<]*/){print "NODENAME: " substr($0,(RSTART+10),(RLENGTH-10))}' tst
NODENAME: SEASONAL
NODENAME: ANOTHER_RANDOM_NODENAME

# 5  
Old 05-25-2011
Quote:
Originally Posted by fpmurphy
Code:
#!/usr/bin/perl

use warnings;
use strict;

use XML::DOM;

my $parser = new XML::DOM::Parser;
my $xmlfile = shift @ARGV;
my $doc = $parser->parsefile ($xmlfile);

# print $doc->toString;

my $iswrappers = $doc->getElementsByTagName('IsWrapper');
if ($iswrappers) {
    for ( my $i = 0; $i < $iswrappers->getLength; $i++) {
        my $wrapper = $iswrappers->item($i);
        my $parent = $wrapper->getParentNode;
        foreach my $child ($parent->getChildNodes) {
           if ($child->getNodeType == ELEMENT_NODE &&
               $child->getNodeName eq 'NODENAME') {
               print $child->getNodeName, ":  ", $child->getFirstChild()->getData(), "\n";
           }
        }
    }
}

$doc->dispose;

For the example XML document you provided, this outputs
Code:
$ ./example.pl example.xml
NODENAME:  SEASONAL
$

Hi,

Thank you very much for your reply.

I want to use the same code using xml::libxml ?

How can i do it using XML::LIBXML in perl?

Regards
Vanitha

---------- Post updated at 02:46 AM ---------- Previous update was at 01:44 AM ----------

Quote:
Originally Posted by fpmurphy
Code:
#!/usr/bin/perl

use warnings;
use strict;

use XML::DOM;

my $parser = new XML::DOM::Parser;
my $xmlfile = shift @ARGV;
my $doc = $parser->parsefile ($xmlfile);

# print $doc->toString;

my $iswrappers = $doc->getElementsByTagName('IsWrapper');
if ($iswrappers) {
    for ( my $i = 0; $i < $iswrappers->getLength; $i++) {
        my $wrapper = $iswrappers->item($i);
        my $parent = $wrapper->getParentNode;
        foreach my $child ($parent->getChildNodes) {
           if ($child->getNodeType == ELEMENT_NODE &&
               $child->getNodeName eq 'NODENAME') {
               print $child->getNodeName, ":  ", $child->getFirstChild()->getData(), "\n";
           }
        }
    }
}

$doc->dispose;

For the example XML document you provided, this outputs
Code:
$ ./example.pl example.xml
NODENAME:  SEASONAL
$

Hi,

The order of processing nodes is very important. For example:
Code:
# cat file1
<Main>
	<NODE>
		<NODENAME>FRUITS</NODENAME>
		<NAME>APPLE</NAME>
		<COLOUR>RED</COLOUR>
	<NODE>
	<IsWrapper/>
	<NODENAME>SEASONAL</NODENAME>
<NODE>
	<NAME>MANGO</NAME>
	<COLOUR>GREEN</COLOUR>
	</NODE>
</NODE>
	<NODE>
	<IsWrapper/>
	<NODENAME>JUICYFRUIT</NODENAME>
<NODE>
	<NAME>JACKFRUIT</NAME>
	<COLOUR>GREEN</COLOUR>
	</NODE>
</NODE>
</NODE>
</MAIN>

In this case the output should be:
Code:
SEASONAL
JUICYFRUIT

But i am not getting the order. How can retain the same order as in the xml.

REgards
Vanitha

---------- Post updated 05-25-11 at 12:16 AM ---------- Previous update was 05-24-11 at 02:46 AM ----------

Quote:
Originally Posted by fpmurphy
Code:
#!/usr/bin/perl

use warnings;
use strict;

use XML::DOM;

my $parser = new XML::DOM::Parser;
my $xmlfile = shift @ARGV;
my $doc = $parser->parsefile ($xmlfile);

# print $doc->toString;

my $iswrappers = $doc->getElementsByTagName('IsWrapper');
if ($iswrappers) {
    for ( my $i = 0; $i < $iswrappers->getLength; $i++) {
        my $wrapper = $iswrappers->item($i);
        my $parent = $wrapper->getParentNode;
        foreach my $child ($parent->getChildNodes) {
           if ($child->getNodeType == ELEMENT_NODE &&
               $child->getNodeName eq 'NODENAME') {
               print $child->getNodeName, ":  ", $child->getFirstChild()->getData(), "\n";
           }
        }
    }
}

$doc->dispose;

For the example XML document you provided, this outputs
Code:
$ ./example.pl example.xml
NODENAME:  SEASONAL
$

Hi,

Is it possible to retain the order of nodes?
If another <isWrapper/> is there for the nodename than the order becomes important.

How to retain the order?

Regards
Vanitha

Last edited by vanitham; 05-25-2011 at 02:08 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Looping through XML file on basis of a node

<?xml version="1.0" encoding="UTF-8"?> <Document> <FIToFICstmrCdtTrf> <GrpHdr> <MsgId>10001</MsgId> <NbOfTxs>1</NbOfTxs> <IntrBkSttlmDt>2015-05-06</IntrBkSttlmDt> <SttlmInf> <SttlmMtd>CLRG</SttlmMtd> </SttlmInf> <PmtTpInf> ... (2 Replies)
Discussion started by: harish2015
2 Replies

3. HP-UX

Mount FIle systems from node-1 onto node-2

Hi, We have HP UX service guard cluster on OS 11.23. Recently 40+ LUNs presented to both nodes by SAN team but I was asked to mount them on only one node. I created required VGs/LVs, created VxFS and mounted all of them and they are working fine. Now client requested those FS on 2nd node as... (4 Replies)
Discussion started by: prvnrk
4 Replies

4. Homework & Coursework Questions

Accessing one UNIX node from another node of the same server

Hi Experts, I am in need of running a script from one node say node 1 via node 2. My scheduling tool dont have access to node2 , so i need to invoke the list file from node1 but the script needs to run from node2. because the server to which i am hitting, is having access only for the node... (5 Replies)
Discussion started by: arun1377
5 Replies

5. UNIX for Dummies Questions & Answers

Iterate/Loop Through XML Node List

I need to load an XML file and loop through a list of nodes in it to execute a shell script for each one using the attributes for each node as parameters for the script. Any ideas? Any help will be much appreciated. (1 Reply)
Discussion started by: bradlecat
1 Replies

6. Shell Programming and Scripting

Find Node and replace line(s) preceding in xml file

Hello, I have an xml file whose contacts are like below: <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Mango <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Bannana (3 Replies)
Discussion started by: umarsatti
3 Replies

7. Shell Programming and Scripting

How to get value from xml node using sed/perl/script?

hello, new to this forum. but i have a requirement to extract the value from multiple xml node and print out the values to new file with comma seperated. would like to know how this would be done using either sed/perl or some unix script. an example would be tremendous... sample input file:... (2 Replies)
Discussion started by: davidsouk
2 Replies

8. Shell Programming and Scripting

shell call xalan to transform xml with xslt, but can not find root node

hi experts: i am using xslt to transform xml in shell. But can not find root node of source xml, if i remove the naming space definition in source xml, it works fine. So our the question is how to let xslt know the naming space of srouce xml file? Thanks for your kindly help in... (0 Replies)
Discussion started by: summer_cherry
0 Replies

9. Shell Programming and Scripting

Perl Array Variables to be returned to main

Hi All, I can;t seem to print out the array in sequence using the below subroutine. My first element in the array @lotsuffix is suppose to be $lotsuffix as defined in the subroutine, but when the array variable is being pass on the main program, my first element actually becomes $lotsuffix ! ... (4 Replies)
Discussion started by: Raynon
4 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