hello can anybody help me with this issue i have java code to print information that exists in XML file i will paste it here, so i want to print the information based on user input , as example the user enter the Id number then the program should print all information relative to that Id
so my java code is :

package parse;

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

    public static void main(String[] args) {
            File inputFile = new File("C:\\Users\\Abdal\\Documents\\NetBeansProjects\\Creation\\user_info.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            // this class is to initialize the usage and dealing of the XML file.
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(inputFile);
            // parse method actually parses (understands) the XML file, the whole sturucture 
            // of the XML file is copied inside a Document object, inour case doc.
            // doc becomes a tree (to be traversed)
            // normalization for tags, the same tags are (grouped) in one group.

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

            NodeList nList = doc.getElementsByTagName("user");
            //NodeList is data structure that used to create a list nList
            //nList will contain a list of student tags.
            //nList is a list of nodes, each node has its own elements and data
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);

                // load the node with number temp
                System.out.println("\nCurrent Element :" + nNode.getNodeName());
                System.out.println("Parent Node is:"+ nNode.getParentNode().getNodeName());

                System.out.println("Element type=" + nNode.getNodeType());

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    //getNodeType() returns the type of the node.
                    // Node is a class, and ELEMENT_NODE is is a constant memebr variable, its value is 1
                     Element eElement = (Element) nNode;
                     // casting from nNode to Element object
                     // Element class has extra methods related to XML functions
                     System.out.println("Id  : " + eElement.getAttribute("id"));
                     System.out.println("Username : " + eElement.getElementsByTagName("username").item(0).getTextContent());
                     System.out.println("Password : " + eElement.getElementsByTagName("password").item(0).getTextContent());
                    System.out.println("Address : " + eElement.getElementsByTagName("address").item(0).getTextContent());
                    //System.out.println("Marks : " + eElement.getElementsByTagName("marks").item(0).getTextContent());

                    //System.out.println("Address : " + eElement.getElementsByTagName("address").item(0).getTextContent());


        }catch(Exception e){



the XML file structure is :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<user id="1">

also i will paste here my XML file creation code :

package creation;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Creation {

    public static void main(String[] args) throws SAXException, IOException, TransformerConfigurationException, TransformerException {
         try {
            String xmlFilePath = "user_info.xml";

            DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
            Document document = documentBuilder.newDocument();

            Element root = document.createElement("users");
            // user element
            Element user = document.createElement("user");
            Attr attr = document.createAttribute("id");


            Element username = document.createElement("username");


            Element password = document.createElement("password");

            Element address = document.createElement("address");

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource domSource = new DOMSource(document);
            StreamResult streamResult = new StreamResult(new File(xmlFilePath));

            // If you use
            // StreamResult result = new StreamResult(System.out);
            // the output will be pushed to the standard output ...
            // You can use that for debugging 

            transformer.transform(domSource, streamResult);

            System.out.println("Done creating XML File");


        catch (ParserConfigurationException pce) {



At line 48 you have eElement so you can use that to test for the ID (or whatever) you are interested in and print or not print accordingly.

commented: #JamesCherill thank you very much bro , Done +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.