sunil (archive)
Usenet Archive
- Joined
- Jun 7, 2002
- Messages
- 164
- Reaction score
- 0
ParserEx.java
import com.ibm.xml.parser.*;
import org.w3c.dom.*;
import java.io.*;
class ParserEx{
public ParserEx{}
public static void main(String args){
try{
Document doc;
String ROOT_ELEMENT_TAG = "person";
String colNames = {"fname","addr","rollno","first","second"}; String URL1 ="C:/Tomcat/static/AddressBook.xml"; java.io.FileReader filereader = new FileReader(URL1); java.io.BufferedReader reader = new BufferedReader( filereader ) ; Parser p = new Parser(URL1); doc = p.readStream(reader);
//display table column
//for(int i=0; i<colNames.length; i++){
//System.out.println( colNames);
//}
//need to iterate the doc to get the fields in it
int rowCount = XmlUtils.getSize( doc , ROOT_ELEMENT_TAG ); System.out.println("-----------------------------------"); System.out.println("count="+rowCount); System.out.println("-----------------------------------"); for(int r=0; r<rowCount; r++) { Element row = XmlUtils.getElement(doc , ROOT_ELEMENT_TAG , r ); int colCount = colNames.length; for(int c=0; c < colCount; c++) { System.out.println( XmlUtils.getValue( row , colNames[c] ));
}//end for c=0...
}//end for r=0...
}catch(Exception e){
System.out.println( e );
}
}
}
XmlUtils.java
import org.w3c.dom.*;
public class XmlUtils{
public static Element getElement( Document doc , String tagName , int index ){ //given an XML document and a tag //return an Element at a given index NodeList rows = doc.getDocumentElement.getElementsByTagName( tagName ); return (Element)rows.item( index ); }
public static int getSize( Document doc , String tagName ){ //given an XML document and a tag name //return the number of ocurances NodeList rows = doc.getDocumentElement.getElementsByTagName( tagName ); return rows.getLength; }
public static String getValue( Element e , String tagName ){ try{ //get node lists of a tag name from a Element NodeList elements = e.getElementsByTagName( tagName );
Node node = elements.item( 0 );
NodeList nodes = node.getChildNodes;
//find a value whose value is non-whitespace
String s;
for( int i=0; i<nodes.getLength; i++){
s = ((Node)nodes.item( i )).getNodeValue.trim; if(s.equals("") || s.equals("\r")) { continue; } else return s; }
}
catch(Exception ex){
System.out.println( ex );
ex.printStackTrace;
}
return null;
}
public static void printNodeTypes( NodeList rows ){ System.out.println( "\tenumerating NodeList (of Elements):"); System.out.println( "\tClass\tNT\tNV" ); //iterate a given Node list for( int ri = 0 ; ri < rows.getLength ; ri++){ Node n = (Node)rows.item( ri ); if( n instanceof Element) { System.out.print( "\tElement" ); } else System.out.print( "\tNode" );
//print out Node type and Node value
System.out.println(
"\t"+
n.getNodeType + "\t" +
n.getNodeValue
);
}
System.out.println;
}
}//end class
to execute this code you need ibm dom parser.
import com.ibm.xml.parser.*;
import org.w3c.dom.*;
import java.io.*;
class ParserEx{
public ParserEx{}
public static void main(String args){
try{
Document doc;
String ROOT_ELEMENT_TAG = "person";
String colNames = {"fname","addr","rollno","first","second"}; String URL1 ="C:/Tomcat/static/AddressBook.xml"; java.io.FileReader filereader = new FileReader(URL1); java.io.BufferedReader reader = new BufferedReader( filereader ) ; Parser p = new Parser(URL1); doc = p.readStream(reader);
//display table column
//for(int i=0; i<colNames.length; i++){
//System.out.println( colNames);
//}
//need to iterate the doc to get the fields in it
int rowCount = XmlUtils.getSize( doc , ROOT_ELEMENT_TAG ); System.out.println("-----------------------------------"); System.out.println("count="+rowCount); System.out.println("-----------------------------------"); for(int r=0; r<rowCount; r++) { Element row = XmlUtils.getElement(doc , ROOT_ELEMENT_TAG , r ); int colCount = colNames.length; for(int c=0; c < colCount; c++) { System.out.println( XmlUtils.getValue( row , colNames[c] ));
}//end for c=0...
}//end for r=0...
}catch(Exception e){
System.out.println( e );
}
}
}
XmlUtils.java
import org.w3c.dom.*;
public class XmlUtils{
public static Element getElement( Document doc , String tagName , int index ){ //given an XML document and a tag //return an Element at a given index NodeList rows = doc.getDocumentElement.getElementsByTagName( tagName ); return (Element)rows.item( index ); }
public static int getSize( Document doc , String tagName ){ //given an XML document and a tag name //return the number of ocurances NodeList rows = doc.getDocumentElement.getElementsByTagName( tagName ); return rows.getLength; }
public static String getValue( Element e , String tagName ){ try{ //get node lists of a tag name from a Element NodeList elements = e.getElementsByTagName( tagName );
Node node = elements.item( 0 );
NodeList nodes = node.getChildNodes;
//find a value whose value is non-whitespace
String s;
for( int i=0; i<nodes.getLength; i++){
s = ((Node)nodes.item( i )).getNodeValue.trim; if(s.equals("") || s.equals("\r")) { continue; } else return s; }
}
catch(Exception ex){
System.out.println( ex );
ex.printStackTrace;
}
return null;
}
public static void printNodeTypes( NodeList rows ){ System.out.println( "\tenumerating NodeList (of Elements):"); System.out.println( "\tClass\tNT\tNV" ); //iterate a given Node list for( int ri = 0 ; ri < rows.getLength ; ri++){ Node n = (Node)rows.item( ri ); if( n instanceof Element) { System.out.print( "\tElement" ); } else System.out.print( "\tNode" );
//print out Node type and Node value
System.out.println(
"\t"+
n.getNodeType + "\t" +
n.getNodeValue
);
}
System.out.println;
}
}//end class
to execute this code you need ibm dom parser.