Subscribe to the blog

Social networks

My page on
My page on Facebook
My page on Google plus
My page on Twitter

Main - Blog - Java XML - Reading XML in a java application

Programming Date of creation: Views: 339

Java XML - Reading XML in a java application


Reading XML in Java

In order to read an XML document from a java application, you need to place the appropriate code that will execute this command, and also make the settings:

1. Create a project in the IDE, for example, with the name Java_XML (Automatically creates a package  java_xml, and in it the main class:

2. We place the XML file itself in a certain folder - for example, C:\NewFolder1\text.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
       <product id="1">
              <title>Mobile iphone</title>
       <product id="2">
              <title>Mobile Samsung</title>

And now, the code for the class:

package java_xml;

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 Java_XML {

    public static void main(String[] args) {
    String path = "C:\\NewFolder1\\text.xml";
        File FileXML = new File(path);
        DocumentBuilderFactory dbXML = DocumentBuilderFactory.newInstance();
        DocumentBuilder BuilderXML = dbXML.newDocumentBuilder();
        Document doc_xml = BuilderXML.parse(FileXML);

        NodeList ListXML = doc_xml.getElementsByTagName("product");

        for (int temp = 0; temp < ListXML.getLength(); temp++) 
            Node NodeXML = ListXML.item(temp);

            if (NodeXML.getNodeType() == Node.ELEMENT_NODE) 
                Element ElementXML = (Element) NodeXML;
                System.out.println("Product: " + ElementXML.getElementsByTagName("title").item(0).getTextContent());
                System.out.println("Color: " + ElementXML.getElementsByTagName("color").item(0).getTextContent());
                System.out.println("Price: " + ElementXML.getElementsByTagName("price").item(0).getTextContent());
                System.out.println("Quantity: " + ElementXML.getElementsByTagName("quantity").item(0).getTextContent() );

        catch (Exception e) 

Result of program execution:


Product: Mobile iphone
Color: black
Price: 1000
Quantity: 24 units.

Product: Mobile samsung
Color: white
Price: 995
Quantity: 52 units.
BUILD SUCCESSFUL (total time: 0 seconds)

Java XML - Reading XML in a java application
Rating: 5 | Тotal votes: 4 | Please, rate this article


    Subscribe to the blog

    Random articles from the blog:

    Installing Apache Tomcat to work in Java JSP

    Installing Apache Tomcat to work in Java JSP
    Topic: Programming

    Tags: java,jsp,сервер,apache tomcat


    Mysql Java - INSERT - add data to Mysql

    Mysql Java - INSERT - add data to Mysql
    Topic: Programming

    Tags: java,mysql,insert


    Compile file into the NetBeans IDE

    Compile file into the NetBeans IDE
    Topic: Programming

    Tags: java,netbeans,compile file