Java XML Element to String getStringValue(Element el)

Here you can find the source of getStringValue(Element el)

Description

get String Value

License

Apache License

Declaration

static public String getStringValue(Element el) 

Method Source Code

//package com.java2s;
/*//from ww w .  j a  v a 2  s . c o m
   Copyright 2013, 2016 Nationale-Nederlanden
    
   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.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Main {
    static public String getStringValue(Element el) {
        return getStringValue(el, true);
    }

    static public String getStringValue(Element el, boolean trimWhitespace) {
        StringBuilder sb = new StringBuilder(1024);
        String str;

        NodeList nl = el.getChildNodes();
        for (int i = 0; i < nl.getLength(); ++i) {
            Node n = nl.item(i);
            if (n instanceof Text) {
                sb.append(n.getNodeValue());
            }
        }
        if (trimWhitespace) {
            str = sb.toString().trim();
        } else {
            str = sb.toString();
        }
        return str;

    }
}

Related

  1. getStringByTagName(Element element, String tag)
  2. getStringFromParagraphElement(Element element)
  3. getStringOptionsList(final Element configuration, final String optionPrefix, final String option)
  4. getStringProperty(Element properties, String name)
  5. getStringRepresentation(Element element)
  6. getStringValue(Element ele, String tagName)
  7. getStringValue(Element element, String tagName)
  8. getStringValue(Element from)
  9. getStringValue(final Element element)