XML Parser?

Anyone know of an XML parser? I have an API that only returns XML. Would love to convert to JSON. I can do it through another API, but would prefer to do it in code if possible.

xpath is built into pixlet/starlark. it works good.
below is a snippet from my noaa_buoy app which parses xml/rss

load("xpath.star", "xpath")
def fetch_data(buoy_id):
    data = dict()

    url = "https://www.ndbc.noaa.gov/data/latest_obs/%s.rss" % buoy_id.lower()
    resp = http.get(url)
    if resp.status_code != 200:
        #fail("request failed with status %d", resp.status_code)
        data["name"] = buoy_id
        data["error"] = "ID not valid"
        return data
    else:
        data["name"] = name_from_rss(xpath.loads(resp.body())) or buoy_id
        data_string = xpath.loads(resp.body()).query("/rss/channel/item/description")

        # continue with parsing build up the list
        re_dict = dict()
1 Like

Nice. Where did you find that? Couldn’t find the docs.

Also, I need to iterate through multiple child nodes. Doesn’t seem to be an iterator or way to get child node count.

Any ideas?

I’m not completely sure but I think a query_all should return multiple nodes in a list if there is more than one.
then you could do a count = len(query_result). to find the number it found.

If you look in the community repo the three apps that use xpath are :
bgghotness/bgg_hotness.star:load(“xpath.star”, “xpath”)
noaabuoy/noaa_buoy.star:load(“xpath.star”, “xpath”)
theysaidso/they_said_so.star:load(“xpath.star”, “xpath”)

Could you tell me where to find the community repo?

1 Like

Thanks so much. Funny that there’s no mention of the xpath module in the Tidbyt documentation.

There is!
https://tidbyt.dev/docs/reference/modules#pixlet-module-xpath

Anyway, the usability on the modules page is not the best. I’ll bet that most people think that the list at the start of the page has all the avaiable modules, when in fact you need to scroll through the page to find the others. :confused: