The XML datatype supports querying with the XPath 1.0 query language.

Querying an Element

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";
  3. SpinXmlTreeElement child = XML(xml).xPath("/root/child").element();

Querying an Element List

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";
  3. SpinList<SpinXmlTreeElement> childs = XML(xml).xPath("/root/child/a").elementList();

Querying an Attribute

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";
  3. SpinXmlTreeAttribute attribute = XML(xml).xPath("/root/child/@id").attribute();

Querying an Attribute List

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";
  3. SpinList<SpinXmlTreeAttribute> attributes = XML(xml).xPath("/root/child/a/@id").attributeList();

Querying a String

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";
  3. String value = XML(xml).xPath("string(/root/child/@id)").string();

Querying a Number

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";
  3. Double count = XML(xml).xPath("count(/root/child/a)").number();

Querying a Boolean

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";
  3. Boolean exists = XML(xml).xPath("boolean(/root/child)").bool();

Querying with Namespaces

To use namespaces in spin with XML, you can choose one of the following methods or combine both of them.

1. Using a Single Prefix - URI Pair

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<root xmlns:t=\"http://camunda.org\"><t:child id=\"child\"><a id=\"a\"/></t:child></root>";
  3. SpinXmlTreeElement child = XML(xml).xPath("/root/t:child")
  4. .ns("t", "http://camunda.org");
  5. .element();

2. Using a Map of Prefix - URI Pairs

  1. import static org.camunda.spin.Spin.XML;
  2. String xml = "<root xmlns:t=\"http://camunda.org\"><t:child id=\"child\"><a id=\"a\"/></t:child></root>";
  3. Map<String, String> namespaceMap = new HashMap<String, String>();
  4. namespaceMap.put("t", "http://camunda.org");
  5. namespaceMap.put("s", "http://camunda.com");
  6. SpinXmlTreeElement child = XML(xml).xPath("/root/t:child")
  7. .ns(namespaceMap);
  8. .element();

If you are using xmlns="<URI>" in your XML file, spin uses DEFAULT as prefix for the namespace. E.g.,: <root xmlns="http://camunda.org"></root&gt; – prefix: DEFAULT, namespace: http://camunda.org so you need to use XML(xml).xPath("/DEFAULT:root") to fetch the correct element.

原文: https://docs.camunda.org/manual/7.9/reference/spin/xml/04-querying-xml/