# Extarcting values from XML files

**URL:** <https://discourse.nodered.org/t/extarcting-values-from-xml-files/13131>\
**Category:** General\
**Created:** [10 July 2019 21:20 UTC](https://discourse.nodered.org/t/extarcting-values-from-xml-files/13131 "2019-07-10T21:20:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![darkdude](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@darkdude](https://discourse.nodered.org/u/darkdude)\
**Post date:** [10 July 2019 21:20 UTC](https://discourse.nodered.org/t/extarcting-values-from-xml-files/13131/1 "2019-07-10T21:20:15Z")

</div>

I'm using Node Red to extract data from reports generated by some medical equipment. These reports are in general XML files. And while these devices do their medical job properly, they are really bad at making reports. Meaning XML files are often faulty. While data I need to extract (process status, tempreature, user id etc.) are always there, some sections are often missing. The file is still converted by XML node to JS object but paths to values I need always change depending on what is missing.

Is it possible to extract a value that is once in msg.payload.Signature.Object[0].Illumination[0].Header[0].User[0], then in msg.payload.Illumination.Header[0].User and then somewhere else? Meaning it's always .User but its position inside an object is always different?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 July 2019 21:40 UTC](https://discourse.nodered.org/t/extarcting-values-from-xml-files/13131/2 "2019-07-10T21:40:18Z")

</div>

You might be better keeping the XML as a string and trying to process it that way. If the XML changes to that degree, it is going to be next to impossible to rely on a general purpose XML-\>JSON conversion.

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [11 July 2019 02:51 UTC](https://discourse.nodered.org/t/extarcting-values-from-xml-files/13131/3 "2019-07-11T02:51:16Z")

</div>

You could try feeding the XML into an `HTML` node and use a CSS selector to find the User tag.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 July 2019 03:00 UTC](https://discourse.nodered.org/t/extarcting-values-from-xml-files/13131/4 "2019-07-11T03:00:36Z")

</div>

> [@darkdude](#):
>
> it's always .User but its position inside an object is always different

Always different or predictable different ? (like in 3 or 4 variations ?)

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [11 July 2019 06:11 UTC](https://discourse.nodered.org/t/extarcting-values-from-xml-files/13131/5 "2019-07-11T06:11:32Z")

</div>

> [@darkdude](#):
>
> Is it possible to extract a value that is once in msg.payload.Signature.Object[0].Illumination[0].Header[0].User[0], then in msg.payload.Illumination.Header[0].User and then somewhere else? Meaning it's always .User but its position inside an object is always different?

Is the path stable enough to craft an XPath for it to read? I’d suggest to keep it as xml, load it in a (function) node and find the value through an xpath search. Without having seen what your XMLs look like I can’t give stable suggestions but looking at the xpath specifications won’t hurt 🙂

1.0 [XML Path Language (XPath)](https://www.w3.org/TR/1999/REC-xpath-19991116/)  
2.0 [XML Path Language (XPath) 2.0 (Second Edition)](https://www.w3.org/TR/2010/REC-xpath20-20101214/)  
2.1, later renamed to 3.0 [XML Path Language (XPath) 3.0](https://www.w3.org/TR/2014/REC-xpath-30-20140408/)  
3.1 [XML Path Language (XPath) 3.1](https://www.w3.org/TR/2017/REC-xpath-31-20170321/)

It will depend on compatibility what version you will need, I’m not sure which version is available in browser-based javascript, nor do I know from mind if node has compatibility for it or if you need an additional library.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 July 2019 06:42 UTC](https://discourse.nodered.org/t/extarcting-values-from-xml-files/13131/6 "2019-07-11T06:42:24Z")

</div>

or convert to json and use jsonata, which is made exactly for this purpose, traversing messy objects.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [12 July 2019 02:53 UTC](https://discourse.nodered.org/t/extarcting-values-from-xml-files/13131/7 "2019-07-12T02:53:46Z")

</div>

I couldn't agree more -- check out this section of the JSONata docs:

> **[Query refinement using predicate expressions · JSONata](http://docs.jsonata.org/predicate#navigate-arbitrary-depths)**
>
> Predicates

From what I'm reading, if you just need all the `User` objects (or strings) at any level in your structure, you should be able to get a list of them using the expression `payload.**.User`

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [12 July 2019 09:18 UTC](https://discourse.nodered.org/t/extarcting-values-from-xml-files/13131/8 "2019-07-12T09:18:30Z")

</div>

However you end up doing it, please do update this thread to let us know how you got on.
