Java XML Document get element by id

Description

Java XML Document get element by id

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

public class Main {
   public static void main(String args[]) {
      try {/*w  w  w . j av a 2  s .co  m*/
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder parser = factory.newDocumentBuilder();
         Document doc = parser.parse("your.xml");
         Element e = doc.getElementById("data"); 
         
         System.out.println(e.getFirstChild().getNodeValue());
         
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}



PreviousNext

Related