public static Element getUniqueDirectChild(Element parent, String namespace, String tag)
         throws MalformedXmlException {
      NodeList children = parent.getElementsByTagNameNS(namespace, tag);
      if (children.getLength() == 0) {
         throw new MalformedXmlException("Element " + tag + " is missing under " + parent.getTagName());
      }
      for (int i = 0; i < children.getLength(); i++) {
         Node child = children.item(i);
         if (child.getNodeType() == Node.ELEMENT_NODE && child.getParentNode() == parent) {
            return (Element) child;
         }
      }
      throw new MalformedXmlException("Element " + tag + " was expected directly under " + parent.getTagName());
   }
@Test
   void getUniqueDirectChild() {
      assertThrows(MalformedXmlException.class, () -> {
         XmlUtil.getUniqueDirectChild(parent, "http://example.com", "child1");
      });

      Element uniqueChild = document.createElementNS("http://example.com", "uniqueChild");
      parent.appendChild(uniqueChild);

      try {
         Element retrievedChild = XmlUtil.getUniqueDirectChild(parent, "http://example.com", "uniqueChild");
         assertEquals("uniqueChild", retrievedChild.getLocalName());
      } catch (MalformedXmlException e) {
         fail("Exception should not have been thrown");
      }
   }