aspose file tools*
The moose likes XML and Related Technologies and the fly likes 'reading' an xml document - what am i doing wrong? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark " Watch " New topic
Author

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

Jay X Brown
Ranch Hand

Joined: Jan 26, 2001
Posts: 51
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).]
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
I was going to run your code, but need the step2p class.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: 'reading' an xml document - what am i doing wrong?