How to extract fields from an XML to a map in a dynamic way?

For fun and learn, I`m working on a library to use Amazon Product Advertisement API. When you make a request, the API returns an XML with a lot of fields.

Now I’m using Sweetxml to access elements, in this way:

    asin: ~x"./ASIN/text()"s,
    title: ~x"./ItemAttributes/Title/text()"s,
    brand: ~x"./ItemAttributes/Brand/text()"s,
    colour: ~x"./ItemAttributes/Color/text()"s,

It returns a map with the results, and It is working pretty well, but I’m looking for a more dynamic way. For example, if you want a book, you want title and author, but you don’t need the colour. If you want a laptop, maybe you need the brand and the Amazon offers.

Sometimes you could need a different structure on the map, for example with the total result count in the root of the map, or with the first offer in the root of every item.

My problem is that I’m not sure how to do that. I have thought on macros, but I don’t know if it is like to use a hammer to crack a nut.

Any idea about how to apply SweetXML syntax dynamically?

Thaks in advance

1 Like

Did you try o to make it optional?

Check out ex_aws since it already handles some xml req/resp.