Skip to main content
Question

Extract key value pairs from XML

  • November 29, 2022
  • 5 replies
  • 296 views

nedwaterman
Contributor
Forum|alt.badge.img+9

Morning,

 

I'm trying to extract logger data from an api returning XML. I can use the XMLFlattener to match my message element and return the pt element as an xml fragment. However I need to then expose all the key value pairs in the <pt></pt> element into attributes and if I try to use the AttributeExposer there's a huge list, which will change when i run the api with different data ranges (ie more key value pairs to manually expose).

Is there any way I can automatically expose all of the elements within the <pt></pt> xml element one column each of time, value, ch, chtype etc to insert into a db table?

 

Cheers!

 

 

5 replies

nielsgerrits
VIP
Forum|alt.badge.img+60
  • 2938 replies
  • November 29, 2022

You can do this using an XMLFragmenter. Elements to match:

messages/message/message/pt

 Check the enable flattening box and expose the flattened attributes.


nedwaterman
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • 69 replies
  • November 30, 2022

Thank you! I had not put in the forward slashes, so the xml elements weren't being fragmented. Cheers for you help!


nedwaterman
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • 69 replies
  • January 3, 2023

Hi Niels

This is working but the original values (such as the logger id) are missing. Any idea how I can carry these through?


kailinatsafe
Safer
Forum|alt.badge.img+23
  • Safer
  • 720 replies
  • January 4, 2023

Hello @nedwaterman​, take a look at the attached workspace and let me know if this workspace accomplishes the goal. It seems like only data messages will carry a <pt> element. I recommend using an AttributeRemover or AttributeKeeper to tidy the attributes afterwards! This article is also a great resource for extracting key/value pairs that come out of XML, KML, etc (see Method 2). Happy to help, Kailin.


nedwaterman
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • 69 replies
  • January 5, 2023

Hello @nedwaterman​, take a look at the attached workspace and let me know if this workspace accomplishes the goal. It seems like only data messages will carry a <pt> element. I recommend using an AttributeRemover or AttributeKeeper to tidy the attributes afterwards! This article is also a great resource for extracting key/value pairs that come out of XML, KML, etc (see Method 2). Happy to help, Kailin.

Thanks Kallin - I'll check it out!