Attributes

Reading Attributes from XML

  1. SpinXmlDomAttribute attribute = XML("<order id=\"order1\" />").attr("id");
  2. String id = XML("<order id=\"order1\" />").attr("id").value();

The attr method returns a wrapper of the XML attribute and with value the value of the attribute can be accessed.

If you want to access an attribute in another namespace, you have to use the attrNs method.

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<order xmlns:cam=\"http://camunda.org/example\" id=\"order1\" cam:name=\"order1\" />";
  3. SpinXmlDomAttribute attribute = XML(xml).attrNs("http://camunda.org/example", "name");

You can also get a collection of all attributes or only of a specific namespace.

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<order xmlns:cam=\"http://camunda.org/example\" id=\"order1\" cam:name=\"order1\" />";
  3. // All attributes
  4. SpinCollection<SpinXmlDomAttribute> attributes = XML(xml).attrs();
  5. // All attributes of a specific namespace
  6. attributes = XML(xml).attrs("http://camunda.org/example");

Or you can directly get all attribute names instead.

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<order xmlns:cam=\"http://camunda.org/example\" id=\"order1\" cam:name=\"order1\" />";
  3. // All attribute names
  4. List<String> names = XML(xml).attrNames();
  5. // All attribute names of a specific namespace

You can also specify the namespace of the attribute to set.

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<order xmlns:cam=\"http://camunda.org/example\" id=\"order1\" cam:name=\"name\" />";
  3. XML(xml).attrNs("http://camunda.org/example", "name", "newName");
  4. SpinXmlDomAttribute attribute = XML(xml).attrNs("http://camunda.org/example", "name");

Removing Attributes from XML

It is possible to remove an attribute from the element directly or to remove the attribute itself.

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<order id=\"order1\" />";
  3. SpinXmlDomElement element = XML(xml).removeAttr("id");
  4. assertFalse(element.hasAttr("id));
  5. SpinXmlDomAttribute attribute = XML(xml).attr("id");
  6. element = attribute.remove();
  7. assertFalse(element.hasAttr("id));

You can also specify the namespace of the attribute to remove.

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<order xmlns:cam=\"http://camunda.org/example\" id=\"order1\" cam:name=\"name\" />";
  3. SpinXmlDomElement element = XML(xml).removeAttrNs("http://camunda.org/example", "name");
  4. assertFalse(element.hasAttrNs("http://camunda.org/example/", "name"));
  5. SpinXmlDomAttribute attribute = XML(xml).attrNs("http://camunda.org/example", "name");
  6. element = attribute.remove()
  7. assertFalse(element.hasAttrNs("http://camunda.org/example", "name"));

Text Content

It is possible to read and write the text content of an XML element with the textContent method.

  1. import static org.camunda.spin.Spin.XML;
  2. SpinXmlDomElement element = XML("<customer>Foo</customer>");
  3. assertEquals("Foo", element.textContent());
  4. element.textContent("Bar");

Child Elements

Append Child Elements

The method append is used to append a single or multiple child elements to an XML element.

  1. import static org.camunda.spin.Spin.XML;
  2. SpinXmlTreeElement root = XML("<root/>");
  3. SpinXmlTreeElement child2 = XML("<child/>");
  4. SpinXmlTreeElement child3 = XML("<child/>");
  5. root.append(child1, child2, child3);

To remove child elements from an XML element, the method is used. It accepts single or multiple child elements and removes them from the parent element.

  1. import static org.camunda.spin.Spin.XML;
  2. SpinXmlTreeElement root = XML("<root><child/><child/><child/></root>");
  3. root.remove(root.childElements("child"));

Replace Elements

To replace an element or a child element, the methods replace and replaceChild are used.

  1. import static org.camunda.spin.Spin.XML;
  2. SpinXmlTreeElement root = XML("<root><date/><order/></root>");
  3. SpinXmlTreeElement child1 = XML("<child/>");
  4. root.replaceChild(root.childElement("date"), child1);
  5. SpinXmlTreeElement child2 = XML("<child/>");
  6. root.childElement("order").replace(child2);

Manipulating XML using a Script Language

XML can be manipulated from script languages in the same was as from Java. Since script languages use dynamic typing, you do not need to hint the data format but you can use autodetection. The following example demonstrates how to access an attribute and a child element from XML in Python:

  1. xml = """
  2. <order id="1231">
  3. <item id="1"/>
  4. </order>
  5. """
  6. assert S(xml).hasAttr('id')
  7. order_id = S(xml).attr('id').value()
  8. assert order_id == '1231'
  9. element = S(xml).attr('order', 'order1')
  10. assert element.hasAttr('order')
  11. assert element.Attr('order').value() == 'order1'
  12. element.removeAttr('order')
  13. assert not element.hasAttr('order')
  14. item_id = S(xml).childElement('item').attr('id').value()