在黑莓中从 URL 解析 XML 数据时遇到问题

发布于 2024-11-26 07:08:10 字数 5190 浏览 0 评论 0原文

我使用 KXML 创建了一个 XML 解析应用程序。我想读取以下位置的 xml 内容 InputStream 格式并希望在我的模拟器或控制台上显示该内容。

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

 }

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

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文