Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best/simplest way to read in an XML file in Java application? [closed]

Tags:

java

file

xml

Currently our Java application uses the values held within a tab delimited *.cfg file. We need to change this application so that it now uses an XML file.

What is the best/simplest library to use in order to read in values from this file?

like image 268
rmcc Avatar asked Jan 09 '09 13:01

rmcc


People also ask

What is the best way to parse XML in Java?

Java XML Parser - DOM DOM Parser is the easiest java xml parser to learn. DOM parser loads the XML file into memory and we can traverse it node by node to parse the XML. DOM Parser is good for small files but when file size increases it performs slow and consumes more memory.

What is the best way to view an XML file?

View an XML file in a browser If all you need to do is view the data in an XML file, you're in luck. Just about every browser can open an XML file. In Chrome, just open a new tab and drag the XML file over. Alternatively, right click on the XML file and hover over "Open with" then click "Chrome".

Which XML parser is fastest Java?

The design is inspired by the design of VTD-XML, the fastest XML parser for Java I have seen, being even faster than the StAX and SAX Java standard XML parsers.


2 Answers

There are of course a lot of good solutions based on what you need. If it is just configuration, you should have a look at Jakarta commons-configuration and commons-digester.

You could always use the standard JDK method of getting a document :

import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document;  [...]  File file = new File("some/path"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(file); 
like image 109
Guillaume Avatar answered Sep 29 '22 04:09

Guillaume


XML Code:

<?xml version="1.0"?> <company>     <staff id="1001">         <firstname>yong</firstname>         <lastname>mook kim</lastname>         <nickname>mkyong</nickname>         <salary>100000</salary>     </staff>     <staff id="2001">         <firstname>low</firstname>         <lastname>yin fong</lastname>         <nickname>fong fong</nickname>         <salary>200000</salary>     </staff> </company> 

Java Code:

import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File;  public class ReadXMLFile {    public static void main(String argv[]) {     try {     File fXmlFile = new File("/Users/mkyong/staff.xml");     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();     Document doc = dBuilder.parse(fXmlFile);     doc.getDocumentElement().normalize();      System.out.println("Root element :" + doc.getDocumentElement().getNodeName());     NodeList nList = doc.getElementsByTagName("staff");     System.out.println("----------------------------");      for (int temp = 0; temp < nList.getLength(); temp++) {         Node nNode = nList.item(temp);         System.out.println("\nCurrent Element :" + nNode.getNodeName());         if (nNode.getNodeType() == Node.ELEMENT_NODE) {             Element eElement = (Element) nNode;             System.out.println("Staff id : "                                + eElement.getAttribute("id"));             System.out.println("First Name : "                                + eElement.getElementsByTagName("firstname")                                  .item(0).getTextContent());             System.out.println("Last Name : "                                + eElement.getElementsByTagName("lastname")                                  .item(0).getTextContent());             System.out.println("Nick Name : "                                + eElement.getElementsByTagName("nickname")                                  .item(0).getTextContent());             System.out.println("Salary : "                                + eElement.getElementsByTagName("salary")                                  .item(0).getTextContent());         }     }     } catch (Exception e) {     e.printStackTrace();     }   } } 

Output:

----------------  Root element :company ----------------------------  Current Element :staff Staff id : 1001 First Name : yong Last Name : mook kim Nick Name : mkyong Salary : 100000  Current Element :staff Staff id : 2001 First Name : low Last Name : yin fong Nick Name : fong fong Salary : 200000 

I recommended you reading this: Normalization in DOM parsing with java - how does it work?

Example source.

like image 36
Ran Adler Avatar answered Sep 29 '22 05:09

Ran Adler