# Extract certain values from XML

**URL:** <https://discourse.nodered.org/t/extract-certain-values-from-xml/66836>\
**Category:** General\
**Tags:** function-node\
**Created:** [24 August 2022 07:35 UTC](https://discourse.nodered.org/t/extract-certain-values-from-xml/66836 "2022-08-24T07:35:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tob84](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@tob84](https://discourse.nodered.org/u/tob84)\
**Post date:** [24 August 2022 07:35 UTC](https://discourse.nodered.org/t/extract-certain-values-from-xml/66836/1 "2022-08-24T07:35:32Z")

</div>

This should be a simple problem to solve but I'm not good with code so I've come up stuck

I have an XML that i'm converting using the json node to extact vertain values using a function node.

I want to get just the type, cost and colour of each object and convert back to xml.

```auto
<?xml version="1.0" standalone="yes"?>
<Products>
  <Product>
    <Type>one</Type>
    <Cost>2</Cost>
    <Quantity>10</Quantity>
	<Size>small</Size>
	<Weight>heavy</Weight>
	<Colour>red</Colour>
   </Product>
     <Product>
    <Type>two</Type>
    <Cost>5</Cost>
    <Quantity>10</Quantity>
	<Size>medium</Size>
	<Weight>light</Weight>
	<Colour>green</Colour>
   </Product>
     <Product>
    <Type>three</Type>
    <Cost>3</Cost>
    <Quantity>10</Quantity>
	<Size>small</Size>
	<Weight>heavy</Weight>
	<Colour>red</Colour>
   </Product>
     <Product>
    <Type>four</Type>
    <Cost>12</Cost>
    <Quantity>10</Quantity>
	<Size>large</Size>
	<Weight>heavy</Weight>
	<Colour>yellow</Colour>
   </Product>
     <Product>
    <Type>five</Type>
    <Cost>25</Cost>
    <Quantity>10</Quantity>
	<Size>small</Size>
	<Weight>light</Weight>
	<Colour>red</Colour>
   </Product>
     <Product>
    <Type>six</Type>
    <Cost>25</Cost>
    <Quantity>18</Quantity>
	<Size>medium</Size>
	<Weight>light</Weight>
	<Colour>blue</Colour>
   </Product>
</Products>
  

```

I have a function which will give me the type and this is where I'm stuck, how to format my function to return type, cost and colour?

```auto
for (let i = 0; i < msg.payload.Products.Product.length; i++)

{
    
msg.payload.Products.Product[i] = [msg.payload.Products.Product[i].Type[0]];

}

return msg;

```

Thanks

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 August 2022 08:32 UTC](https://discourse.nodered.org/t/extract-certain-values-from-xml/66836/2 "2022-08-24T08:32:41Z")

</div>

Here is one way...

```auto
const data = msg.payload.Products.Product
for (let index = 0; index < data.length; index++) {
    const Product = data[index];
    data[index] = {
        Type: Product.Type,
        Cost: Product.Cost,
        Colour: Product.Colour,
    }
}
return msg;

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2e893ede6133d1d9557aca2357554856b3ac7927.png)

Flow...

```auto
[{"id":"2fad0bd21ed94ffd","type":"inject","z":"49f61d916c8f6022","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":1490,"y":100,"wires":[["bfd4939520419f99"]]},{"id":"bfd4939520419f99","type":"template","z":"49f61d916c8f6022","name":"original XML","field":"payload","fieldType":"msg","format":"handlebars","syntax":"plain","template":"<?xml version=\"1.0\" standalone=\"yes\"?>\n<Products>\n <Product>\n <Type>one</Type>\n <Cost>2</Cost>\n <Quantity>10</Quantity>\n\t<Size>small</Size>\n\t<Weight>heavy</Weight>\n\t<Colour>red</Colour>\n </Product>\n <Product>\n <Type>two</Type>\n <Cost>5</Cost>\n <Quantity>10</Quantity>\n\t<Size>medium</Size>\n\t<Weight>light</Weight>\n\t<Colour>green</Colour>\n </Product>\n <Product>\n <Type>three</Type>\n <Cost>3</Cost>\n <Quantity>10</Quantity>\n\t<Size>small</Size>\n\t<Weight>heavy</Weight>\n\t<Colour>red</Colour>\n </Product>\n <Product>\n <Type>four</Type>\n <Cost>12</Cost>\n <Quantity>10</Quantity>\n\t<Size>large</Size>\n\t<Weight>heavy</Weight>\n\t<Colour>yellow</Colour>\n </Product>\n <Product>\n <Type>five</Type>\n <Cost>25</Cost>\n <Quantity>10</Quantity>\n\t<Size>small</Size>\n\t<Weight>light</Weight>\n\t<Colour>red</Colour>\n </Product>\n <Product>\n <Type>six</Type>\n <Cost>25</Cost>\n <Quantity>18</Quantity>\n\t<Size>medium</Size>\n\t<Weight>light</Weight>\n\t<Colour>blue</Colour>\n </Product>\n</Products>","output":"str","x":1640,"y":100,"wires":[["468a8c5864382d4c"]]},{"id":"468a8c5864382d4c","type":"xml","z":"49f61d916c8f6022","name":"","property":"payload","attr":"","chr":"","x":1790,"y":100,"wires":[["7dfd99eabe79f7c2","1cb607b178bc43a9"]]},{"id":"7dfd99eabe79f7c2","type":"function","z":"49f61d916c8f6022","name":"Keep Type, Cost & Colour","func":"const data = msg.payload.Products.Product\nfor (let index = 0; index < data.length; index++) {\n let Product = data[index];\n data[index] = {\n Type: Product.Type,\n Cost: Product.Cost,\n Colour: Product.Colour,\n }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1590,"y":160,"wires":[["4fb306fb0e154988"]]},{"id":"1cb607b178bc43a9","type":"debug","z":"49f61d916c8f6022","name":"before","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1930,"y":100,"wires":[]},{"id":"4fb306fb0e154988","type":"xml","z":"49f61d916c8f6022","name":"","property":"payload","attr":"","chr":"","x":1790,"y":160,"wires":[["7464e8e3b370722e"]]},{"id":"7464e8e3b370722e","type":"debug","z":"49f61d916c8f6022","name":"after","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1930,"y":160,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![tob84](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@tob84](https://discourse.nodered.org/u/tob84)\
**Post date:** [24 August 2022 08:50 UTC](https://discourse.nodered.org/t/extract-certain-values-from-xml/66836/3 "2022-08-24T08:50:39Z")

</div>

Steve thank you so much that works perfectly.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 September 2022 08:50 UTC](https://discourse.nodered.org/t/extract-certain-values-from-xml/66836/4 "2022-09-07T08:50:47Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
