java xml reader

Solutions on MaxInterview for java xml reader by the best coders in the world

showing results for - "java xml reader"
Adriana
11 May 2018
1String dir = System.getProperty("user.dir");
2File inputFile = new File(dir + File.separator + "dats.xml");
3DocumentBuilderFactory dbFactory =
4	DocumentBuilderFactory.newInstance();
5DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
6Document doc = dBuilder.parse(inputFile);
7doc.getDocumentElement().normalize();
8/*
9Elimina nodos vacíos y combina adyacentes en caso de que los
10hubiera
11*/
Leia
05 Jan 2021
1
2package com.mkyong.seo;
3
4import javax.xml.parsers.DocumentBuilderFactory;
5import javax.xml.parsers.DocumentBuilder;
6import org.w3c.dom.Document;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Node;
9import org.w3c.dom.Element;
10import java.io.File;
11
12public class ReadXMLFile {
13
14  public static void main(String argv[]) {
15
16    try {
17
18    File fXmlFile = new File("/Users/mkyong/staff.xml");
19    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
20    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
21    Document doc = dBuilder.parse(fXmlFile);
22            
23    //optional, but recommended
24    //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
25    doc.getDocumentElement().normalize();
26
27    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
28            
29    NodeList nList = doc.getElementsByTagName("staff");
30            
31    System.out.println("----------------------------");
32
33    for (int temp = 0; temp < nList.getLength(); temp++) {
34
35        Node nNode = nList.item(temp);
36                
37        System.out.println("\nCurrent Element :" + nNode.getNodeName());
38                
39        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
40
41            Element eElement = (Element) nNode;
42
43            System.out.println("Staff id : " + eElement.getAttribute("id"));
44            System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
45            System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
46            System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
47            System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
48
49        }
50    }
51    } catch (Exception e) {
52    e.printStackTrace();
53    }
54  }
55
56}
57
queries leading to this page
reading xml files javajava gui xml readerxml file reader javajava from xmljava xml readingjava file that read xmlhow to read from xml file javaread xml file javahow to read xml request in javahow to read xml files javahow to retrieve from xml file in javareading xml file in javareader xmlreading xml in javajava how to read xml filethrough java how to read xml filejava read xml file to objecthow read xml file in javaread xml files in javaread xml document in javajava file reader xmljava read xml file with xsdhow to read an xml file in javajava read from xml fileread xml objects javaread xml file using java seingread xml from file and set to vo javaread xml properties file javahow to make java read a xml filejava read xmlread from xml file in javaread xml from file javaread xml file as a string in javaget xml document from a file javaread xml java examplethe best to read xml file javaread xml as a string in javaread xml javahow to read from xml file java exemplejava reading xml file fastest wayjava load xml from fileread a xml in javareading xml string in javajava program to read xml filehow to read xml data in javajava read xmlfilehow to read a xml filein javahow to read xml using javajava code to read xml file with attributesread an xml in javaparse xml in javahow to read by xml javahow to open a xml file in java as a stringread xml file in javaread xml data in javareading an xml file in javajava xml readerreading xml file as string in javahow to read xml javaxml reader javajava file readergetting a xml file in javaeasy xml reader javareading xml as a string javajava xml string readerxml reader java built inxml readertjava load xml filehow to read xml file in javabest way to read xml file in java xml readerxml readerread xml file in java and access propertiesread xml to javaopen xml file javajava reading xml fileread xml dom javahow to read from xml file in javaxml parserjava read xml fileread xml as string javareading from xml file javajava read xml easyread an xml file with javaparse xml file javaread xml file data using reflection javajava simple read xmlread xml in javajava xml reader