FeaturesPluginsDocs & SupportCommunityPartners

NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

parsing XML

 
Post new topic   Reply to topic    NetBeans Forums -> Java ME Users
View previous topic :: View next topic  
Author Message
Ivy



Joined: 14 Mar 2009
Posts: 18
Location: Philippines

PostPosted: Mon Sep 07, 2009 1:26 pm    Post subject: parsing XML Reply with quote

what's wromg with these codes? it doesnt work. thanks!!!

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
//kxml imports
import org.kxml.*;
import org.kxml.parser.*;

public class ParseXML extends MIDlet implements CommandListener {
private Command exitCommand; // The exit command
private Command displayXML; // On execution, it displays title and description
// on phone screen
private Display display; // The display for this MIDlet
// UI Items for display of title and description on phone screen
private static TextBox t;
private static String textBoxString = "";
// XML String
private String xmlStr = "";
public ParseXML() {
display = Display.getDisplay( this );
exitCommand = new Command( "Exit", Command.EXIT, 2 );
displayXML = new Command( "XML", Command.SCREEN, 1 );
// The XML String in form of RSS
StringBuffer xmlString = new StringBuffer();
xmlString.append("<?xml version=\"1.0\"?>
<!DOCTYPE rss PUBLIC \"-//Netscape Communications//DTD RSS 0.91//EN\"");

xmlString.append("\"http://my.netscape.com/publish/formats/rss-0.91.dtd\">");
xmlString.append("<rss version=\"0.91\">");
xmlString.append("<channel><title>Meerkat: An Open Wire Service</title>");
xmlString.append("<link>http://meerkat.oreillynet.com</link>");
xmlString.append("<description>Meerkat is a Web-based syndicated content
reader based on RSS (\"Rich Site Summary\").
RSS is a fantastic, simple-yet-powerful syndication
system rapidly gaining momentum.");
xmlString.append("</description><language>en-us</language>");
xmlString.append("</channel>");
xmlString.append("</rss>");
xmlStr = xmlString.toString();
}
public void startApp() {
// The textbox displays title and description from a RSS String
t = new TextBox( "MIDlet XML", "kXML", 256, 0 );
t.addCommand( exitCommand );
t.addCommand( displayXML );
t.setCommandListener( this );
display.setCurrent( t );
}
/**
Pause is a no-op since there are no background activities or
record stores that need to be closed.
*/
public void pauseApp() { }
/**
Destroy must cleanup everything not handled by the garbage collector.
In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) { }
/*
Respond to commands, including exit. On the exit command, cleanup
and notify that the MIDlet has been destroyed.
*/
public void commandAction(Command c, Displayable s) {
if ( c == exitCommand ) {
destroyApp( false );
notifyDestroyed();
}
else if ( c == displayXML ) {
try {
viewXML();
}
catch( Exception e ) {
e.printStackTrace();
}
}
}
// This function sets up kxml parser and calls traverse() to parse the whole XML String
public void viewXML() throws IOException {
try {
byte[] xmlByteArray = xmlStr.getBytes();
ByteArrayInputStream xmlStream = new
ByteArrayInputStream( xmlByteArray );
InputStreamReader xmlReader = new
InputStreamReader( xmlStream );
XmlParser parser = new XmlParser( xmlReader );
try
{
traverse( parser, "" );
}
catch (Exception exc)
{
exc.printStackTrace();
}
return;
}
catch ( IOException e ) {
return ;
} finally {
return ;
}
}
/**
Traverses the XML file
*/
public static void traverse( XmlParser parser, String indent ) throws Exception
{
boolean leave = false;
String title = new String();
String desc = new String();
do {
ParseEvent event = parser.read ();
ParseEvent pe;
switch ( event.getType() ) {
// For example, <title>
case Xml.START_TAG:
// see API doc of StartTag for more access methods
// Pick up Title for display
if ("title".equals(event.getName()))
{
pe = parser.read();
title = pe.getText();
}
// Pick up description for display
if ("description".equals(event.getName()))
{
pe = parser.read();
desc = pe.getText();
}
textBoxString = title + " " + desc;
traverse( parser, "" ) ; // recursion call for each <tag></tag>
break;
// For example </title?
case Xml.END_TAG:
leave = true;
break;
// For example </rss>
case Xml.END_DOCUMENT:
leave = true;
break;
// For example, the text between tags
case Xml.TEXT:
break;
case Xml.WHITESPACE:
break;
default:
}
} while( !leave );
t.setString( textBoxString );
}
}
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java ME Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB