Java XML Element Create createElement(Element parent, String path)

Here you can find the source of createElement(Element parent, String path)

Description

create Element

License

Open Source License

Declaration

public static Element createElement(Element parent, String path) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://www.  j ava2s .com
 *     Exadel, Inc. and Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Element createElement(Element parent, String path) {
        int i = path.indexOf('.');
        Element element = null;
        if (i < 0) {
            element = parent.getOwnerDocument().createElement(path);
            parent.appendChild(element);
        } else {
            String p = path.substring(0, i), c = path.substring(i + 1);
            Element pe = getUniqueChild(parent, p);
            if (pe == null) {
                pe = parent.getOwnerDocument().createElement(p);
                parent.appendChild(pe);
            }
            element = createElement(pe, c);
        }
        return element;
    }

    public static Element getUniqueChild(Element parent, String name) {
        return getFirstChild(parent, name);
    }

    public static Element getFirstChild(Element parent, String name) {
        NodeList nl = parent.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
                return (Element) n;
            }
        }
        return null;
    }
}

Related

  1. createElement(Document dom, String elementName, Properties attributes)
  2. createElement(Document owner, String nsURI, String elementName, String textValue)
  3. createElement(Element parent, String elementName)
  4. createElement(Element parent, String name)
  5. createElement(Element parent, String name)
  6. createElement(Element parent, String tagName)
  7. createElement(Element parent, String tagName)
  8. createElement(Element parent, String tagName, String value)
  9. createElement(final Document document, final String elementName)