Read Data from XML

  1. 2 weeks ago

    Markus R

    Feb 9 Pre-Release Testers, Xojo Pro Europe / Germany
    Edited 2 weeks ago

    hello,
    how can i get the elements by name
    response
    .data
    ..METAR
    ...temp_c
    and then the temp_c value?

    i can't believe that there is only a Child by index method.
    i guess i need to add a Extends method to find something by name.

    <response version="1.2" xsi:noNamespaceSchemaLocation="http://aviationweather.gov/adds/schema/metar1_2.xsd">
    <data num_results="2">
    <METAR>
    <temp_c>9.0</temp_c>

    XQL is your friend here.
    Check the docs... it could return several nodes, not just one, so you start with an XMLNodeList

    Something like this should help:

    Dim ListNodes As XmlNodeList = TheXMLNode.XQL("//temp_c")
    dim theNode as XmlNode = ListNodes.item(0)
    dim theValue as string = theNode.FirstChild.Value
  2. Jeff T

    Feb 9 Pre-Release Testers Answer Midlands of England, Europe

    XQL is your friend here.
    Check the docs... it could return several nodes, not just one, so you start with an XMLNodeList

    Something like this should help:

    Dim ListNodes As XmlNodeList = TheXMLNode.XQL("//temp_c")
    dim theNode as XmlNode = ListNodes.item(0)
    dim theValue as string = theNode.FirstChild.Value
  3. Antonio R

    Feb 10 Pre-Release Testers, Xojo Pro, Third Party Store Europe (Italy)

    Remember that it all depends on the structure of your XML
    //nodeName will return all the nodes with that name in your document and sometime it could be misleading.
    I usually prefer a more difensive solution using the path starting from the documentNode

    For example in your case, say theXML is the XMLDocument:
    Var theNodes as XMLNodeList=theXML.documentElement("data/METAR/temp_c")

    using this strategy you can use as starting node any internal node you can identify.
    More over you can select between siblings nodes: theXML.documentElement("data[@num_result=""2""]/METAR/temp_c")

    In any case the value could be not existent a better XQL could be (valid for both strategy):
    //temp_c/text()
    or
    data/METAR/temp_c/text()

    now if you get a list of noted (XQL result length >0) you are sure that is the desired text node and now you can get the value.

  4. Markus R

    Feb 10 Pre-Release Testers, Xojo Pro Europe / Germany

    thank you all :) both answering my question.
    i overlooked this query feature in help.

  5. Emile S

    Feb 10 Europe (France, Strasbourg)

    @Markus R i overlooked this query feature in help.

    You’re not the only one (Thanks Jeff).

or Sign Up to reply!