Need to find root element name of XML file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to find root element name of XML file
# 1  
Old 07-21-2011
Need to find root element name of XML file

Given this XML:
Code:
<?xml version="1.0"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>

I'm using the following code to determine if the root element is 'catalog' or not.

Code:
#!/usr/bin/perl
use File::Basename;
use XML::LibXML;
use XML::LibXML::XPathContext;
my $inputFile = $ARGV[0];
my($XMLfile, $inputpath) = fileparse($inputFile);
chdir($inputpath) or die "Can\'t chdir to $inputpath";
 
my $doc = XML::LibXML->new()->parse_file($XMLfile);
my $xp = XML::LibXML::XPathContext->new($doc);
$object = $xp->exists('/catalog');
print $object ."\n";

This works fine for a <catalog> file, but I also need to determine other file types by reading the root element. Does anyone know of a method using XML::LibXML (or similar) module that would return the name of the root element?
# 2  
Old 07-21-2011
Hi,

There is a method called getDocumentElement() in XML:: DOM and remember reading something similar in LibXML. Please search for a similar method.
# 3  
Old 07-22-2011
thanks for the reply, but something a little more specific would be more helpful.

---------- Post updated 07-22-11 at 11:50 AM ---------- Previous update was 07-21-11 at 10:57 PM ----------

I found a solution for anyone searching this thread:

Code:
#!/usr/bin/perl
use File::Basename;
use XML::DOM;
my $inputFile = $ARGV[0];
my($XMLfile, $inputpath) = fileparse($inputFile);
chdir($inputpath) or die "Can\'t chdir to $inputpath";
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile($XMLfile) or die "Could not parse!";
my $docNode   = $doc->getDocumentElement->getTagName;
print "Tag Name: ".$docNode."\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extract Element from XML file

<?xml version = '1.0' encoding =... (8 Replies)
Discussion started by: Siva SQL
8 Replies

2. Shell Programming and Scripting

Merge XML file (to same root)

HI I am looking for a easy way to merge 2 xml files like the following file 1: will have a structure like this <Group GID="TOTO" NAME="TOTO" UNITS="1.0"> <Property NAME="Id" VALUE="TOTO"/> <Property NAME="Currency" VALUE="USD"/> <Group... (6 Replies)
Discussion started by: kykyboss
6 Replies

3. Shell Programming and Scripting

Find if XML element has a matching required element

I want to check if every <Part> element has corresponding <Description> in this sample XML. ....<Lot Of XML> <Inv lineNumber="2"> <Item> ... (4 Replies)
Discussion started by: kchinnam
4 Replies

4. UNIX for Advanced & Expert Users

Perl XML::DOM: How to test if element exists?

Hi, I'm trying to write a script for some xml file handling, but I'm not getting too far with it. I've got the following xml content <?xml version="1.0" encoding="UTF-8"?> <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <Operation name="OPER1"> <Action name="ACTION1">... (2 Replies)
Discussion started by: Juha
2 Replies

5. Shell Programming and Scripting

Extracting the Root Element from the XML File

Any help to extract the root element from an XML file will be appreciated. Example: test.xml <?xml version="1.0" encoding="utf-8" ?> <TestXMLMessage> <TestRec> <ID>1000</ID> </TestRec> </TestXMLMessage> Wanted to extract the TestXMLMessage. Regards, Chari (6 Replies)
Discussion started by: sree_chari
6 Replies

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

7. UNIX Desktop Questions & Answers

read XML xml element with REGEXP

Hi, I would need to read an xml element from an xml file to a local variable. Please could you help me with a shell script to get so? Considering that I have a file called file.xml like below: <header> <description>This is the description</description> <content>This is the... (2 Replies)
Discussion started by: oscarmon
2 Replies

8. Shell Programming and Scripting

Extract XML Element Values

I have a rather large file with XML-style content. Each line contains one full XML entry. For example: 1:<Message><DNIS>1234</DNIS><UCID>3456</UCID><TransferGroup>XYZXYZ</TransferGroup></Message> 2:<Message><DNIS>9999</DNIS><UCID>2584</UCID><TransferGroup>ABCABC</TransferGroup></Message>... (1 Reply)
Discussion started by: sharpi03
1 Replies

9. Shell Programming and Scripting

XML root element

Hi All Can someone please help me with this awk to search an element in a XML file with a particular value and then change the root element. Thanks & Regards Karan (9 Replies)
Discussion started by: karansachdeva
9 Replies

10. Shell Programming and Scripting

Finding a XML element and moving the file

Hi All, I am looking for a awk/shell which can find an element named REFERENCE in a XML file and check whether it is empty or not. If there is no value in the REFERENCE element then correspondingly move the file to some other folder. The Unix server is AIX version 4. Any inputs... (9 Replies)
Discussion started by: karansachdeva
9 Replies
Login or Register to Ask a Question