Here you can find the source of searchParentNamespaces(Node xml, String attribute)
Parameter | Description |
---|---|
xml | The Node to start the search from. The search will move up through the parents of this Node. |
attribute | The name of the XML namespace attribute to search for. This should be <em>xmlns:prefix</em> or <em>targetNamespace</em>. |
private static String searchParentNamespaces(Node xml, String attribute)
//package com.java2s; /* /*ww w . j a v a2 s . com*/ * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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 javax.xml.namespace.QName; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** * * Traverses up the XML Document tree looking for XML an namespace * declaration that matches the given attribute. * * @param xml * The Node to start the search from. The search will move up * through the parents of this Node. * * @param attribute * The name of the XML namespace attribute to search for. This * should be <em>xmlns:prefix</em> or <em>targetNamespace</em>. * * @return The first value found for the given attribute. The method * returns null if the attribute is not found in the given Node * or its parents. * */ private static String searchParentNamespaces(Node xml, String attribute) { Node next = xml; String uri = null; // // go up the tree until we find a matching prefix/URI (or hit the top) // while (next != null && (uri == null || uri.length() == 0)) { if (next.getNodeType() == Node.ELEMENT_NODE) uri = ((Element) next).getAttribute(attribute); next = next.getParentNode(); } // // DOM returns empty strings for non-existent attributes - we want null // if (uri != null && uri.length() == 0) uri = null; return uri; } public static String getAttribute(Element xml, QName qname) { String uri = qname.getNamespaceURI(); String name = qname.getLocalPart(); String value = xml.getAttributeNS(uri, name); if (value != null && value.length() == 0) return null; return value; } }