Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Java Forum & Tutorial


Use XML in Java to Write Save Documents


Reply
Views: 2394  
Thread Tools Rate Thread
  #1  
Old 05-25-2009, 04:53 PM
bholus7
Guest
 
Posts: n/a
Default Use XML in Java to Write Save Documents

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
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().tri m();
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================




1
Economics


2
Sports



3
Medical


4
Music


5
Mytest


======================end==================

Reply With Quote
Reply

New topics in Java Forum & Tutorial





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)