0

I am trying for first time to parse a XML file and I am using SWXMLHash.

My xml file is:

<weatherdata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.met.no/weatherapi/locationforecast/1.9/schema" created="2018-02-13T18:12:31Z">
    <meta>
        <model name="LOCAL" termin="2018-02-13T12:00:00Z" runended="2018-02-13T14:27:32Z" nextrun="2018-02-13T22:00:00Z" from="2018-02-13T19:00:00Z" to="2018-02-16T06:00:00Z" />
        <model name="EPS" termin="2018-02-13T00:00:00Z" runended="2018-02-13T09:03:05Z" nextrun="2018-02-13T22:00:00Z" from="2018-02-16T12:00:00Z" to="2018-02-22T18:00:00Z" />
    </meta>
    <product class="pointData">
        <time datatype="forecast" from="2018-02-13T19:00:00Z" to="2018-02-13T19:00:00Z">
            <location altitude="0" latitude="" longitude="">
                <temperature id="TTT" unit="celsius" value="3.5"/>
                <windDirection id="dd" deg="158.8" name="S"/>
                <windSpeed id="ff" mps="8.8" beaufort="5" name="Frisk bris"/>
                <windGust id="ff_gust" mps="15.0"/>
                <areaMaxWindSpeed mps="13.8"/>
                <humidity value="82.1" unit="percent"/>
                <pressure id="pr" unit="hPa" value="1002.5"/>
                <cloudiness id="NN" percent="99.3"/>
                <fog id="FOG" percent="0.0"/>
                <lowClouds id="LOW" percent="73.2"/>
                <mediumClouds id="MEDIUM" percent="0.0"/>
                <highClouds id="HIGH" percent="91.0"/>
                <dewpointTemperature id="TD" unit="celsius" value="0.6"/>
            </location>
        </time>

The <time> </time> repeates.

Having made a let parsedResultXML = SWXMLHash.parse(data)

I am trying to parse with a for in loop

for node in parsedResultXML["weatherdata"]["product"]["time"].all{
    print(node)
}

I get results

<time to="2018-02-14T01:00:00Z" from="2018-02-14T01:00:00Z" datatype="forecast">
         <location latitude="" longitude="" altitude="0">
            <temperature id="TTT" unit="celsius" value="3.4"></temperature>
            <windDirection id="dd" name="S" deg="158.7"></windDirection>
            <windSpeed name="Liten kuling" mps="11.1" id="ff" beaufort="6"></windSpeed>
            <windGust id="ff_gust" mps="19.0"></windGust>
            <areaMaxWindSpeed mps="17.5"></areaMaxWindSpeed>
            <humidity unit="percent" value="88.3"></humidity>
            <pressure id="pr" unit="hPa" value="1002.5"></pressure>
            <cloudiness id="NN" percent="99.3"></cloudiness>
            <fog id="FOG" percent="0.1"></fog>
            <lowClouds id="LOW" percent="98.6"></lowClouds>
            <mediumClouds id="MEDIUM" percent="93.4"></mediumClouds>
            <highClouds id="HIGH" percent="57.8"></highClouds>
            <dewpointTemperature id="TD" unit="celsius" value="1.5"></dewpointTemperature>
         </location>
      </time>

But I can't access the elements.

Using

for node in parsedResultXML["weatherdata"]["product"]["time"].all{
    node["time"].element?.attribute(by: "from")?.text 
}

I don't get any results back.

Any idea??

Thanks

P S
  • 527
  • 4
  • 18

1 Answers1

0

You're close... as @rmaddy commented, node is already indexed to "time". So instead of:

for node in parsedResultXML["weatherdata"]["product"]["time"].all {
    node["time"].element?.attribute(by: "from")?.text 
}

Do this instead:

for node in parsedResultXML["weatherdata"]["product"]["time"].all {
    node.element?.attribute(by: "from")?.text 
}
David Mohundro
  • 11,922
  • 5
  • 40
  • 44