Java XML Element Get getElement(Document doc, QName elementQName)

Here you can find the source of getElement(Document doc, QName elementQName)

Description

Get an element from the document given its QName

First an attempt to get the element based on its namespace is made, failing which an element with the localpart ignoring any namespace is returned.

License

Apache License

Parameter

Parameter Description
doc a parameter
elementQName a parameter

Declaration

public static Element getElement(Document doc, QName elementQName) 

Method Source Code

//package com.java2s;
/*/*from  w w  w  . j a  v  a  2 s.c  o m*/
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * 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 org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;
import javax.xml.namespace.QName;

public class Main {
    /**
     * <p> Get an element from the document given its {@link QName} </p> <p> First an attempt to get the element based
     * on its namespace is made, failing which an element with the localpart ignoring any namespace is returned. </p>
     *
     * @param doc
     * @param elementQName
     *
     * @return
     */
    public static Element getElement(Document doc, QName elementQName) {
        NodeList nl = doc.getElementsByTagNameNS(elementQName.getNamespaceURI(), elementQName.getLocalPart());
        if (nl.getLength() == 0) {
            nl = doc.getElementsByTagNameNS("*", elementQName.getLocalPart());
            if (nl.getLength() == 0)
                nl = doc.getElementsByTagName(elementQName.getPrefix() + ":" + elementQName.getLocalPart());
            if (nl.getLength() == 0)
                return null;
        }
        return (Element) nl.item(0);
    }
}

Related

  1. findElement(String name, Document doc)
  2. findElementAndSetElseCreateAndSet(Document document, Element parent, String child, boolean value)
  3. findElementElseCreateAndSet(Document document, Element parent, String child, boolean value)
  4. findElementList(String name, String attrName, String attrValue, Document doc)
  5. findElementOrContainer(Document document, Element parent, String element)
  6. getElement(Document doc, String path)
  7. getElement(Document pDocument, String psTagName, int index)
  8. getElement(Element config, String elementName)
  9. getElement(Element config, String elementName)