I have created one XML parsing application using KXML.I want to read the xml content in InputStream format and want to display that content on my Emulator or on console.
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
impor开发者_StackOverflow社区t org.xml.sax.SAXException;
public class Connection1 extends Thread {
private Connection1bean conn1 = new Connection1bean();
public Connection1(InputStream is) {
// TODO Auto-generated constructor stub
StreamConnection conn;
DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
try{
conn=(StreamConnection)Connector.open("http://magazine.ateemo.com/magazines/by_publisher/2;deviceside=true;interface=wifi");
documentbuilderfactory.setCoalescing(true);
DocumentBuilder docBuilder=documentbuilderfactory.newDocumentBuilder();
Document doc = docBuilder.parse(is);
Vector book = new Vector();
NodeList nodelist = doc.getElementsByTagName("cid");
Node node = nodelist.item(0);
Node tempNode = ((NodeList)node.getChildNodes()).item(0);
conn1.setCid(Integer.parseInt(tempNode.getNodeValue()));
NodeList nodelist1 = doc.getElementsByTagName("description");
Node node1 = nodelist1.item(0);
Node tempNode1 = ((NodeList)node1.getChildNodes()).item(0);
conn1.setDescription(tempNode1.getNodeValue());
NodeList nodelist2 = doc.getElementsByTagName("publisher");
Node node2 = nodelist2.item(0);
Node tempNode2 = ((NodeList)node2.getChildNodes()).item(0);
conn1.setPublisher((tempNode2.getNodeValue()));
/*NodeList nodelist = doc.getElementsByTagName("image_url");
Node node = nodelist.item(0);
Node tempNode = ((NodeList)node.getChildNodes()).item(0);
conn1.setimage_url(Integer.parseInt(tempNode.getNodeValue()));*/
conn1.setBook(book);
//conn.setimage_name(image_name);
}
catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection1bean getconn1(){
return conn1;
}
}
And i am using this below class to display the contents.
import java.io.InputStream;
import net.rim.device.api.ui.UiApplication;
import javax.microedition.io.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.browser.field.RequestedResource;
import net.rim.device.api.lbs.maps.ui.RichMapField;
import net.rim.device.api.system.*;
import net.rim.device.api.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class XML_Parsing_Sample extends UiApplication {
//creating a member variable for the MainScreen
MainScreen _screen= new MainScreen();
// MainScreen abc= new MainScreen();
//string variables to store the values of the XML document
String _node,_element;
//Connection _connectionthread;
Connection1 _connectionthread1;
public XML_Parsing_Sample() {
// TODO Auto-generated constructor stub
_screen.setTitle("XML Parsing");//setting title
_screen.add(new RichTextField("Requesting....."));
_screen.add(new SeparatorField());
//_screen.add(new RichTextField("XML data"));
pushScreen(_screen); // creating a screen
//creating a connection thread to run in the background
System.out.println("1111111111111");
_connectionthread1 = new Connection1();
System.out.println("222222222222222");
_connectionthread1.start();//starting the thread operation
System.out.println("After connection run");
// public void receive(Response response);
//Connection1bean conbean=new Connection1bean();
//String abc=conbean.getDescription();
}
public void updateField(String node, String element){
//receiving the parsed node and its value from the thread
//and updating it here
//so it can be displayed on the screen
String title="Title";
_screen.add(new RichTextField(node+" : "+element));
if(node.equals(title)){
_screen.add(new SeparatorField());
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
XML_Parsing_Sample application = new XML_Parsing_Sample();
//create a new instance of the application
//and start the application on the event thread
application.enterEventDispatcher();
}
}
精彩评论