Java XML Attribute from Element getTrimedAttribute(Element elem, String attr_name)

Here you can find the source of getTrimedAttribute(Element elem, String attr_name)

Description

get Trimed Attribute

License

Open Source License

Declaration

static String getTrimedAttribute(Element elem, String attr_name) 

Method Source Code

//package com.java2s;
/*  it under the terms of the GNU General Public License as published by    */

import org.w3c.dom.Element;

public class Main {
    static String getTrimedAttribute(Element elem, String attr_name) {
        return trimOuterWhitespace(elem.getAttribute(attr_name));
    }/* w w w  .  ja va2s  . com*/

    static String trimOuterWhitespace(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }

        String ret = trimLeadingWhitespace(str);
        ret = trimTrailingWhitespace(ret);

        return ret;
    }

    static String trimLeadingWhitespace(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }

        StringBuffer buf = new StringBuffer(str);
        for (;;) {
            if (buf.length() == 0
                    || Character.isWhitespace(buf.charAt(0)) == false) {
                break;
            }
            buf.deleteCharAt(0);
        }

        return buf.toString();
    }

    static String trimTrailingWhitespace(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }

        StringBuffer buf = new StringBuffer(str);
        for (;;) {
            int len = buf.length();
            if (len == 0
                    || Character.isWhitespace(buf.charAt(len - 1)) == false) {
                break;
            }
            buf.deleteCharAt(len - 1);
        }

        return buf.toString();
    }
}

Related

  1. getTagAttribute(String sTag, String sAtt, Element eElement)
  2. getTagAttribute(XMLStreamReader xmler, String attribute, String defaultValue)
  3. getTagAttributeRecursive(String sTag, String sAtt, Element eElement)
  4. getTagAttributes(Element element)
  5. getThisClassTypeAttr(Element methodNode)
  6. getValue(final Element elem, final String attrName)
  7. getValueAttribute(final Node aNode)
  8. getValueAttributeUri(Element parent, String defaultBaseUri)
  9. getValueForAttribute(String attributeName, Node parentNode)