Wednesday, April 3

Reading Xml File using Java Program

Hi All,


Step1: Create a xml file as the xml Standard.

<?xml version="1.0" encoding="UTF-8"?>
<stocks>
       <stock>
              <symbol>Citibank</symbol>
              <price>100</price>
              <quantity>1000</quantity>
       </stock>
       <stock>
              <symbol>Axis bank</symbol>
              <price>90</price>
              <quantity>2000</quantity>
       </stock>
          <stock>
              <symbol>Indian bank</symbol>
              <price>695</price>
              <quantity>1200</quantity>
       </stock>
          <stock>
              <symbol>Canara bank</symbol>
              <price>370</price>
              <quantity>1000</quantity>
       </stock>
          <stock>
              <symbol>Icici    bank</symbol>
              <price>180</price>
              <quantity>3000</quantity>
       </stock>
          <stock>
              <symbol>State bank</symbol>
              <price>990</price>
              <quantity>200</quantity>
       </stock>
</stocks>


Step 2: Save the file as stock.xml (user dependent format).

Java program for read above xml file in console:

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

public class xmlRead {

    public static void main(String args[]) {
        try {

            File stocks = new File("C:\\Documents and Settings\\Sify\\Desktop\\stock.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(stocks);
            doc.getDocumentElement().normalize();

            System.out.println("XML File " + doc.getDocumentElement().getNodeName());
            NodeList nodes = doc.getElementsByTagName("stock");
            System.out.println("==========================");

            for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                System.out.println("Stock Name: " + getValue("symbol", element));
                System.out.println("Stock Price: " + getValue("price", element));
                System.out.println("Stock Quantity: " + getValue("quantity", element));
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        }

    private static String getValue(String tag, Element element) {
        NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
        Node node = (Node) nodes.item(0);
        return node.getNodeValue();
    }
}







output:

XML File stocks
==========================
Stock Name: Citibank
Stock Price: 100
Stock Quantity: 1000
Stock Name: Axis bank
Stock Price: 90
Stock Quantity: 2000
Stock Name: Indian bank
Stock Price: 695
Stock Quantity: 1200
Stock Name: Canara bank
Stock Price: 370
Stock Quantity: 1000
Stock Name: Icici    bank
Stock Price: 180
Stock Quantity: 3000
Stock Name: State bank
Stock Price: 990
Stock Quantity: 200
BUILD SUCCESSFUL (total time: 0 seconds)


No comments:

Post a Comment

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...