Read XML from URL

Just starting out? Need help? Post your questions and find answers here.
JackWebb
Posts: 6
Joined: Sun Mar 02, 2014 7:51 pm

Read XML from URL

Post by JackWebb »

New to XML and so far it seems far more complex than it needs to be. But I'm betting it's really easy and I'm just missing some information. I am attempting to read an XML from a URL
https://api.met.no/weatherapi/locationf ... 0&lon=9.58

I'm not looking to use the entire file and all it's data, I am simply looking to get the temperature and current weather conditions such as rain etc.

And here is the documentation
https://api.met.no/weatherapi/locationf ... umentation

I have no idea what I'm doing, the docs and forum are light on examples.

Thank you in advance!
Jack :D
Simplicity is the ultimate sophistication. ~ Leonardo Da Vinci
poshu
Posts: 96
Joined: Mon Feb 24, 2014 11:46 pm

Re: Read XML from URL

Post by poshu »

And what is the problem? Where does it fail?

Let me get my crystal ball... It says CORS.
JackWebb
Posts: 6
Joined: Sun Mar 02, 2014 7:51 pm

Re: Read XML from URL

Post by JackWebb »

poshu wrote:And what is the problem? Where does it fail?

Let me get my crystal ball... It says CORS.
And if I knew that, then I wouldn't need to ask the question now would I? What does your crystal ball say about that?
Simplicity is the ultimate sophistication. ~ Leonardo Da Vinci
User avatar
Peter
Posts: 1093
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Read XML from URL

Post by Peter »

@JackWebb: Did you solve your problem?

Greetings ... Peter
plouf
Posts: 196
Joined: Tue Feb 25, 2014 6:01 pm
Location: Athens,Greece

Re: Read XML from URL

Post by plouf »

where exaclty have you failed ?

have you actually manage to read the file ?
are you aware that you cannon read files from diffirent domain ?
i.e. if you script runs under https://api.met.no server domain
Christos
JackWebb
Posts: 6
Joined: Sun Mar 02, 2014 7:51 pm

Re: Read XML from URL

Post by JackWebb »

Peter wrote:@JackWebb: Did you solve your problem?

Greetings ... Peter
Thank you Peter for your reply. I have no time to code anymore, and my health dictates how many hours I can spend sitting down unfortunately. My problem is that I simply do not know how to use this library. I can load the file successfully it seems, but then what? So for now I am just using a scraping method until I have some time to figure this out.
plouf wrote:where exaclty have you failed ?
I don't know that it failed, I don't think it has. This is the code I was using to experiment with the XML library. Not much I know but I didn't get very far.

Code: Select all

Enumeration
  #XML
EndEnumeration

Procedure.s XMLStatusString(StatusCode)
  Protected Result$

  Select StatusCode
  Case  #PB_XML_Success
    Result$ = "no error"
  EndSelect

  ProcedureReturn Result$
EndProcedure

FileName.s = "https://api.met.no/weatherapi/locationforecast/1.9/?lat=60.10&lon=9.58;"

If  LoadXML(#XML, FileName$)
  Debug "LoadXML = " + "loaded"
  StatusCode = XMLStatus(#XML)
  Debug "XMLStatusString = " + XMLStatusString(StatusCode)
  If IsXML(#XML) <> 0 And StatusCode = #PB_XML_Success
    ParseXML(#XML, FileName$)
    encoding = GetXMLEncoding(#XML)
    Select encoding
      Case #PB_Ascii   : Debug "Ascii"
      Case #PB_Unicode : Debug "UTF16"
      Case #PB_UTF8    : Debug "UTF8"
    EndSelect
    Debug "Encoding = " + encoding
    Debug "ExamineXMLAttributes = " + ExamineXMLAttributes(#PB_XML_Normal)
    Debug "IsXML = " + IsXML(#XML)
    Debug "StatusCheck = " + GetXMLNodeName(MainXMLNode(#XML))
    Debug "GetXMLNodeText = " + GetXMLNodeText(#PB_XML_Normal)
    Debug "RootXMLNode = " + RootXMLNode(#XML)
    Debug "ResolveXMLNodeName = " + ResolveXMLNodeName(#PB_XML_Normal)
    Debug "ResolveXMLAttributeName = " + ResolveXMLAttributeName(#PB_XML_Normal, "forecast")
  EndIf
EndIf
Simplicity is the ultimate sophistication. ~ Leonardo Da Vinci
plouf
Posts: 196
Joined: Tue Feb 25, 2014 6:01 pm
Location: Athens,Greece

Re: Read XML from URL

Post by plouf »

never bother with lib but i know that you canot access anything in a different domain tha the one you run to

i.e. you script MUST run in api.met.no server
in ADDITION as manual says you MUST bind a procedure in loading

the following example starts to work if you save your XML to a local file in the same folder/server you run this script

Code: Select all

Enumeration
  #XML
EndEnumeration

Procedure.s XMLStatusString(StatusCode)
  Protected Result$

  Select StatusCode
  Case  #PB_XML_Success
    Result$ = "no error"
  Case #PB_XML_Error
    result$= "error loading"
  EndSelect

  ProcedureReturn Result$
EndProcedure

FileName$ = "https://api.met.no/weatherapi/locationforecast/1.9/?lat=60.10&lon=9.58;"
filename$ = "api.met.no.xml"
Procedure  Loading()
  Debug "LoadXML = " + "loaded"
  StatusCode = XMLStatus(#XML)
  Debug "XMLStatusString = " + XMLStatusString(StatusCode)
  If IsXML(#XML) <> 0 And StatusCode = #PB_XML_Success
   ; ParseXML(#XML, FileName$)
    encoding = GetXMLEncoding(#XML)
    Select encoding
      Case #PB_Ascii   : Debug "Ascii"
      Case #PB_Unicode : Debug "UTF16"
      Case #PB_UTF8    : Debug "UTF8"
    EndSelect
    Debug "Encoding = " + encoding
    Debug "ExamineXMLAttributes = " + ExamineXMLAttributes(#PB_XML_Normal)
    Debug "IsXML = " + IsXML(#XML)
    Debug "StatusCheck = " + GetXMLNodeName(MainXMLNode(#XML))
    Debug "GetXMLNodeText = " + GetXMLNodeText(#PB_XML_Normal)
    Debug "RootXMLNode = " + RootXMLNode(#XML)
    Debug "ResolveXMLNodeName = " + ResolveXMLNodeName(#PB_XML_Normal)
    Debug "ResolveXMLAttributeName = " + ResolveXMLAttributeName(#PB_XML_Normal, "forecast")
  EndIf
EndProcedure

; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loading())
;BindEvent(#PB_Event_LoadingError, @LoadingError())
LoadXML(#XML, FileName$)
Christos
Post Reply