You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by "Swanson, Brion" <Br...@westgroup.com> on 2001/03/02 16:41:33 UTC
RE: Why the Elements are not parsed???
Have you tried changing:
XMLReader reader = new SAXParser ();
reader.parse (uri);
to...
SAXParser parser = new SAXParser ();
parser.parse (uri);
Brion Swanson
-------------
Software Engineer
West Group
Rochester, NY
-----Original Message-----
From: sateesh [mailto:kls_kumar@123india.com]
Sent: Friday, March 02, 2001 3:29 PM
To: 'xerces-j-user@xml.apache.org'
Subject: Why the Elements are not parsed???
Hi,
I tested the following java program and the xml file i used is
HelloWorld.xml which is given at the end and the output I am getting is
also given at the end:
Please let me know why the startElement and endElement call backs are
not getting invoked...Where I went wrong................
import java.io.*;
import org.xml.sax.XMLReader;
import org.apache.xerces.parsers.*;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.ContentHandler;
public class SAXParserDemo implements ContentHandler{
private Locator locator;
public void setDocumentLocator(Locator locator) {
System.out.println("Inside the setDocumentLocator()");
this.locator=locator;
}
public void startDocument()throws SAXException{
System.out.println("Parisng begins
now..................");
}
public void endDocument()throws SAXException{
System.out.println("Parisng ended ..................");
}
public void startElement(String nameSpaceURI,String
localName,String rawName,Attributes attr)throws SAXException{
System.out.println("Start Element:"+localName);
if(!"".equals(nameSpaceURI)){
System.out.println("In NameSpace "+nameSpaceURI+"
("+rawName+")");
}
else{
System.out.println("Has no associated
NameSpace");
}
}
public void endElement(String nameSpaceURI,String localName,String
rawName)throws SAXException{
}
public void characters(char[] ch,int start,int end)throws
SAXException{
}
public void ignorableWhitespace(char[] ch,int start,int end)throws
SAXException{
}
public void skippedEntity(String name)throws SAXException{
}
public void startPrefixMapping(java.lang.String prefix,
java.lang.String uri) {
}
public void endPrefixMapping(java.lang.String prefix)throws
SAXException{
}
public void processingInstruction(java.lang.String target,
java.lang.String data) {
}
public void performDemo(String uri){
System.out.println("Parsing XML File"+uri);
try{
System.out.println("Before creating the SAXParser
Object");
XMLReader reader=new SAXParser();
reader.parse(uri);
System.out.println("After parsing the File");
}
catch(IOException ie){
System.out.println("IOException has
arised"+ie.getMessage());
}
catch(SAXException se){
System.out.println("SAXException has
arised"+se.getMessage());
}
}
public static void main(String args[]){
if(args.length!=1){
System.out.println("Usage: java SAXParserDemo [XML
URI]");
System.exit(0);
}
String uri=args[0];
System.out.println("Before creating the ParseDemo
Object");
SAXParserDemo parserDemo=new SAXParserDemo();
System.out.println("After creating the ParseDemo
Object");
parserDemo.performDemo(uri);
}
}
THE XML FILE IS GIVEN BELOW....
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
<?xml-stylesheet type="text/xsl" href="foo.xsl"?>
-->
<foo name="first">Some Sample Text
<child>I Am a child1
<d1 name="ram">cell11</d1>
<d2>cell12</d2>
<d3>cell13</d3>
<d4>cell14</d4>
</child>
<child>I Am a child2
<d1 name="ravi">cell21</d1>
<d2>cell22</d2>
<d3>cell23</d3>
<d4>cell24</d4>
</child>
</foo>
--------------------------
OUTPUT
------------------------
Before creating the ParseDemo Object
After creating the ParseDemo Object
Parsing XML Filec:\xmlSamples\HelloWorld.xml
Before creating the SAXParser Object
After parsing the File
---------------------------------------------------------------------------
Thanks in advance..............
Regards
Sateesh
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org