Use XML in Java to Write Save Documents

An explanation on how to use xml in java to write and save documents.

This java program will help you to read xml value from xml file.

Also you have to download two jar file:
  1 xerces_1_0_3.jar
  2 xmlparserv2.jar
and set class path for above jar files after compile the below java file
import org.w3c.dom.Document;
import org.w3c.dom.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
  public class ReadAndPrintXMLFile{
public static void main(String arg[])
 Vector vcrs = null;
try {
  vcrs = null ;
vcrs = new Vector() ;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("interest.xml"));
  // normalize text representation
doc.getDocumentElement ().normalize ();
  NodeList listOfPersons = doc.getElementsByTagName("interest_master");
int totalPersons = listOfPersons.getLength();
  for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
NodeList firstNameList = firstPersonElement.getElementsByTagName("interest_id");
Element firstNameElement = (Element)firstNameList.item(0);
  NodeList textFNList = firstNameElement.getChildNodes(); System.out.println("First Name : " + ((Node)textFNList.item(0)).getNodeValue().trim());
String id=((Node)textFNList.item(0)).getNodeValue().trim();
NodeList lastNameList = firstPersonElement.getElementsByTagName("interest_name");
Element lastNameElement = (Element)lastNameList.item(0);
  NodeList textLNList = lastNameElement.getChildNodes(); System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
String name=((Node)textFNList.item(0)).getNodeValue().trim();
}//end of if clause
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.out.println(" " + err.getMessage ());
  }catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
  }catch (Throwable t) {
t.printStackTrace ();
//System.exit (0);
System.out.println("size of the vector is " + vcrs.size()) ;
}//end of  class ===========================end==================

Below is xml file from which we will read value.
  <?xml version="1.0"?>

Tips by : Mukesh Kamal

Have a Java Problems
Do you have a Java Question?

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Best Regards,
Java Programming Hints and Tips

All the site contents are Copyright © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of or the content authors.