Java XML Attribute Sort sortAttributes(NamedNodeMap attrs)

Here you can find the source of sortAttributes(NamedNodeMap attrs)

Description

Sorts Attributes of a given Node

License

Apache License

Parameter

Parameter Description
attrs the NamedNodeMap containing Node Attributes

Return

Array containing sorted Attributes

Declaration


protected static Attr[] sortAttributes(NamedNodeMap attrs)

    

Method Source Code

//package com.java2s;
/*//from   ww  w .  ja va2s  .c  o  m
 * Copyright 2015 www.seleniumtests.com
 * 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.Attr;

import org.w3c.dom.NamedNodeMap;

public class Main {
    /**
     * Sorts Attributes of a given Node
     *
     * @param attrs the NamedNodeMap containing Node Attributes
     * @return Array containing sorted Attributes
     */

    protected static Attr[] sortAttributes(NamedNodeMap attrs)

    {

        int len = (attrs != null) ? attrs.getLength() : 0;

        Attr array[] = new Attr[len];

        for (int i = 0; i < len; i++)

        {

            array[i] = (Attr) attrs.item(i);

        }

        for (int i = 0; i < len - 1; i++)

        {

            String name = array[i].getNodeName();

            int index = i;

            for (int j = i + 1; j < len; j++)

            {

                String curName = array[j].getNodeName();

                if (curName.compareTo(name) < 0)

                {

                    name = curName;

                    index = j;

                }

            }

            if (index != i)

            {

                Attr temp = array[i];

                array[i] = array[index];

                array[index] = temp;

            }

        }

        return (array);

    }
}

Related

  1. sortAttributeNodes(org.w3c.dom.NamedNodeMap attrs)
  2. sortAttributes(NamedNodeMap attrs)
  3. sortAttributes(org.w3c.dom.NamedNodeMap attrs)
  4. sortElementAttributes(NamedNodeMap attrs)