Java XML QName getOrCreateAsFirstElement(Element parentElement, QName elementQName)

Here you can find the source of getOrCreateAsFirstElement(Element parentElement, QName elementQName)

Description

get Or Create As First Element

License

Apache License

Declaration

public static Element getOrCreateAsFirstElement(Element parentElement, QName elementQName) 

Method Source Code

//package com.java2s;
/*//  w  w  w . j  ava 2s .c o  m
 * Copyright (c) 2010-2013 Evolveum
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;

import java.util.List;

import javax.xml.namespace.QName;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

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

public class Main {
    public static Element getOrCreateAsFirstElement(Element parentElement, QName elementQName) {
        Element element = getChildElement(parentElement, elementQName);
        if (element != null) {
            return element;
        }
        Document doc = parentElement.getOwnerDocument();
        element = doc.createElementNS(elementQName.getNamespaceURI(), elementQName.getLocalPart());
        parentElement.insertBefore(element, getFirstChildElement(parentElement));
        return element;
    }

    public static Element getChildElement(Element element, QName qname) {
        for (Element subelement : listChildElements(element)) {
            if (qname.equals(getQName(subelement))) {
                return subelement;
            }
        }
        return null;
    }

    public static Element getChildElement(Element element, String localPart) {
        for (Element subelement : listChildElements(element)) {
            if (subelement.getLocalName().equals(localPart)) {
                return subelement;
            }
        }
        return null;
    }

    public static Element getChildElement(Element element, int index) {
        return listChildElements(element).get(index);
    }

    public static Element getFirstChildElement(Node parent) {
        if (parent == null || parent.getChildNodes() == null) {
            return null;
        }

        NodeList nodes = parent.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) child;
            }
        }

        return null;
    }

    public static List<Element> listChildElements(Node node) {
        List<Element> subelements = new ArrayList<Element>();
        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                subelements.add((Element) childNode);
            }
        }
        return subelements;
    }

    public static QName getQName(Node node) {
        if (node.getLocalName() == null) {
            if (node.getNodeName() == null) {
                return null;
            } else {
                return new QName(null, node.getNodeName());
            }
        }
        if (node.getPrefix() == null) {
            return new QName(node.getNamespaceURI(), node.getLocalName());
        }
        return new QName(node.getNamespaceURI(), node.getLocalName(), node.getPrefix());
    }
}

Related

  1. getMatchingExtensionProvider(Map extensionMap, String namespaceURI, String prefix, String localName)
  2. getMediaTypeFromAttributes(Map attributes)
  3. getNodeQName(Node el)
  4. getNoNamespacePath(Stack stack, NamespaceContext ctx)
  5. getNSName(Element e, String qname)
  6. getPartQName(String opName, String suffix, String partName)
  7. getPort(Service service, Class seiClass, QName port)
  8. getPrefix(QName qName)
  9. getQualifiedName(QName qName)