This section is going to tell you how to access a XML file using Java Code.
In this example we have provided you a simple java example with the source
code that will make it possible to access the XML file through Java. For that we have used DOM parser.
DOM parser that loads the XML file into the memory and makes an object model which can be
traversed to get the file elements. For this, we have created two different files:
1) MyFile.xml
2) AccessingXmlFile.java
Source Code for accessing XML file through Java
File MyFile.xml
| <?xml version="1.0"?> <student> <student-name> <firstname>Anusmita</firstname> <lastname>Singh</lastname> </student-name> <student-address> <address>Rohini</address> <city>Delhi</city> </student-address> </student> |
Here is the code of AccessingXmlFile.java
import java.io.File;
|
In the above example, the method DocumentBuilderFactory.newInstance() enables applications to obtain a parser that produces DOM. The DocumentBuilder provides the DOM document instances from XML document. The Document refers to the HTML or XML document. The getDocumentElement() method provides the XML root Element. The getElementsByTag Name() provides the tag. Then get the value of node by getNodeValue().
Following output will be displayed on the console:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Accessing XML file from Java View All Comments
Post your Comment