Java XML Element Text Set setElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue, String newValue)

Here you can find the source of setElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue, String newValue)

Description

Sets text of element by attribute value (e.g.

License

Open Source License

Declaration

public static void setElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue,
        String newValue) 

Method Source Code

//package com.java2s;
/**// w  w  w.ja va2s.c o  m
 * Jafer Poject.
 * Copyright (C) 2005, Jafer Project, Oxford University.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

import org.w3c.dom.Attr;

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

public class Main {
    /**
     * Sets text of element by attribute value (e.g. <identifier type="issn">xxx
     * </identifier>).
     */
    public static void setElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue,
            String newValue) {
        NodeList list = modsroot.getElementsByTagName(nodename);
        for (int i = 0; i < list.getLength(); i++) {
            Node node = (Node) list.item(i);
            NamedNodeMap attrs = node.getAttributes();
            Attr attr = (Attr) attrs.getNamedItem(attrname);
            if (attr != null) {
                if (attr.getValue().equals(attrvalue)) {
                    setTextValue(node, newValue);
                    break;
                }
            }
        }
    }

    /**
     * set the text node from an element node.
     */
    public static void setTextValue(Node node, String newValue) {
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i).getNodeType() == Node.TEXT_NODE) {
                list.item(i).setNodeValue(newValue);
            }
        }
    }
}

Related

  1. setElementText(Element element, String value)
  2. setElementText(Element elm, String text)
  3. setElementText(Element elm, String text)
  4. setElementText(Element elm, String text)
  5. setElementText(Node elem, Object text)
  6. setElementTextValue(Element e, String text)
  7. setElementValue(Element element, String val)
  8. setElementValue(Element element, String value)