Add new attribute to XML or remove the element from parent - Android XML

Android examples for XML:XML Attribute

Description

Add new attribute to XML or remove the element from parent

Demo Code

/*******************************************************************************
 * Copyright (c) 2014 Jens Reimann.//from  w  w  w  . ja  v a2 s.  c  om
 * 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:
 *     Jens Reimann - initial API and implementation
 *******************************************************************************/
//package com.java2s;

import org.w3c.dom.Element;

public class Main {
    public static void fixSize(final Element element) {
        final int len = element.getChildNodes().getLength();

        if (len > 0) {
            element.setAttribute("size", "" + len);
        } else {
            element.getParentNode().removeChild(element);
        }
    }
}

Related Tutorials