Sponsored Content
Full Discussion: parsing xml file
Top Forums Shell Programming and Scripting parsing xml file Post 302573931 by radoulov on Wednesday 16th of November 2011 05:22:49 AM
Old 11-16-2011
Yes, the code need to be corrected.
Using the latest xml attached to your post the following code produces the following result:

Code:
xgawk -lxml 'XMLCHARDATA {
  d[XMLPATH] = $0
  }
XMLENDELEM == "name" {
  XMLPATH == "/domain/server/name" && name = d["/domain/server/name"]
  XMLPATH == "/domain/name"        && domain_name = d["/domain/name"]
  }
XMLENDELEM == "listen-port" && XMLPATH == "/domain/server/listen-port" {
  listen_port = d["/domain/server/listen-port"]
  }
XMLENDELEM == "machine" && XMLPATH == "/domain/server/machine" {
  machine = d["/domain/server/machine"]
  }
XMLENDELEM == "server" {  
  print machine, listen_port, name, domain_name
  }' OFS=: sample.xml

Output:

Code:
:9999:WebLogicAdmin:peoplesoft
Crm-Test-Web:8001:PIA:peoplesoft
Crm-Test-Web:8011:PIA1:peoplesoft
Crm-Test-Web:8021:PIA2:peoplesoft
Crm-Test-Web:8080:RPS:peoplesoft
Crm-Test-Web:8081:PSEMHUB:peoplesoft
Crm-Test-Web:6001:PSOL:peoplesoft

Is it wrong?

Edit: rewritten using the switch statement:

Code:
xgawk -lxml 'XMLCHARDATA { d[XMLPATH] = $0 }
{ 
  switch (XMLENDELEM) {
    case "name": 
      switch (XMLPATH) { 
        case "/domain/server/name":  
          name = d["/domain/server/name"]
          break
        case "/domain/name":
          domain_name = d["/domain/name"]
          break
        }
    case "listen-port":
      XMLPATH == "/domain/server/listen-port" && 
        listen_port = d["/domain/server/listen-port"]
      break
    case "machine": 
      XMLPATH == "/domain/server/machine" && 
        machine = d["/domain/server/machine"]
      break
    case "server":  
      print machine, listen_port, name, domain_name
      break  
    }
  }' OFS=: sample.xml


Last edited by radoulov; 11-16-2011 at 07:08 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Parsing xml file using Sed

Hi All, I have this(.xml) file as: <!-- define your instance here --> <instance name='ins_C2Londondev' user='' group='' fullname='B2%20-%20London%20(dev)' > <property> </property> </instance> I want output as: <!-- define your instance here --> <instance... (3 Replies)
Discussion started by: kapilkinha
3 Replies

2. Shell Programming and Scripting

XML file parsing using script

Hi I need some help with XML file parsing. I have an XML file with the below tag, I need a script to identify the value of srvcName which is this case is "AAA srvc name". I need to put contents of this value which is AAA srvc and name into different variables using an array and then reformat it... (6 Replies)
Discussion started by: zmfcat1
6 Replies

3. UNIX for Dummies Questions & Answers

Help parsing a XML file ....

Well I have read several threads on the subject ... but being a newbie like me makes it hard to understand ... What I need is the following: Input data: ------- snip --------- <FavouriteLocations> <FavouriteLocations class="FavouriteList"><Item... (6 Replies)
Discussion started by: misak
6 Replies

4. Shell Programming and Scripting

Parsing xml file

hi guys, great help to the original question, can i expand please? i have large files filled with blocks like this <Placemark> network type: hot line1 line2 line3 <styleUrl>red.png</styleUrl> </Placemark> <Placemark> network type: cold line1 line2 line3... (3 Replies)
Discussion started by: garvald
3 Replies

5. Shell Programming and Scripting

Help in parsing xml file (sed/nawk)

I have a large xml file as shown below: <input> <blah> <blah> <atr="blah blah value = ""> <blah> <blah> </input> ..2nd chunk... ..3rd chunk... ...4th chunk... All lines between <input> and </input> is one 'order' and this 'order' is repeated... (14 Replies)
Discussion started by: shekhar2010us
14 Replies

6. Shell Programming and Scripting

Parsing an XML file

Hello, I have the following xml file as an input. <?xml version="1.0" encoding="UTF-8"?> <RECORDS PS3_VERSION="1104_01"><RECORD> <POI_ID>931</POI_ID> <SUPPLIER_ID>2</SUPPLIER_ID> <POI_PVID>997920846</POI_PVID> <DB_ID>1366650925</DB_ID> <REGION>H1</REGION> <POI_NAME NAME_TYPE="Official"... (4 Replies)
Discussion started by: ramky79
4 Replies

7. Shell Programming and Scripting

Help in parsing XML output file in perl.

Hi I have an XML output like : <?xml version="1.0" encoding="ISO-8859-1" ?> - <envelope> - <body> - <outputGetUsageSummary> - <usgSumm rerateDone="5"> - <usageAccum accumId="269" accumCaptn="VD_DP_AR" inclUnits="9999999.00" inclUnitsUsed="0.00" shared="false" pooled="false"... (7 Replies)
Discussion started by: rkrish
7 Replies

8. Shell Programming and Scripting

XML: parsing of the Google contacts XML file

I am trying to parse the XML Google contact file using tools like xmllint and I even dived into the XSL Style Sheets using xsltproc but I get nowhere. I can not supply any sample file as it contains private data but you can download your own contacts using this script: #!/bin/sh # imports... (9 Replies)
Discussion started by: ripat
9 Replies

9. UNIX for Dummies Questions & Answers

Parsing XML file

I want to parse xml file sample file....... <name locale="en">my_name<>/name><lastChanged>somedate</lastChanged><some more code here> <name locale="en">tablename1<>/name><lastChanged>somedate</lastChanged> <definition><dbquery><sources><sql type="cognos">select * from... (10 Replies)
Discussion started by: ms2001
10 Replies

10. Shell Programming and Scripting

Help with parsing xml file

Hi, Need help with parsing xml data in unix and place it in a csv file. My xml file looks like this: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <iwgroups> <nextid value="128"> </nextid> <iwgroup name="RXapproval" id="124" display-name="RXapproval"... (11 Replies)
Discussion started by: ajayakunuri
11 Replies
HWLOC-ASSEMBLER(1)						       hwloc							HWLOC-ASSEMBLER(1)

NAME
hwloc-assembler - Assemble multiple XML topologies SYNOPSIS
hwloc-assembler [options] output.xml [--name <name1>] input1.xml [--name <name2>] input2.xml ... OPTIONS
-n --name <name> Set <name> in the AssemblerName info attribute of the next input topology root to ease identification within the final global topol- ogy. -f --force Ignore errors while reading input files. -v --verbose Verbose messages. DESCRIPTION
hwloc-assembler combines the input XML topologies and exports the resulting global topologies to a new XML file. All inputs are inserting as children of the global root object. Each input topology root is annotated with info attributes before insertion. AssemblerIndex is set to the index within the list of inputs. AssemblerName is set to the name given with --name if any. hwloc-assembler-remote offers a fron- tend for assembling remote nodes topologies without having to manually gather and transfer each of them. EXAMPLES
To assemble two nodes topologies: $ hwloc-assembler output.xml --name host1 host1.xml --name host2 host2.xml RETURN VALUE
Upon successful execution, hwloc-assembler returns 0. hwloc-assembler will return nonzero if any kind of error occurs, such as (but not limited to) failure to parse the command line. SEE ALSO
hwloc(7), lstopo(1), hwloc-assembler-remote(1) 1.7 Apr 07, 2013 HWLOC-ASSEMBLER(1)
All times are GMT -4. The time now is 12:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy