• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

'reading' an xml document - what am i doing wrong?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am reposting as a response to michael's reply below. with complete code for all four relevant files.
i would really appreciate some help.
---
i have written a simple class to get output from a simple sax parser that is parsing a very simple xml.
i am only getting null output.
what am i doing wrong?
---
again, please note that in the startElement method it shows an updating of the xmlInput array but this somehow only shows up as 'null' if it is recalled anywhere else.
thanks in advance.
jay
====================================
******the class to be executed ****
public class step2app
{
public static void main (String arg[])
{
step2p obj=new step2p();
String[] output=obj.makeArray();
for (int i=0;i<3;i++)
{
System.out.println(output[i]);
}
}
}
******the parsing class ****
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class step2p extends HandlerBase
{
public String[] xmlInput=new String[8];
int count=0;
public static void main (String arg [])
{
//
}
//===========================================================
// SAX DocumentHandler methods
//===========================================================
public void setDocumentLocator (Locator l)
{
}
public void startDocument ()
throws SAXException
{
}
public void endDocument ()
throws SAXException
{
}
public void startElement (String name, AttributeList attrs)
throws SAXException
{
xmlInput[count]=name;
// System.out.println("In startElement method (Element Name): "+name);
// System.out.println("In startElement method (count): "+count);
// System.out.println("In startElement method (xmlInput):" +xmlInput[count]);
count=count+1;
}
public void endElement (String name)
throws SAXException
{
//
}
public void characters (char buf [], int offset, int len)
throws SAXException
{
//
}
public void ignorableWhitespace (char buf [], int offset, int len)
throws SAXException
{
//
}
public void processingInstruction (String target, String data)
throws SAXException
{
//
}
//===========================================================
// makeArray
//===========================================================
public String[] makeArray()
{
step2p arrayObj=new step2p();
// validating parser
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
try {
// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File("step2.xml"), new step2p() );
} catch (SAXParseException spe) {
// Error generated by the parser
System.out.println ("\n** Parsing error"
+ ", line " + spe.getLineNumber ()
+ ", uri " + spe.getSystemId ());
System.out.println(" " + spe.getMessage() );
// Use the contained exception, if any
Exception x = spe;
if (spe.getException() != null)
x = spe.getException();
x.printStackTrace();
} catch (SAXException sxe) {
// Error generated by this application
// (or a parser-initialization error)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}
return xmlInput;
}
//===========================================================
// SAX ErrorHandler methods
//===========================================================
// treat validation errors as fatal
public void error (SAXParseException e)
throws SAXParseException
{
throw e;
}
// dump warnings too
public void warning (SAXParseException err)
throws SAXParseException
{
System.out.println ("** Warning"
+ ", line " + err.getLineNumber ()
+ ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}
}
****** xml document ****
<?xml version='1.0' encoding='us-ascii'?>
<!-- parsing experiment -->
<!DOCTYPE country_info SYSTEM "step2.dtd">
<country_info>
<country_code>XN</country_code>
<worldarea>IMG</worldarea>
<country_name>Xanadu</country_name>
</country_info>
****** the dtd ****
<?xml version='1.0' encoding='us-ascii'?>
<!--
step2 DTD
-->
<!ELEMENT country_info (country_code|country_name|worldarea)*>
<!ELEMENT country_code (#PCDATA) >
<!ELEMENT country_name (#PCDATA)>
<!ELEMENT worldarea (#PCDATA)>

[This message has been edited by Jay Brown (edited February 22, 2001).]
 
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was going to run your code, but need the step2p class.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic