Here you can find the source of getElement(Document doc, QName elementQName)
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.
Parameter | Description |
---|---|
doc | a parameter |
elementQName | a parameter |
public static Element getElement(Document doc, QName elementQName)
//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); } }