xml parsing using dom

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.
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
1) Did you have a question, or are you just sharing code?

2) What part of this is relying on the IBM parser, and why? Most DOM code should work across implementations (that's the point of using a standard API, after all). Until recently, loading a DOM from a file and saving it back was unfortunately not standardized, but DOM Level 3 has introduced standard calls for those purposes and DOM users should consider converging on those... or at least, if you're working in Java, on the JAXP interfaces which that community agreed upon... which again would help keep your code relatively parser-independent. (I believe the current version of IBM's parser supports both DOM Level 3 and JAXP, but I haven't checked recently.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,244
Members
248,133
Latest member
DujonT

Latest Threads

Top