# getAttribute value from XML node

**URL:** <https://discourse.processing.org/t/getattribute-value-from-xml-node/21977>\
**Category:** Coding Questions\
**Created:** [June 18, 2020, 1:06pm UTC](https://discourse.processing.org/t/getattribute-value-from-xml-node/21977 "2020-06-18T13:06:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [June 18, 2020, 1:06pm UTC](https://discourse.processing.org/t/getattribute-value-from-xml-node/21977/1 "2020-06-18T13:06:56Z")

</div>

Hi,

I’m trying to get the value of attribute in XML.  
Here’s the xml line : `<image x="20" y="190"> `  
How to have 20 in a variable ?

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 18, 2020, 6:17pm UTC](https://discourse.processing.org/t/getattribute-value-from-xml-node/21977/2 "2020-06-18T18:17:56Z")

</div>

It seems you need a valid xml: `<image></image>`

Kf

```java
XML xml, xml4;

String raw="<?xml version=\"1.0\"?><mammals><animal id=\"0\" species=\"Capra hircus\">Goat</animal><animal id=\"1\" species=\"Panthera pardus\">Leopard</animal><animal id=\"2\" species=\"Equus zebra\">Zebra</animal></mammals>";
String raw2="<?xml version=\"1.0\"?><mammals><image x=\"20\" y=\"190\">Goat</image></mammals>";
String raw3="<mammals><image x=\"20\" y=\"190\">Goat</image></mammals>";
String raw4="<image x=\"20\" y=\"190\"></image>";

void setup() {
  xml = parseXML(raw3);
  xml4 = parseXML(raw4);
  
  
  println(xml);
  XML firstChild = xml.getChild("image");
  println(firstChild.hasAttribute("x"));
  println(firstChild.hasAttribute("color"));
  
  println("DEMO=====");
  println(xml4.hasAttribute("x"));
  println(xml4.getString("x"));
}

```
