AppleScript read XML file with a lot Same Name

Discussion in 'Mac Programming' started by macweather, Mar 12, 2017.

  1. macweather macrumors newbie

    macweather

    Joined:
    Mar 12, 2017
    Location:
    Taiwan
    #1
    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?
     
  2. cqexbesd macrumors regular

    Joined:
    Jun 4, 2009
    #2
    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.
     
  3. Macromagnon macrumors newbie

    Joined:
    Oct 20, 2014
    #3
    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]
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    +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.
     
  5. macweather thread starter macrumors newbie

    macweather

    Joined:
    Mar 12, 2017
    Location:
    Taiwan
    #5
    because I need to import xml file every 10 mins with GIS
    so I need to write a script that could do it automative
     
  6. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK

Share This Page