Mac AppleScript read XML file with a lot Same Name

macweather

macrumors newbie
Original poster
Mar 12, 2017
2
0
Taiwan
I have a file like this , its about weather observation data Like below


<?xml version="1.0" encoding="UTF-8"?><cwbopendata xmlns="urn:cwb:gov:tw:cwbcommon:0.1">
<identifier>2d726733-adff-4e93-b051-694f1d46689c</identifier>
<sender>weather@cwb.gov.tw</sender>
<sent>2017-03-10T11:04:09+08:00</sent>
<status>Actual</status>
<msgType>Issue</msgType>
<dataid>CWB_A0002</dataid>
<scope>Public</scope>
<dataset/>
<location>
<lat>23.9637</lat>
<lon>120.8368</lon>
<locationName>station_name</locationName>
<stationId>C1I230</stationId>
<time>
<obsTime>2017-03-10T11:00:00+08:00</obsTime>
</time>
<weatherElement>
<elementName>ELEV</elementName>
<elementValue>
<value>837.00</value>
</elementValue>
</weatherElement>
<weatherElement>
<elementName>RAIN</elementName>
<elementValue>
<value>0.00</value>
</elementValue>
</weatherElement>
<weatherElement>
<elementName>MIN_10</elementName>
<elementValue>
<value>0.00</value>
</elementValue>
</weatherElement>
<weatherElement>
<elementName>HOUR_3</elementName>
<elementValue>
<value>0.50</value>
</elementValue>
</weatherElement>
<weatherElement>
<elementName>HOUR_6</elementName>
<elementValue>
<value>0.50</value>
</elementValue>
</weatherElement>
<weatherElement>
<elementName>HOUR_12</elementName>
<elementValue>
<value>0.50</value>
</elementValue>
</weatherElement>
<weatherElement>
<elementName>HOUR_24</elementName>
<elementValue>
<value>1.00</value>
</elementValue>
</weatherElement>
<weatherElement>
<elementName>NOW</elementName>
<elementValue>
<value>0.50</value>
</elementValue>
</weatherElement>
<parameter>
<parameterName>CITY</parameterName>
<parameterValue>Taiwan</parameterValue>
</parameter>
<parameter>
<parameterName>CITY_SN</parameterName>
<parameterValue>13</parameterValue>
</parameter>
<parameter>
<parameterName>TOWN</parameterName>
<parameterValue>Tainan</parameterValue>
</parameter>
<parameter>
<parameterName>TOWN_SN</parameterName>
<parameterValue>139</parameterValue>
</parameter>
<parameter>
<parameterName>ATTRIBUTE</parameterName>
<parameterValue>CWB_noguy</parameterValue>
</parameter>
</location>


every station data is start at<location>... </location>
and about 1000 stations in this file

I want to read it and cover it to *.csv

how can I do for it?
 

cqexbesd

macrumors regular
Jun 4, 2009
159
26
Germany
I know you have put AppleScript in your subject line but it might be a task better suited to XSLT or XPath queries - you could call the appropriate tools from AppleScript if desired. Google will find you examples.
 
  • Like
Reactions: superscape

Macromagnon

macrumors newbie
Oct 20, 2014
2
0
This is a very simple text editor processing task:
Load your XML file into a text editor program that supports Grep searches, such as TextWrangler (excellent freeware).
1) "Replace all" the final </location> tag with *
2) Check the Grep search box, and use "Replace all", while searching for the Grep string <.+?> replacing it with nothing. This removes all of the XML tags, but leaves the carriage returns in the text.
3) Grep-search for the string \r+ [i.e. one or more carriage returns] replacing it with , [i.e. comma]
4) Uncheck Grep search, and "Replace all" for the string ,* replacing it with \r [i.e. carriage return symbol]
 

macweather

macrumors newbie
Original poster
Mar 12, 2017
2
0
Taiwan
+1 for the XSLT solution.

If you need to use it in AppleScript then you can always use a 'do shell script' to do the XSLT then read the results and do whatever with them.
because I need to import xml file every 10 mins with GIS
so I need to write a script that could do it automative
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.