Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Reading XML file and print the values in the text file using Linux shell script Post 302950227 by sravanreddy on Wednesday 22nd of July 2015 09:14:40 AM
Old 07-22-2015
Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file

<sequence>
<Filename>aldorzum.doc</Filename>
<DivisionCode>US</DivisionCode>
<ContentType>Template</ContentType>
<ProductCode>VIMZIM</ProductCode>
</sequence>
<sequence>
<Filename>alozyme.doc</Filename>
<DivisionCode>CA</DivisionCode>
<ContentType>Standard</ContentType>
<ProductCode />
</sequence>
<sequence>
<Filename>ASER.doc</Filename>
<DivisionCode>SA</DivisionCode>
<ContentType>Template</ContentType>
<ProductCode>ENCLOSER</ProductCode>
</sequence>

I want Out put in sample.text file below manner

Filename ,DivisionCode , Product Code ,ContentType
aldorzum.doc ,US ,VIMZIM , Template
alozyme.doc , CA , , Standard
ASER.doc ,SA , ENCLOSER ,Template

I WANT ABOVE OUT PUT IN TEXT FILE WHILE READING XML FILE USING LINUX SHELL SCRIPT

PLEASE ANY ONE HELP ME...I NEED URGENT THAT LINUX SHELL SCRIPT
FOR READING XML FILE AND PRINT THE VALUES INTO THE TEXT FILE COLUMN WISE...
IF PRODUCT CODE VALUE IS EMPTY OR NOT EXIST JUST PRINT EMPTY SPACE.


i used below script not working according to my requirement.

$ nawk -F"[\"<>]" '/Filename/{id=$3}/DivisionCode/{m=$3}/ProductCode/{mc=$3}/ContentType/{ct=$3} {if(id && m && mc && ct){print id,m,mc,ct; id=m=mc=ct=0;}}' DocumentExportMetaData_20150705T000008.xml >sample.txt

but i get belowoutput
aldorzum.doc ,US ,VIMZIM ,Template
alozyme.doc , CA , ,
, Standard
ASER.doc , SA , ENCLOSER , Template
pleasure.doc CT
, TEMPLATE

I GOT ABOVE RESULTS WHILE USING MY SHELL SCRIPT..HERE IF PRODUCTCODE VALUE IS NOT EXIST THEN CONTENTTYPE VALUE IS PRINTED IN NEXT FILE ...
BUT I NEED IF PRODUCT CODE VALUE IS NOTEXIST OR NULL VALUE THEN LEAVE SPACE INSTEAD OF PRODUCTCODE PLACE THEN PRINT CONTENTTYPE IN NEXT COLUMN


Please any one help me...for my required output
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing the values in text file using while loop in shell script

Hi Frdz while read line do name=`echo $line | cut -d' ' -f 1 ` password=`echo $line | cut -d`-` -f 2` name > logfile.txt password > logfile.txt done < list.txt When it is run, am getting last values in list.txt file only,it is not storing lall the list entry values. How can i... (5 Replies)
Discussion started by: KiranKumarKarre
5 Replies

2. Shell Programming and Scripting

shell script to print particular column into text file

hi everyone, i need a script which would just print column into a text file nmap 10.226.112.222 PORT STATE SERVICE 7/tcp open echo 13/tcp open daytime 22/tcp open ssh 23/tcp open telnet 37/tcp open time 1100/tcp open unknown above nmap command gives us open ports on that IP. can... (6 Replies)
Discussion started by: anand121
6 Replies

3. Shell Programming and Scripting

Print some results in a text file using script in linux

hello everyone, i really need your help to write a script which would just print following kind of result into a text file (result.txt) XYZ test Results ID: <unique-id> Date: <date> ------------------------------------------------- | Task | Result | Time |... (3 Replies)
Discussion started by: viriimind
3 Replies

4. UNIX for Advanced & Expert Users

Shell Script to compare xml files and print output to a file

All, PLease can you help me with a shell script which can compare two xml files and print the difference to a output file. I have attached one such file for you reference. <Group> <Member ID=":Year_Quad:41501" childCount="4" fullPath="PEPSICO Year-Quad-Wk : FOLDER.52 Weeks Ending Dec... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

5. Shell Programming and Scripting

Reading a word from a text file into shell script

Hi, I am new to shell programming.I need to write a script that would accept a word from each line fo an input text file.Can anyone help me with this?Exact requirement: word1 word2 word3 (separated by space) .Now I need word3 from each such line in the text file. Thanks in Advance, Manish (3 Replies)
Discussion started by: manish007
3 Replies

6. Shell Programming and Scripting

AWK/Shell script for reading text file

Hello, I have a text file which has list of SQL Commands, from where I have grepped the lines where there is a specific string: grep <string> <file1> >> <file2> A sample of the file2 is: INSERT INTO PS_PWC_SP_ERN_DATA SELECT A.EMPLID ,B.COMPANY ,B.PAYGROUP , B.OTH_PAY FROM... (7 Replies)
Discussion started by: suddhasatwa_bha
7 Replies

7. Shell Programming and Scripting

Passing values to an XML file from shell script

:wall: Hi, I have an XML file with 5 tags. I need to pass values to the XML file from a shell script that will replace values in 2 of the tags. I cannot hardcode the tag values in XML and use replace command in script as the values are likely to change. Please help !!!!!!!!!!! (2 Replies)
Discussion started by: Monalisaa
2 Replies

8. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

9. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies
Template::Plugin::XML::DOM(3pm) 			User Contributed Perl Documentation			   Template::Plugin::XML::DOM(3pm)

NAME
Template::Plugin::XML::DOM - Plugin interface to XML::DOM SYNOPSIS
# load plugin [% USE dom = XML.DOM %] # also provide XML::Parser options [% USE dom = XML.DOM(ProtocolEncoding = 'ISO-8859-1') %] # parse an XML file [% doc = dom.parse(filename) %] [% doc = dom.parse(file = filename) %] # parse XML text [% doc = dom.parse(xmltext) %] [% doc = dom.parse(text = xmltext) %] # call any XML::DOM methods on document/element nodes [% FOREACH node = doc.getElementsByTagName('report') %] * [% node.getAttribute('title') %] # or [% node.title %] [% END %] # define VIEW to present node(s) [% VIEW report notfound='xmlstring' %] # handler block for a <report>...</report> element [% BLOCK report %] [% item.content(view) %] [% END %] # handler block for a <section title="...">...</section> element [% BLOCK section %] <h1>[% item.title %]</h1> [% item.content(view) %] [% END %] # default template block converts item to string [% BLOCK xmlstring; item.toString; END %] # block to generate simple text [% BLOCK text; item; END %] [% END %] # now present node (and children) via view [% report.print(node) %] # or print node content via view [% node.content(report) %] # following methods are soon to be deprecated in favour of views [% node.toTemplate %] [% node.childrenToTemplate %] [% node.allChildrenToTemplate %] DESCRIPTION
This is a Template Toolkit plugin interfacing to the XML::DOM module. The plugin loads the XML::DOM module and creates an XML::DOM::Parser object which is stored internally. The parse() method can then be called on the plugin to parse an XML stream into a DOM document. [% USE dom = XML.DOM %] [% doc = dom.parse('/tmp/myxmlfile') %] The XML::DOM plugin object (i.e. 'dom' in these examples) acts as a sentinel for the documents it creates ('doc' and any others). When the plugin object goes out of scope at the end of the current template, it will automatically call dispose() on any documents that it has cre- ated. Note that if you dispose of the the plugin object before the end of the block (i.e. by assigning a new value to the 'dom' variable) then the documents will also be disposed at that point and should not be used thereafter. [% USE dom = XML.DOM %] [% doc = dom.parse('/tmp/myfile') %] [% dom = 'new value' %] # releases XML.DOM plugin and calls # dispose() on 'doc', so don't use it! The plugin constructor will also accept configuration options destined for the XML::Parser object: [% USE dom = XML.DOM(ProtocolEncoding = 'ISO-8859-1') %] METHODS
parse() The parse() method accepts a positional parameter which contains a filename or XML string. It is assumed to be a filename unless it con- tains a < character. [% xmlfile = '/tmp/foo.xml' %] [% doc = dom.parse(xmlfile) %] [% xmltext = BLOCK %] <xml> <blah><etc/></blah> ... </xml> [% END %] [% doc = dom.parse(xmltext) %] The named parameters 'file' (or 'filename') and 'text' (or 'xml') can also be used: [% doc = dom.parse(file = xmlfile) %] [% doc = dom.parse(text = xmltext) %] The parse() method returns an instance of the XML::DOM::Document object representing the parsed document in DOM form. You can then call any XML::DOM methods on the document node and other nodes that its methods may return. See XML::DOM for full details. [% FOREACH node = doc.getElementsByTagName('CODEBASE') %] * [% node.getAttribute('href') %] [% END %] This plugin also provides an AUTOLOAD method for XML::DOM::Node which calls getAttribute() for any undefined methods. Thus, you can use the short form of [% node.attrib %] in place of [% node.getAttribute('attrib') %] PRESENTING DOM NODES USING VIEWS
You can define a VIEW to present all or part of a DOM tree by automatically mapping elements onto templates. Consider a source document like the following: <report> <section title="Introduction"> <p> Blah blah. <ul> <li>Item 1</li> <li>item 2</li> </ul> </p> </section> <section title="The Gory Details"> ... </section> </report> We can load it up via the XML::DOM plugin and fetch the node for the <report> element. [% USE dom = XML.DOM; doc = dom.parse(file = filename); report = doc.getElementsByTagName('report') %] We can then define a VIEW as follows to present this document fragment in a particular way. The Template::Manual::Views documentation con- tains further details on the VIEW directive and various configuration options it supports. [% VIEW report_view notfound='xmlstring' %] # handler block for a <report>...</report> element [% BLOCK report %] [% item.content(view) %] [% END %] # handler block for a <section title="...">...</section> element [% BLOCK section %] <h1>[% item.title %]</h1> [% item.content(view) %] [% END %] # default template block converts item to string representation [% BLOCK xmlstring; item.toString; END %] # block to generate simple text [% BLOCK text; item; END %] [% END %] Each BLOCK defined within the VIEW represents a presentation style for a particular element or elements. The current node is available via the 'item' variable. Elements that contain other content can generate it according to the current view by calling [% item.content(view) %]. Elements that don't have a specific template defined are mapped to the 'xmlstring' template via the 'notfound' parameter specified in the VIEW header. This replicates the node as an XML string, effectively allowing general XML/XHTML markup to be passed through unmodified. To present the report node via the view, we simply call: [% report_view.print(report) %] The output from the above example would look something like this: <h1>Introduction</h1> <p> Blah blah. <ul> <li>Item 1</li> <li>item 2</li> </ul> </p> <h1>The Gory Details</h1> ... To print just the content of the report node (i.e. don't process the 'report' template for the report node), you can call: [% report.content(report_view) %] AUTHORS
This plugin module was written by Andy Wardley and Simon Matthews. The XML::DOM module is by Enno Derksen and Clark Cooper. It extends the the XML::Parser module, also by Clark Cooper which itself is built on James Clark's expat library. COPYRIGHT
Copyright (C) 2000-2006 Andy Wardley, Simon Matthews. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
Template::Plugin, XML::DOM, XML::Parser perl v5.8.8 2008-03-01 Template::Plugin::XML::DOM(3pm)
All times are GMT -4. The time now is 05:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy