Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Demo Example

<?xml version = "1.0"?>
<class>
   <student rollno = "393">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks>85</marks>
   </student>
   
   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>vinni</nickname>
      <marks>95</marks>
   </student>
   
   <student rollno = "593">
      <firstname>jasvir</firstname>
      <lastname>singn</lastname>
      <nickname>jazz</nickname>
      <marks>90</marks>
   </student>
</class>
Comment

Demo Example

package com.tutorialspoint.xml;

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class DomParserDemo {

   public static void main(String[] args) {

      try {
         File inputFile = new File("input.txt");
         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         Document doc = dBuilder.parse(inputFile);
         doc.getDocumentElement().normalize();
         System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
         NodeList nList = doc.getElementsByTagName("student");
         System.out.println("----------------------------");
         
         for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            System.out.println("
Current Element :" + nNode.getNodeName());
            
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
               Element eElement = (Element) nNode;
               System.out.println("Student roll no : " 
                  + eElement.getAttribute("rollno"));
               System.out.println("First Name : " 
                  + eElement
                  .getElementsByTagName("firstname")
                  .item(0)
                  .getTextContent());
               System.out.println("Last Name : " 
                  + eElement
                  .getElementsByTagName("lastname")
                  .item(0)
                  .getTextContent());
               System.out.println("Nick Name : " 
                  + eElement
                  .getElementsByTagName("nickname")
                  .item(0)
                  .getTextContent());
               System.out.println("Marks : " 
                  + eElement
                  .getElementsByTagName("marks")
                  .item(0)
                  .getTextContent());
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
Comment

Demo Example

Root element :class
----------------------------

Current Element :student
Student roll no : 393
First Name : dinkar
Last Name : kad
Nick Name : dinkar
Marks : 85

Current Element :student
Student roll no : 493
First Name : Vaneet
Last Name : Gupta
Nick Name : vinni
Marks : 95

Current Element :student
Student roll no : 593
First Name : jasvir
Last Name : singn
Nick Name : jazz
Marks : 90
Comment

PREVIOUS NEXT
Code Example
Java :: how to create arraylist in java 
Java :: Remove ArrayList Elements using remove() function 
Java :: jframe circles in java 
Java :: prime numbers most efficient algorithm java 
Java :: okhttp Updating Views on UIThread 
Java :: android stop audio playing by activity lifecycle 
Java :: java awt bild einfügen 
Java :: bloomreach clone session 
Java :: jframe centerlaized 
Java :: transform primitive float array to float array 
Java :: unique numbers in array java 
Java :: how to split string with dolor sign in java 
Java :: regex pattern to mathc IP address 
Java :: .throwFor in java 
Java :: konva crop outside width and height of image 
Java :: java catch stop signal 
Java :: como limitar o random em java 
Java :: print jtable in java 
Java :: java -jar -l resourceses.porperties -i ejemplo.txt -o inject.bin 
Java :: jwt token in android studio 
Java :: signed and unsigned data types in java 
Java :: Java Method Overloading by changing the data type of parameters 
Java :: how to add 0 in left padding using regular expression java 
Java :: Hibernate/JPA criteria query 
Java :: how to see page is open in selenium 
Java :: Not allowed to bind to service Intent 
Java :: @expose(serialize = false) not working 
Java :: find center of screen graphic g 
Java :: how to create gravity in Java 
Java :: Java Access Specifier in Overriding 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =