menu


Subscribe to the blog

Social networks

My page on vk.com
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: 319

Java XML - Reading XML in a java application

lesnih.info


Russia

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: Java_XML.java)

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

text.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<company>
       <product id="1">
              <title>Mobile iphone</title>
              <color>black</color>
              <price>1000</price>
              <quantity>24</quantity>
       </product>
       <product id="2">
              <title>Mobile Samsung</title>
              <color>white</color>
              <price>995</price>
              <quantity>52</quantity>
       </product>
</company>


And now, the code for the class: 

Java_XML.java

package java_xml;

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

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


        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("\n");
                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) 
    {
        e.printStackTrace();
    }    
  }          
}

Result of program execution:

run:


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




Commets:









    Subscribe to the blog


    Random articles from the blog:


    Generator Binary code online

    Generator Binary code online
    Topic: Programming

    Tags: binary code, online resource,generator

    Views:1163

    http request and read the html page Java

    http request and read the html page Java
    Topic: Programming

    Tags: java,http,html read

    Views:132

    How to send email to JSP Java

    How to send email to JSP Java
    Topic: Programming

    Tags: java,jsp,send email,email

    Views:602