# Read external xml and access values

**URL:** <https://community.homey.app/t/read-external-xml-and-access-values/61721>\
**Category:** Questions & Help\
**Tags:** homeyscript\
**Created:** [March 15, 2022, 10:33am UTC](https://community.homey.app/t/read-external-xml-and-access-values/61721 "2022-03-15T10:33:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![opstad](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/opstad/32/49391_2.png) [@opstad](https://community.homey.app/u/opstad)\
**Post date:** [March 15, 2022, 10:33am UTC](https://community.homey.app/t/read-external-xml-and-access-values/61721/1 "2022-03-15T10:33:42Z")

</div>

I want to fetch an external xml file, and access a certain value in that xml file. Does anybody have an example for this in Homeyscript?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [March 15, 2022, 10:45am UTC](https://community.homey.app/t/read-external-xml-and-access-values/61721/2 "2022-03-15T10:45:37Z")

</div>

You may have more luck with the [HTTP request flow cards app](https://homey.app/a/com.internet/), which has XML support (as opposed to Homeyscript, which doesn’t).

---

<div class="post-metadata">

**Author:** ![ChrisBoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/chrisboer/32/1252_2.png) [@ChrisBoer](https://community.homey.app/u/ChrisBoer)\
**Post date:** [June 3, 2023, 7:48am UTC](https://community.homey.app/t/read-external-xml-and-access-values/61721/3 "2023-06-03T07:48:30Z")

</div>

@robertklep

Ik heb een vraag ik wil graag deze data uit mijn SAJ inverter uitlezen in een text variabele SAJxml  
const url = ‘[http://192.168.1.120/real\_time\_data.xml](http://192.168.1.120/real_time_data.xml)’;

\<real\_time\_data\>

Normal

227.2

0.14

49.99

22

9.3

0.0

0.6

15131.8

15086.40

41895.4

175.8

0.04

-

-

359.2

\</real\_time\_data\>

Ik probeer dit  
var xmlhttp = new XMLHttpRequest();

xmlhttp.open(“GET”, “[http://192.168.1.120/real\_time\_data.xml](http://192.168.1.120/real_time_data.xml)”, false);

xmlhttp.send();

SAJxml = xmlhttp.responseXML;

Hoe zou ik dit met HTTP request flow card kunnen doen. of beter via Homey Script omdat deze app niet meer ondersteund wordt door Homey Pro 2023

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [June 3, 2023, 8:02am UTC](https://community.homey.app/t/read-external-xml-and-access-values/61721/4 "2023-06-03T08:02:40Z")

</div>

(English only)

HomeyScript supports `fetch()` to retrieve data from a URL. However, it doesn’t support XML so you’ll have to extract the data “manually”.

I don’t know how exactly it’ll work with the HTTP Request Flow Cards app (haven’t used it for a long time), but it probably won’t be very difficult.

---

<div class="post-metadata">

**Author:** ![Kotick](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kotick/32/16570_2.png) [@Kotick](https://community.homey.app/u/Kotick)\
**Post date:** [December 28, 2023, 5:10pm UTC](https://community.homey.app/t/read-external-xml-and-access-values/61721/5 "2023-12-28T17:10:36Z")

</div>

have you had any results with this?

---

<div class="post-metadata">

**Author:** ![ChrisBoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/chrisboer/32/1252_2.png) [@ChrisBoer](https://community.homey.app/u/ChrisBoer)\
**Post date:** [December 28, 2023, 9:15pm UTC](https://community.homey.app/t/read-external-xml-and-access-values/61721/6 "2023-12-28T21:15:57Z")

</div>

Nope

---

<div class="post-metadata">

**Author:** ![Kotick](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kotick/32/16570_2.png) [@Kotick](https://community.homey.app/u/Kotick)\
**Post date:** [January 7, 2024, 3:35pm UTC](https://community.homey.app/t/read-external-xml-and-access-values/61721/7 "2024-01-07T15:35:21Z")

</div>

I managed to read some data from the inverter using HomeyScripts (with a lot of help from ChatGPT, of course).  
// Define the URL of the XML data  
const url = ‘[http://192.168.0.245/real\_time\_data.xml](http://192.168.0.245/real_time_data.xml)’;

// Make an HTTP GET request to fetch the XML data  
fetch(url)  
.then(response =\> response.text())  
.then(data =\> {  
// Parse the XML data using regular expressions  
const parsedData = parseXML(data);

```
// Access specific values from the parsed data
const state = parsedData.state;
const voltageL1 = parsedData.Vac_l1;
const currentL1 = parsedData.Iac_l1;
const frequency = parsedData.Freq1;
const powerGenerated = parsedData.pac1;
const temperature = parsedData.temp;
const energyToday = parsedData['e-today'];
const energyTotal = parsedData['e-total'];

// Print the retrieved data
console.log('Inverter State:', state);
console.log('Voltage L1:', voltageL1, 'V');
console.log('Current L1:', currentL1, 'A');
console.log('Frequency:', frequency, 'Hz');
console.log('Power Generated:', powerGenerated, 'W');
console.log('Temperature:', temperature, '°C');
console.log('Energy Today:', energyToday, 'kWh');
console.log('Energy Total:', energyTotal, 'kWh');

```

})  
.catch(error =\> {  
console.error(‘Error fetching XML data:’, error.message);  
});

// Function to parse XML data using regular expressions  
function parseXML(xml) {  
const regex = /\<(\w+)\>([^\<]+)\</\1\>/g;  
let match;  
const data = {};

while ((match = regex.exec(xml)) !== null) {  
const nodeName = match[1];  
const nodeValue = match[2];  
data[nodeName] = nodeValue;  
}

return data;  
}  
Now, I really have no idea what I can do with it in Homey. I tried to create an app, but I haven’t made much progress with that 😏
