Java XML Attribute Parse parseElementAttributes(Element element)

Here you can find the source of parseElementAttributes(Element element)

Description

Aggregates all attributes from the given element, formats then into the proper key="value" XML format and returns them as one String

License

Open Source License

Parameter

Parameter Description
element a parameter

Return

The formatted String or null if the element has no attributes

Declaration

private static String parseElementAttributes(Element element) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w  ww  . j  a v a 2  s  .  c  om*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**
     * Aggregates all attributes from the given element, formats then into the
     * proper key="value" XML format and returns them as one String
     * 
     * @param element
     * @return The formatted String or null if the element has no attributes
     */
    private static String parseElementAttributes(Element element) {
        // Verify we have attributes
        if (element.hasAttributes() == false) {
            return null;
        }
        // Create the buffer
        StringBuffer buffer = new StringBuffer();
        // Get the attributes
        NamedNodeMap attributeMap = element.getAttributes();
        // Accumulate all attributes
        for (int i = 0; i < attributeMap.getLength(); i++) {
            Node node = attributeMap.item(i);
            if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                Attr attribute = (Attr) node;
                // Append space before attribute
                buffer.append(' ');
                // Append attribute name
                buffer.append(attribute.getName());
                // Append =
                buffer.append('=');
                // Append quote
                buffer.append('"');
                // Append attribute value
                buffer.append(attribute.getValue());
                // Append quote
                buffer.append('"');
            }
        }

        return buffer.toString();
    }
}

Related

  1. mapOptionalBeanRefAttributes(Element element, BeanDefinitionBuilder builder, ParserContext parserContext, String... attrs)
  2. mapRequiredBeanRefAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String... attrs)
  3. parseAttributeValuePairTags(Node parentNode)
  4. parseBoolean(String xmlAttributeValue, Boolean invalidValue)
  5. parseConfigAttr(NamedNodeMap attributes)
  6. parseFloat(Node xmlAttribute, float invalidValue)
  7. parseFloat(String attributeName, NamedNodeMap map)
  8. parseProtoypes(Node module, Node iFace, PrintWriter out, boolean onlyAttributes)
  9. parseString(final Element parent, final String attrName)