BlackBerry/J2ME - SAX parse collection of objects with attributes

Posted by Changqi Guo on Stack Overflow See other posts from Stack Overflow or by Changqi Guo
Published on 2010-03-16T00:12:58Z Indexed on 2010/03/17 19:21 UTC
Read the original article Hit count: 575

Filed under:
|
|
|

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();
}

}

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();
}

}

© Stack Overflow or respective owner

Related posts about j2me

Related posts about blackberry