java left logo
java middle logo
java right logo
 

Home arrow Java ME Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4100
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Introducing XML parsing in J2ME devices E-mail
User Rating: / 44
PoorBest 

javax.xml.parsers package defines the api for XML parsing.JSR 172 implements Java API for XML parsing. This API represents SAX parser.

Illustration below parse an XML file.

import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.lcdui.*;

public class XMLMidlet extends MIDlet
{
  public XMLMidlet() {}

  protected void startApp()
  {
    try
    {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      SAXParser saxParser = factory.newSAXParser();

      FileConnection fc = (FileConnectionConnector.open(
        "file:///root1/helloworld.xml");
      InputStream is = fc.openInputStream();
      InputSource inputSource = new InputSource(is);
      saxParser.parse(is,new xmlHandler(this));
    }
    catch(Exception ex) {}
  }

  protected void alert(String msg)
  {
    Display display = Display.getDisplay(this);
    Form form = new Form("HelloWorld XML !");
    form.append(msg);
    display.setCurrent(form);
  }

  protected void pauseApp() {}
  protected void destroyApp(boolean bool) {}
}

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import java.util.*;


class xmlHandler extends DefaultHandler
{
  private XMLMidlet midlet;
  private Vector nodes = new Vector()
  private Stack tagStack = new Stack();

  public xmlHandler (XMLMidlet midlet)
  {
    this.midlet = midlet;
  }

  public void startDocument() throws SAXException {}

  public void startElement(String uri, String localName, String qName, 
    Attributes attributesthrows SAXException
  {
    if(qName.equals("node1"))
    {
      Noden node = new Noden();
      nodes.addElement(node);
    }
    tagStack.push(qName);
  }

  public void characters(char[] ch, int start, int lengththrows SAXException
  {
    String chars = new String(ch, start, length).trim();

    if(chars.length() 0)
    {
      String qName = (String)tagStack.peek();
      Noden currentnode = (Noden)nodes.lastElement();

      if (qName.equals("name"))
      {
        currentnode.setName(chars);
      }
      else if(qName.equals("type"))
      {
        currentnode.setType(chars);
      }
    }
  }

  public void endElement(String uri, String localName, String qName, 
    Attributes attributesthrows SAXException
  {
    tagStack.pop();
  }

  public void endDocument() throws SAXException
  {
    StringBuffer result = new StringBuffer();
    for (int i=0; i<nodes.size(); i++)
    {
      Noden currentnode = (Noden)nodes.elementAt(i);
      result.append("Name : "currentnode.getName() " Type : " 
        currentnode.getType() "\n");
    }
    midlet.alert(result.toString());
  }


  class Noden
  {
    private String name;
    private String type;

    public Noden()
    {}

    public void setName(String name)
    {
      this.name = name;
    }

    public void setType(String type)
    {
      this.type = type;
    }

    public String getName()
    {
      return name;
    }

    public String getType()
    {
      return type;
    }
  };
}

You can also use NanoXML which is a small XML parser for Java.


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.