# How to fetch and parse XML data in the new UI's (e.g. Notebooks) code tile?

**URL:** <https://community.dynatrace.com/t/how-to-fetch-and-parse-xml-data-in-the-new-uis-e-g-notebooks-code-tile/40817>\
**Category:** Developer Q&A Forum\
**Tags:** app-functions\
**Created:** [July 21, 2023, 10:32am UTC](https://community.dynatrace.com/t/how-to-fetch-and-parse-xml-data-in-the-new-uis-e-g-notebooks-code-tile/40817 "2023-07-21T10:32:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalelazic](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@kalelazic](https://community.dynatrace.com/u/kalelazic)\
**Post date:** [July 21, 2023, 10:32am UTC](https://community.dynatrace.com/t/how-to-fetch-and-parse-xml-data-in-the-new-uis-e-g-notebooks-code-tile/40817/1 "2023-07-21T10:32:16Z")

</div>

I found a public api with XML mock data: [https://mocktarget.apigee.net/xml](https://mocktarget.apigee.net/xml)

```auto
<root>
  <city>San Jose</city>
  <firstName>John</firstName>
  <lastName>Doe</lastName>
  <state>CA</state>
</root>

```

would like to fetch and access the data within \<city\>.

I tried the following, but DOMParser is not defined.

```auto
export default async function () {
  const apiUrl = 'https://mocktarget.apigee.net/xml';
  const response = await fetch(apiUrl);
  const result = await response.text()
  let parser = new DOMParser(),
			xmlDoc = parser.parseFromString (result, 'text/xml');
  return xmlDoc.getElementsByTagName ('city');
}

```

```auto
{
  "error": {
    "code": 540,
    "message": "Execution crashed.",
    "details": {
      "logs": "",
      "type": "UNCAUGHT_EXCEPTION",
      "message": "Uncaught (in promise) ReferenceError: DOMParser is not defined",
      "details": {
        "sourceLine": " let parser = new DOMParser(),",
        "lineNumber": 7,
        "startColumn": 15,
        "stack": "Uncaught (in promise) ReferenceError: DOMParser is not defined\n at default (script.ts:7:15)\n at eventLoopTick (script.ts:11:1)"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![imsingh](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/imsingh/32/7858_2.png) [@imsingh](https://community.dynatrace.com/u/imsingh)\
**Post date:** [July 24, 2023, 7:35am UTC](https://community.dynatrace.com/t/how-to-fetch-and-parse-xml-data-in-the-new-uis-e-g-notebooks-code-tile/40817/2 "2023-07-24T07:35:56Z")

</div>

Hi there

At the moment, DOMParser API isn’t supported in the app function. Take a look at the current supported APIs at&nbsp;[JavaScript runtime | Dynatrace Developer](https://developer.dynatrace.com/reference/javascript-runtime/)

However, you can send the full XML document as a text to the frontend and use DOM parser there. Take a look at the following example:

1. App function

```javascript
export default async function (payload: unknown = undefined) {
  const apiUrl = 'https://mocktarget.apigee.net/xml';
  const response = await fetch(apiUrl);
  const result = await response.text()
  return result;
}

```

2. Dynatrace App(frontend):

```markup
functions.call('xml-parser') //xml-parser is the name of the function
    .then((result) => result.text())
    .then((result) => {
      let parser = new DOMParser(),
        xmlDoc = parser.parseFromString(result, "text/xml");
      return xmlDoc.getElementsByTagName("city");
    }).then(result => console.log(result[0].textContent)); // San Jose

```

Let me know if it works!

---

<div class="post-metadata">

**Author:** ![kalelazic](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@kalelazic](https://community.dynatrace.com/u/kalelazic)\
**Post date:** [July 24, 2023, 7:55am UTC](https://community.dynatrace.com/t/how-to-fetch-and-parse-xml-data-in-the-new-uis-e-g-notebooks-code-tile/40817/3 "2023-07-24T07:55:55Z")

</div>

Hi Im! Thanks for the reply.

I tried to apply this inside the existing app Notebooks - in the code tile.&nbsp;  
However, I guess you suggestion rather works for the custom app context.  
I could not manage to embed this within the code tile alone.

---

<div class="post-metadata">

**Author:** ![imsingh](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/imsingh/32/7858_2.png) [@imsingh](https://community.dynatrace.com/u/imsingh)\
**Post date:** [July 24, 2023, 8:23am UTC](https://community.dynatrace.com/t/how-to-fetch-and-parse-xml-data-in-the-new-uis-e-g-notebooks-code-tile/40817/4 "2023-07-24T08:23:49Z")

</div>

You can also use this tiny xml parser library([GitHub - TobiasNickel/tXml: ⚡very small and fast xml-parser in pure javascript⚡ · GitHub](https://github.com/TobiasNickel/tXml)) in app functions/Notebook. Just copy the code from the github and add it inline in your app function as following:

```markup
// tXml code here

// your app function logic
export default async function (payload: unknown = undefined) {
  const apiUrl = 'https://mocktarget.apigee.net/xml';
  const response = await fetch(apiUrl);
  const result = await response.text()
  const parsedData = txml.simplify(txml.parse(result));
  return parsedData.city;
}

```

This will work, however it isn’t a good idea.
