# XML Parse Attribute

**URL:** <https://forum.servoy.com/t/xml-parse-attribute/17462>\
**Category:** Classic Servoy\
**Created:** [September 12, 2013, 5:52pm UTC](https://forum.servoy.com/t/xml-parse-attribute/17462 "2013-09-12T17:52:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobcusick1332964082](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@bobcusick1332964082](https://forum.servoy.com/u/bobcusick1332964082)\
**Post date:** [September 12, 2013, 5:52pm UTC](https://forum.servoy.com/t/xml-parse-attribute/17462/1 "2013-09-12T17:52:03Z")

</div>

Hey folks - I’m accessing a web service that returns “funky” XML as the response:

```auto
<result>
  <record>
    <field name="donor_id" id="donor_id" value="1"/>
    <field name="first_name" id="first_name" value="John D."/>
    <field name="last_name" id="last_name" value="McClurkin"/>
    <field name="middle_name" id="middle_name" value=""/>
  </record>
  <record>
    <field name="donor_id" id="donor_id" value="2"/>
    <field name="first_name" id="first_name" value="Bill"/>
    <field name="last_name" id="last_name" value="Jones"/>
    <field name="middle_name" id="middle_name" value=""/>
  </record>
</result>

```

That’s literally all it returns. There is no default namespace.

So, I thought it would be a piece of cake parsing it using this:

```auto
var str = plugins.http.getPageData("myLongURL")
  var myXML = new XML(str);
  var firstName = myXML.result.record.field.attribute("first_name").toString()

```

I know that I’m doing something stupid ![:D]( "Very Happy") but everything I try just winds up with an empty string.

Then I tried:

```auto
var firstName = myXML.result.record[0].field.attribute("first_name").toString()

```

But that just got an error: ```  
TypeError: Cannot read property "field" from undefined

```auto

I've already had a look at <a class="postlink-local" href="https://www.servoy.com/forum/viewtopic.php?f=22&t=15818&p=85410">viewtopic.php?f=22&t=15818&p=85410</a> and I guess I'm just missing it.

Any pointers would be greatly appreciated!
```

---

<div class="post-metadata">

**Author:** ![bobcusick1332964082](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@bobcusick1332964082](https://forum.servoy.com/u/bobcusick1332964082)\
**Post date:** [September 12, 2013, 8:00pm UTC](https://forum.servoy.com/t/xml-parse-attribute/17462/2 "2013-09-12T20:00:18Z")

</div>

**UPDATE - ANSWERED**

OK, after a lot of time and effort - I finally found the right syntax:

```auto
myXML.record[0].field[1].attribute("value").toString()

```

I was trying to filter the FIELDS by the VALUE of the “name” property - rather than getting the ATTRIBUTE itself (e.g. “value” rather than “first\_name”).

Also, by explicitly calling specific records and fields (with the array notation), then getting the ATTRIBUTE “value” - bingo.

---

<div class="post-metadata">

**Author:** ![ptalbot](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/ptalbot/32/4952_2.png) [@ptalbot](https://forum.servoy.com/u/ptalbot)\
**Post date:** [September 12, 2013, 10:09pm UTC](https://forum.servoy.com/t/xml-parse-attribute/17462/3 "2013-09-12T22:09:59Z")

</div>

XML to be well formed, need a root Node. That root node is implied when you do new XML(string): your object becomes the root. And since you have more than one record and field nodes, they are XMLList (equivalent to arrays).
