Java XML Element Get by Name getElement(Document doc, String tagName, int index)

Here you can find the source of getElement(Document doc, String tagName, int index)

Description

Return an Element given an XML document, tag name, and index

License

Open Source License

Parameter

Parameter Description
doc XML docuemnt
tagName a tag name
index a index

Return

an Element

Declaration

public static Element getElement(Document doc, String tagName, int index) 

Method Source Code

//package com.java2s;
/********************************************************************
 * Copyright (c) 1999 The Bean Factory, LLC.
 * All Rights Reserved.//from  w  w  w  .  j a v  a  2 s  . c o m
 *
 * The Bean Factory, LLC. makes no representations or
 * warranties about the suitability of the software, either express
 * or implied, including but not limited to the implied warranties of
 * merchantableness, fitness for a particular purpose, or
 * non-infringement. The Bean Factory, LLC. shall not be
 * liable for any damages suffered by licensee as a result of using,
 * modifying or distributing this software or its derivatives.
 *
 *******************************************************************/

import org.w3c.dom.*;

public class Main {
    /**
     Return an Element given an XML document, tag name, and index
        
     @param     doc     XML docuemnt
     @param     tagName a tag name
     @param     index   a index
     @return    an Element
     */
    public static Element getElement(Document doc, String tagName, int index) {
        //given an XML document and a tag
        //return an Element at a given index
        NodeList rows = doc.getDocumentElement().getElementsByTagName(tagName);
        return (Element) rows.item(index);
    }
}

Related

  1. getElement(Document doc, String tagName, int index)
  2. getElement(Document document, Element parent, String element)
  3. getElement(Document document, String elementName)
  4. getElement(Document owner, String elementName, String nsURI)