How to get a field from an XML?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get a field from an XML?
# 1  
Old 01-28-2011
How to get a field from an XML?

We have an input XML of the following form
Code:
<item>
<account>1234</account>
<id>345</id>
</item>

How do we get the value of <account> tag (1234) using UNIX ?

Last edited by Franklin52; 01-28-2011 at 04:16 AM.. Reason: Please use code tags
# 2  
Old 01-28-2011
Try this,
Code:
awk '/<account>/' inputfile

# 3  
Old 01-28-2011
search-word-print-specific-string

Thanks Pravin. But we still get the entire xml.
Our input xml looks like:
Code:
 <doc>
     <str name="account_id">1111</str> 

    <str name="prd_id">DHEP155EK</str>  

   </doc>


  - <doc>
     <str name="account_id">6666</str> 

    <str name="prd_id">394531662</str>  

   </doc>


  - <doc>
     <str name="account_id">6666</str> 

    <str name="prd_id">394805955</str>  

   </doc>

here we want to get the account_id|prd_id to another text file, output.txt like:
Code:
1111|DHEP155EK
6666|394531662
6666|394805955

Can you help?

Last edited by Franklin52; 01-28-2011 at 04:18 AM.. Reason: Please use code tags
# 4  
Old 01-28-2011
Try...
Code:
awk -F '[<>]' '$2~/account_id|prd_id/ {print $3}' file1 | paste -d '|' - -

# 5  
Old 01-28-2011
awk '{print $0}' input.xml gives the whole text in a single line. Hence i guess none of the above codes are working!Smilie
# 6  
Old 01-30-2011
Code:
awk -F "[><]" '/"account_id"/{printf $3} /"prd_id"/ {print "|" $3}' infile

# 7  
Old 01-30-2011
First of all, what you are showing us in not an XML document. It is a text document using XML-like tags. A valid XML document (example.xml) has a single root, i.e.
Code:
<docs>
  <doc>
     <str name="account_id">1111</str>
     <str name="prd_id">DHEP155EK</str>
  </doc>
  <doc>
     <str name="account_id">6666</str>
     <str name="prd_id">394531662</str>
  </doc>
</docs>

If you have a valid document, you can use an XSL stylesheet to quickly contain the output you want.

For example, here is a stylesheet (example.xsl):
Code:
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="text"/>

   <xsl:template match="/">
      <xsl:apply-templates select="docs"/>
   </xsl:template>

   <xsl:template match="docs">
      <xsl:apply-templates select="doc"/>
   </xsl:template>

   <xsl:template match="doc">
      <xsl:value-of select="./str[@name='account_id']" />|<xsl:value-of select="./str[@name='prd_id']" />
<xsl:text>
</xsl:text>
   </xsl:template>

</xsl:stylesheet>

which when used with a XSLT engine such as xsltproc produces the output you want:
Code:
$ xsltproc example.xsl example.xml
1111|DHEP155EK
6666|394531662

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Field delimited data to XML

Hi, We need to produce a XML file based on a record/field delimited data file. At this point we could just script something out but I would like to ask the community what would be the best choice of programming language to do this, in terms of performance of execution, and in terms of complexity... (9 Replies)
Discussion started by: Indalecio
9 Replies

2. Shell Programming and Scripting

Cgi to dump xml data from form input field

Hi All, I am trying to write a shell script which takes parse the web form find the input field and dump the data of that field into one xml file. The form looks like, <input type="button" id="btnSave" value="Save" onclick="saveXmlData()"/> <form name="submitForm"... (1 Reply)
Discussion started by: jdp
1 Replies

3. Red Hat

How to get a particular field from xml file?

i have a xml file and in that input file path is given. how to fetch that input file path using shell script. (4 Replies)
Discussion started by: ramsavi
4 Replies

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

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

6. Shell Programming and Scripting

How do I extract the third field in an xml entry?

I have the following XML entry - I need to extract the contents of the third defined variable, Asset_Name. <AMS Asset_Class="package" Asset_ID="XXXXXXXXXXXXXXXXXXXX" Asset_Name="MOVIE_XXXXXXXXXXX_XXXXXXXX" Creation_Date="2011-09-13" Description="test asset package" Product="MOD"... (4 Replies)
Discussion started by: hrr_mute
4 Replies

7. UNIX for Dummies Questions & Answers

Extract Field Value from XML file

Hi, Within a UNIX shell script I need to extract a value from an XML field. The field will contain different values but will always be 6 digits in length. E.g.: <provider-id>999999</provider-id> I've tried various ways but no luck. Any ideas how I might get the provider id (in this case... (2 Replies)
Discussion started by: pnclayt11
2 Replies

8. Shell Programming and Scripting

Help needed XML Field Extraction

I had an immediate work to sort out the error code and error message which are associated within the log. But here im facing an problem to extract 3 different fields from the XML log can some one please help. I tried using different script including awk & nawk, but not getting the desired output. ... (18 Replies)
Discussion started by: raghunsi
18 Replies

9. Programming

Extracting Field values for XML file

i have an input file of XML type with data like <nx-charging:additional-parameter name="NX_INTERNATIONALIZED_CLID" value="919427960829"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-charging:process> i want to extract data such that i get the output... (3 Replies)
Discussion started by: junaid.nehvi
3 Replies

10. Shell Programming and Scripting

How To Find Length of a Field in XML File

Hi I have a xml file with below data...have to find the length of the filedvalues... <?xml version="1.0" encoding="ISO-8859-15" standalone="no"?><abc xmlns:xsi="http://www.w3.org/2000/XMLSchem... (3 Replies)
Discussion started by: naughty21
3 Replies
Login or Register to Ask a Question