# Difficulty accessing converted xml child nodes

**URL:** <https://community.postman.com/t/difficulty-accessing-converted-xml-child-nodes/4272>\
**Category:** Help Hub\
**Created:** [February 25, 2019, 9:56am UTC](https://community.postman.com/t/difficulty-accessing-converted-xml-child-nodes/4272 "2019-02-25T09:56:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crispness](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/crispness/32/1367_2.png) [@crispness](https://community.postman.com/u/crispness)\
**Post date:** [February 25, 2019, 9:56am UTC](https://community.postman.com/t/difficulty-accessing-converted-xml-child-nodes/4272/1 "2019-02-25T09:56:23Z")

</div>

Hi  
I generally work with XML and while I understand what JSON is, reading it is not second nature to me. So I am trying to access a particular child-node within an XML response, without success.

I am using code I nabbed from [this thread](https://community.postman.com/t/how-to-extract-a-value-attribute-from-an-input-tag-where-the-body-is-a-web-page/1535)

```auto
    var parseString = require('xml2js').parseString;

    parseString(pm.response.text(), function (err, result) {
        pm.globals.set("xml_parsed_response", JSON.stringify(result));
    });

    let xml_string = pm.globals.get("xml_parsed_response");

    console.log(xml_string);
    pm.globals.set("tranlog_tranid", xml_string.tranlist.trans.TranID);

```

And I get an error “TypeError | Cannot read property ‘trans’ of undefined”

The value of xml\_string is

```auto
    {
        "tranlist": {
            "$": {"xmlns:sql": "urn:schemas-microsoft-com:xml-sql"},
            "trans": [
                {
                    "PIUID": ["99999"],
                    "TranID": ["21601"],
                    "DateCreated": ["2019-02-22T16:04:00"],
                    "ByWhom": ["sglOffice"],
                    "DateAmended": ["2019-02-22T16:04:00"],
                    "AmendBy": ["sglOffice"],
                    "What": ["PostmanTest"]
                }
            ]
        }
    }

```

I am trying to set the value of global ‘tranlog\_tranid’ to 21601 in this instance.

Any idea what I am doing wrong?

---

<div class="post-metadata">

**Author:** ![prashant\_postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/prashant_postman/32/25_2.png) [@prashant\_postman](https://community.postman.com/u/prashant_postman)\
**Post date:** [February 26, 2019, 3:27pm UTC](https://community.postman.com/t/difficulty-accessing-converted-xml-child-nodes/4272/2 "2019-02-26T15:27:31Z")

</div>

@crispness `parsString` is an asynchronous function. In your code, you are expecting it to be synchronously executed. Try this:

```auto
var parseString = require('xml2js').parseString;

parseString(pm.response.text(), function (err, result) {
    pm.globals.set("tranlog_tranid", result.tranlist.trans[0].TranID[0]);
});

```

---

<div class="post-metadata">

**Author:** ![crispness](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/crispness/32/1367_2.png) [@crispness](https://community.postman.com/u/crispness)\
**Post date:** [February 27, 2019, 9:39am UTC](https://community.postman.com/t/difficulty-accessing-converted-xml-child-nodes/4272/3 "2019-02-27T09:39:09Z")

</div>

Thanks @prashant_postman

Is there any documentation to go with that library?

---

<div class="post-metadata">

**Author:** ![prashant\_postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/prashant_postman/32/25_2.png) [@prashant\_postman](https://community.postman.com/u/prashant_postman)\
**Post date:** [February 27, 2019, 9:52am UTC](https://community.postman.com/t/difficulty-accessing-converted-xml-child-nodes/4272/4 "2019-02-27T09:52:41Z")

</div>

@crispness Yes [https://www.npmjs.com/package/xml2js](https://www.npmjs.com/package/xml2js)
