I have a problem with using the SAX parser to parse a XML file. It is a complex XML file, it is like the following.
<Objects>
<Object no="1">
<field name="PID">ilives:87877</field>
<field name="dc.coverage">Charlottetown</field>
<field name="fgs.ownerId">fedoraAdmin</field>
</Object>
<Object no="2">......
I am confused how to get the names in each field, and how to store the information of each object.
import java.util.Enumeration;
import java.util.Hashtable;
public class XMLObject {
private Hashtable mFields = new Hashtable();
private int mN = -1;
public int getN() {
return mN;
}
public void setN(int n) {
mN = n;
}
public String getStringField(String key) {
return (String) mFields.get(key);
}
public void setStringField(String key, String value)
{
mFields.put(key, value);
}
public String getPID() {
return getStringField("PID");
}
public void setPID(String pid) {
setStringField("PID", pid);
}
public String getDcCoverage() {
return getStringField("dc.coverage");
}
public void setDcCoverage(String dcCoverage) {
setStringField("dc.coverage", dcCoverage);
}
public String getFgsOwnerId() {
return getStringField("fgs.ownerId");
}
public void setFgsOwnerId(String fgsOwnerId) {
setStringField("fgs.ownerId", fgsOwnerId);
}
public String dccreator() {
return getStringField("dc.creator");
}
public void dccreator(String dccreator) {
setStringField("dc.creator", dccreator);
}
public String getdcformat() {
return getStringField("dc.format");
}
public void setdcformat(String dcformat) {
setStringField("dc.format", dcformat);
}
public String getdcidentifier() {
return getStringField("dc.identifier");
}
public void setdcidentifier(String dcidentifier) {
setStringField("dc.identifier", dcidentifier);
}
public String getdclanguage() {
return getStringField("dc.language");
}
public void setdclanguage(String dclanguage) {
setStringField("dc.language", dclanguage);
}
public String getdcpublisher()
{
return getStringField("dc.publisher");
}
public void setdcpublisher(String dcpublisher)
{
setStringField("dc.publisher",dcpublisher);
}
public String getdcsubject()
{
return getStringField("dc.subject");
}
public void setdcsubject(String dcsubject)
{
setStringField("dc.subject",dcsubject);
}
public String getdctitle()
{
return getStringField("dc.title");
}
public void setdctitle(String dctitle)
{
setStringField("dc.title",dctitle);
}
public String getdctype()
{
return getStringField("dc.type");
}
public void setdctype(String dctype)
{
setStringField("dc.type",dctype);
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("N:"+mN+";");
Enumeration keys = mFields.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
sb.append(key+":"+mFields.get(key)+";");
}
return sb.toString();
}
}
i used the same handler class you provided
import java.io.*;
import net.rim.device.api.system.Bitmap;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.InputStream;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
public class xmlparsermainscreen extends MainScreen{
private static String xmlres = "/xml/xml1.xml";
private RichTextField textOutputField;
public xmlparsermainscreen() throws ParserConfigurationException,
net.rim.device.api.xml.parsers.ParserConfigurationException,
IOException
{
InputStream inputStream = getClass().getResourceAsStream(xmlres);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[10000];
int bytesRead = inputStream.read(buffer);
while (bytesRead > 0) {
baos.write(buffer, 0, bytesRead);
bytesRead = inputStream.read(buffer);
}
baos.close();
String result=baos.toString();
ByteArrayInputStream bais =
new ByteArrayInputStream(result.getBytes());
XMLObject[] xmlObjects = getXMLObjects(bais);
for (int i = 0; i < xmlObjects.length; i++) {
XMLObject o = xmlObjects[i];
textOutputField = new RichTextField();
add(textOutputField);
textOutputField.setText(o.toString());
// add(new LabelField(o.toString()));
}
LabelField resultdis=new LabelField("resultdisplay");
add(resultdis);
//textOutputField = new RichTextField();
//add(textOutputField);
//textOutputField.setText(result);
}
static XMLObject[] getXMLObjects(InputStream is) throws
ParserConfigurationException {
XMLObjectHandler xmlObjectHandler = new XMLObjectHandler();
try {
SAXParser parser = SAXParserFactory.newInstance()
.newSAXParser();
parser.parse(is, xmlObjectHandler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xmlObjectHandler.getXMLObjects();
}
}
App code:
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import net.rim.device.api.ui.UiApplication;
public class xmlparser extends UiApplication {
private xmlparser() throws ParserConfigurationException,
net.rim.device.api.xml.parsers.ParserConfigurationException,
IOException
{
pushScreen( new xmlparsermainscreen() );
}
public static void main( String[] args )
throws ParserConfigurationException,
net.rim.device.api.xml.parsers.ParserConfigurationException,
IOException
{
new xmlparser().enterEventDispatcher();
}
}
the xml i am using
<resultPage xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:foxml="info:fedora/fedora-system:def/foxml#"
xmlns:zs="http://www.loc.gov/zing/srw/" indexName="BasicIndex"
dateTime="Wed Mar 17 09:13:26 ADT 2010">
<gfindObjects hitTotal="199" resultPageXslt="" hitPageSize="2"
hitPageStart="1" query="dc.title:"charlottetown"">
<objects>
<object no="1" score="4.550192">
<field name="PID">ilives:86472</field>
<field name="dc.coverage">Charlottetown (P.E.I.)</field>
<field name="dc.coverage">Prince Edward Island</field>
<field name="dc.creator">Hines, Sherman , 1941-</field>
<field name="dc.format">electronic</field>
<field name="dc.identifier">None</field>
<field name="dc.identifier">ilives:86472</field>
<field name="dc.language">eng</field>
<field name="dc.publisher">Random House</field>
<field name="dc.subject">Description--Views</field>
<field name="dc.subject">Description and
travel--Views</field>
<field name="dc.title" snippet="yes">
<span class="highlight">Charlottetown</span>and Prince
Edward Island: Sherman Hines images of Canada</field>
<field name="dc.type">collection</field>
<field name="dc.type">bib-only</field>
<field name="fgs.createdDate">
2009-06-09T18:24:11.972Z</field>
<field name="fgs.label">191_86472 _foXML.xml</field>
<field name="fgs.lastModifiedDate">
2009-06-23T19:29:44.930Z</field>
<field name="fgs.ownerId">fedoraAdmin</field>
<field name="fgs.state">Active</field>
<field name="mods.dateIssued">1990</field>
<field name="mods.extent">1 v. unpaged ; 22 cm.</field>
<field name="mods.form">print</field>
<field name="mods.issuance">monographic</field>
<field name="mods.place_of_publication">Toronto</field>
<field name="mods.publisher">Random House</field>
<field name="mods.subTitle">Sherman Hines images of
Canada</field>
<field name="mods.subject">Charlottetown (P.E.I.)</field>
<field name="mods.subject">Description</field>
<field name="mods.subject">Views</field>
<field name="mods.subject">Prince Edward Island</field>
<field name="mods.subject">Description and travel</field>
<field name="mods.subject">Views</field>
<field name="mods.title">Charlottetown and Prince Edward
Island</field>
<field name="rels.hasModel">
info:fedora/ilives:bookCModel</field>
<field name="rels.isMemberOf">
info:fedora/ilives:collection</field>
</object>
</objects>
</gfindObjects>
</resultPage>