Java XML Attribute Read readAttributeWithPrefix(Node node, String attributePrefix, String defaultValue)

Here you can find the source of readAttributeWithPrefix(Node node, String attributePrefix, String defaultValue)

Description

Reads the value of the first attribute which name matches with the specified attributePrefix.

License

Apache License

Parameter

Parameter Description
node node to look for attributes.
attributePrefix attribute prefix.
defaultValue default returned value.

Return

the value found or default.

Declaration

public static String readAttributeWithPrefix(Node node, String attributePrefix, String defaultValue) 

Method Source Code


//package com.java2s;
/*//w  ww.ja  va  2 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 org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**
     * Reads the value of the first <i>attribute</i> which name matches with the specified <code>attributePrefix</code>.
     * Returns the <code>defaultValue</code> if not found.
     *
     * @param node node to look for attributes.
     * @param attributePrefix attribute prefix.
     * @param defaultValue default returned value.
     * @return the value found or default.
     */
    public static String readAttributeWithPrefix(Node node, String attributePrefix, String defaultValue) {
        final NamedNodeMap attributes = node.getAttributes();
        if (null == attributes) {
            return defaultValue;
        }
        Node attribute;
        for (int a = 0; a < attributes.getLength(); a++) {
            attribute = attributes.item(a);
            if (attribute.getNodeName().startsWith(attributePrefix)) {
                return attribute.getNodeValue();
            }
        }
        return defaultValue;
    }
}

Related

  1. getNullSafe(NamedNodeMap attr, String key)
  2. getOptionalAttribute(Element elt, String name)
  3. getOptionalAttributeValue(NamedNodeMap attrs, String name)
  4. readAttribute(Node element, String attributeName)
  5. readAttribute(Node node, String attribute, String defaultValue)
  6. readBoolAttr(Element element, String attributeName)
  7. readBooleanAttribute(Element elem, String name, boolean defaultValue)
  8. readBooleanAttributeElement(final XMLStreamReader reader, final String attributeName)
  9. readFloat(Node node, String attributeName, float def)