Parsing xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing xml file
# 1  
Old 04-13-2010
Java Parsing xml file

hi guys, great help to the original question, can i expand please?

i have large files filled with blocks like this
Code:
<Placemark>
network type: hot
line1
line2
line3
<styleUrl>red.png</styleUrl>
</Placemark>
<Placemark>
network type: cold
line1
line2
line3
<styleUrl>red.png</styleUrl>
</Placemark>
<Placemark>
network type: medium
line1
line2
line3
<styleUrl>red.png</styleUrl>
</Placemark>

..currently all styleUrl values are red.png, however i want to change this according to whatever value "network type:" is, so if it's hot i want a red.png and if it's medium it should be green.png and if cold it'll be blue.png

the files already exist and contain 1000's of lines, i just dont have the awk knowledge to manage a quick change

can you please provide some insight?

i was thinking along the lines of this:

Code:
awk'/^network type:/{ choose some graphic, like foo=blue.png;}/\<styleUrl\>/{print "  <styleUrl>"foo"</styleUrl>"; next;}{print}'

..but i must include some "if" statements for the different colors, that's where i get lost..

many thanks, G

Last edited by zaxxon; 04-15-2010 at 10:19 AM.. Reason: use code tags please, ty
# 2  
Old 04-15-2010
No thread Hijacking in future and open up your own thread. This has been separated from the other thread. You'll also get a pm for a notice on using code tags.
# 3  
Old 04-16-2010
Quote:
Originally Posted by garvald
..currently all styleUrl values are red.png, however i want to change this according to whatever value "network type:" is, so if it's hot i want a red.png and if it's medium it should be green.png and if cold it'll be blue.png
...
Here's one way to do it with Perl:

Code:
$
$
$ cat -n f0
     1  <Placemark>
     2  network type: hot
     3  line1
     4  line2
     5  line3
     6  <styleUrl>red.png</styleUrl>
     7  </Placemark>
     8  <Placemark>
     9  network type: cold
    10  line1
    11  line2
    12  line3
    13  <styleUrl>red.png</styleUrl>
    14  </Placemark>
    15  <Placemark>
    16  network type: medium
    17  line1
    18  line2
    19  line3
    20  <styleUrl>red.png</styleUrl>
    21  </Placemark>
    22  <Placemark>
    23  network type: something_else
    24  line1
    25  line2
    26  line3
    27  <styleUrl>something_else.png</styleUrl>
    28  </Placemark>
$
$
$ perl -lne '/^network type: (hot|cold|medium)$/ and $x = $1 eq "hot" ? "red.png" : ($1 eq "medium" ? "green.png" : "blue.png");
             if (/(<styleUrl>).*?(<\/styleUrl>)/ and $x ne "") {print "$1$x$2"; $x=""} else {print $_}' f0
<Placemark>
network type: hot
line1
line2
line3
<styleUrl>red.png</styleUrl>
</Placemark>
<Placemark>
network type: cold
line1
line2
line3
<styleUrl>blue.png</styleUrl>
</Placemark>
<Placemark>
network type: medium
line1
line2
line3
<styleUrl>green.png</styleUrl>
</Placemark>
<Placemark>
network type: something_else
line1
line2
line3
<styleUrl>something_else.png</styleUrl>
</Placemark>
$
$

tyler_durden
# 4  
Old 04-16-2010
oops forget it

Last edited by bigearsbilly; 04-16-2010 at 01:02 PM.. Reason: mistake
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

6. Shell Programming and Scripting

parsing xml file

Hello! We need to parse weblogic config.xml file and display rows in format: machine:listen-port:name:application_name In our enviroment the output should be (one line for every instance): Crm-Test-Web:8001:PIA:peoplesoft Crm-Test-Web:8011:PIA:peoplesoft... (9 Replies)
Discussion started by: annar
9 Replies

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

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

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

10. 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
Login or Register to Ask a Question