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
================start=============================
import java.io.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();
vcrs.add(id);
//-------
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();
vcrs.add(name);
//----
  //------

}//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.
========================start================

  <?xml version="1.0"?>
<interest>
 <interest_master>
  <interest_id>1</interest_id>
  <interest_name>Economics</interest_name>
 </interest_master>
   <interest_master>
  <interest_id>2</interest_id>
  <interest_name>Sports</interest_name>
 </interest_master>

 <interest_master>
  <interest_id>3</interest_id>
  <interest_name>Medical</interest_name>
 </interest_master>
   <interest_master>
  <interest_id>4</interest_id>
  <interest_name>Music</interest_name>
 </interest_master>
   <interest_master>
  <interest_id>5</interest_id>
  <interest_name>Mytest</interest_name>
 </interest_master>
</interest>
======================end==================

Tips by : Mukesh Kamal

Related:

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

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com 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
www.erpgreat.com or the content authors.