Extracting the Root Element from the XML File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting the Root Element from the XML File
# 1  
Old 02-15-2011
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
Code:
<?xml version="1.0" encoding="utf-8" ?> 
<TestXMLMessage>
<TestRec>
<ID>1000</ID> 
</TestRec>
</TestXMLMessage>

Wanted to extract the TestXMLMessage.

Regards,

Chari

Last edited by Franklin52; 02-15-2011 at 04:23 AM.. Reason: Please use code tags
# 2  
Old 02-15-2011
Something like this?
Code:
awk -F"[<>]" 'NR==2{print $2}' file

or maybe this what you mean:
Code:
awk '/<TestXMLMessage>/{f=1};f;/<\/TestXMLMessage>/{f=0}' file


Last edited by Franklin52; 02-15-2011 at 04:43 AM..
This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 02-15-2011
Could this help you?
Code:
awk '{if(/^<TestXMLMessage>/){print;flg=1;next} if(flg==1) {print;next} if (/^<\/TestXMLMessage>/){print;flg=0}}' test.xml

# 4  
Old 02-15-2011
Quote:
Originally Posted by Franklin52
Code:
awk '/<TestXMLMessage>/{f=1};f;/<\/TestXMLMessage>/{f=0}' file

This can be done a bit shorter:
Code:
awk '/<TestXMLMessage>/,/<\/TestXMLMessage>/' file

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 02-15-2011
Here is an XSL stylesheet which prints the root element of the document
Code:
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="text"/>

   <xsl:template match="/">
      <xsl:value-of select="name(/*)"/>
   </xsl:template>

</xsl:stylesheet>

Using xsltproc to transform the input docuemnt:
Code:
$ xsltproc example.xsl example.xml
TestXMLMessage
$

# 6  
Old 02-15-2011
Hi All,

Thanks for your quick response.

This helped what I wanted to achieve.
Code:
awk -F"[<>]" 'NR==2{print $2}' file

Best Regards,

Chari

Last edited by Franklin52; 02-16-2011 at 05:03 AM.. Reason: Please use code tags, thank you
# 7  
Old 02-23-2011
Hi All,

I am trying to change the XML filename in a folder to the rootelement in the XML file.

Now I have something like this.
Code:
for i in *.xml; do echo $i;awk -F"[<>]" 'NR==2{print $2}' $i; done

Output of above command:
Code:
TestMsg2010-10-19_20_20_54.xml
R1Msg TimeStamp="2010-10-19T08:49:08.000000Z"
TestMsg2010-10-19_20_20_58.xml
R2Msg TimeStamp="2010-10-19T08:49:08.000000Z"

Renamed File OutputSmilieThis is what I want to achieve)
Code:
R1Msg2010-10-19_20_20_54.xml
R2Msg2010-10-19_20_20_58.xml
R3Msg2010-10-19_20_21_00.xml

Is there a way that I can tweak the above command to achieve the same? Please help me as I am new to this side of programming.

Thanks in advance.

Last edited by Franklin52; 02-24-2011 at 03:30 AM.. Reason: Please use code tags
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

Reading XML file and extracting value

Dear All, I am reading one XML file to extract value from the particular tag:- Sample xml is below:- <KeyValuePairs> <Key>TestString</Key> <Value>Test12_Pollings</Value> </KeyValuePairs> I want to read the value for the KEY tag and there will be multiple key tags :- awk... (4 Replies)
Discussion started by: sharsour
4 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. Shell Programming and Scripting

Need to find root element name of XML file

Given this XML: <?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> ... (2 Replies)
Discussion started by: ricksj
2 Replies

5. Shell Programming and Scripting

Need help in extracting data from xml file

Hello, This is my first post in here, so excuse me if I sound too noob here! I need to extract the path "/apps/mp/installedApps/V61/HRO/hrms_01698_A_qa.ear" from the below xml extract. The path will always appear with the key "binariesURL" <deployedObject... (6 Replies)
Discussion started by: abhishek2386
6 Replies

6. UNIX for Dummies Questions & Answers

Extracting data from an xml file

Hello, Please can someone assist. I have the following xml file: <?xml version="1.0" encoding="utf-8" ?> - <PUTTRIGGER xmlns:xsd="http://www.test.org/2001/XMLSchema" xmlns:xsi="http://www.test.org/2001/XMLSchema-instance" APPLICATIONNUMBER="0501160" ACCOUNTNAME="Mrs S Test"... (15 Replies)
Discussion started by: Dolph
15 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

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

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

10. Shell Programming and Scripting

extracting XML file using sed

Hello folks I want to extract data between certain tag in XML file using 'sed' <xml> ......... .......... <one>XXXXXXXXXXXXXXXXXXXX</one> ...... Anyone ?Thank you (7 Replies)
Discussion started by: pujansrt
7 Replies
Login or Register to Ask a Question