public static void addNewNode(final Document doc, final XmlEntity xmlEntity)
      throws IOException, XPathExpressionException, SAXException, ParserConfigurationException {
    // Build up map per call to avoid issues with caching wrong version of the map.
    final LinkedHashMap<String, CacheElement> elementOrderMap = CacheElement.buildElementMap(doc);

    final Node newNode = createNode(doc, xmlEntity.getXmlDefinition());
    final Node root = doc.getDocumentElement();
    final int incomingElementOrder =
        getElementOrder(elementOrderMap, xmlEntity.getNamespace(), xmlEntity.getType());

    boolean nodeAdded = false;
    NodeList nodes = root.getChildNodes();
    final int length = nodes.getLength();
    for (int i = 0; i < length; i++) {
      final Node node = nodes.item(i);

      if (node instanceof Element) {
        final Element childElement = (Element) node;
        final String type = childElement.getLocalName();
        final String namespace = childElement.getNamespaceURI();

        if (namespace.equals(xmlEntity.getNamespace()) && type.equals(xmlEntity.getType())) {
          // First check if the element has a name
          String nameOrId = getAttribute(childElement, "name");
          // If not then check if the element has an Id
          if (nameOrId == null) {
            nameOrId = getAttribute(childElement, "id");
          }

          if (nameOrId != null) {
            // If there is a match , then replace the existing node with the incoming node
            if (nameOrId.equals(xmlEntity.getNameOrId())) {
              root.replaceChild(newNode, node);
              nodeAdded = true;
              break;
            }
          } else {
            // This element does not have any name or id identifier for e.g PDX and gateway-receiver
            // If there is only one element of that type then replace it with the incoming node
            if (!isMultiple(elementOrderMap, namespace, type)) {
              root.replaceChild(newNode, node);
              nodeAdded = true;
              break;
            }
          }
        } else {
          if (incomingElementOrder < getElementOrder(elementOrderMap, namespace, type)) {
            root.insertBefore(newNode, node);
            nodeAdded = true;
            break;
          }
        }
      }
    }
    if (!nodeAdded) {
      root.appendChild(newNode);
    }
  }
**
Adds a new node or replaces an existing node in the Document

@param doc Target document where the node will added
@param xmlEntity contains definition of the xml entity
geode-core/src/main/java/org/apache/geode/management/internal/configuration/utils/XmlUtils.java