Java XML Element Get by ID getElementById(Document dom, String id)

Here you can find the source of getElementById(Document dom, String id)

Description

get Element By Id

License

Open Source License

Declaration

public static Element getElementById(Document dom, String id) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 IBM Corporation and others.
 * 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:/* w  ww  .jav a2  s. c o m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static Element getElementById(Document dom, String id, String localElementName) {

        NodeList children = dom.getElementsByTagNameNS("*", localElementName); //$NON-NLS-1$
        for (int i = 0; i < children.getLength(); i++) {
            Element element = (Element) children.item(i);
            if (element.getAttribute("id").equals(id)) //$NON-NLS-1$
                return element;
        }
        // non found.
        return null;

    }

    public static Element getElementById(Document dom, String id) {
        return getElementById(dom, id, "*"); //$NON-NLS-1$
    }
}

Related

  1. getElemenById(Document document, String elementId)
  2. getElementById(Document doc, String id, Map idMap)
  3. getElementById(Document document, String id)
  4. getElementById(Document dom, String id, String localElementName)