getAttribute value from XML node

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 ?

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

Kf

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"));
}
1 Like