在 Blackberry 中创建 XML 的更好方法 [英] Better approach for XML Creation in Blackberry

查看:28
本文介绍了在 Blackberry 中创建 XML 的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了 XML 文件,但是我无法查看/输出它.我知道没有办法输出创建的 XML 文件.

I have created XML file,but I can't view it/output it.I know there is no way to output created XML file.

任何人都可以建议创建 xml 文件的更好方法吗?1.用DocumentBuilderFactory创建xml然后解析或者2.手动创建硬编码的xml并将其保存在sd卡上,然后访问它进行解析.

Can anyone please suggest what is better way of creating xml files? 1. create xml with DocumentBuilderFactory and then parse it Or 2. manually create hardcoded xml and save it on sd card and then access it for parsing.

我在 xml 文件中有不断变化的文本数据.哪种方法最适合我?

I have continuosly varying text data in xml files. Which approach will suite me most?

推荐答案

我使用 kXML2 来创建/chage/save/read xml.与 BlackBerry 一起使用请记住:
- 要发布,您必须预先验证它&用蚂蚁构建项目
Ahmad Ferdous Bin Alam - 如何将 kxml jar 文件导入您的项目
Slashdev - 使用 Ant & 进行黑莓开发日食
更新: 教程:如何在您的应用程序中使用第 3 方库
- 对于调试,您必须添加 kXML 源org.xmlpull.v1 源 到您的BB项目

I'm using kXML2 to create/chage/save/read xml. Using it with BlackBerry remember:
- for release you have to preverify it & build proj with ant
Ahmad Ferdous Bin Alam - How to Import kxml jar File to Your Project
Slashdev - BlackBerry Development with Ant & Eclipse
UPDATE: Tutorial: How To Use 3rd Party Libraries in your Applications
- for debug you have to add kXML sources and org.xmlpull.v1 sources to your BB project

    Document d = new Document();
    Element root = d.createElement("", "parent");       
    root.setName("catalog");
    Element book = d.createElement("", "child");            
    book.setName("book");       
    book.setAttribute(null, "id", "1");             
    Element author = d.createElement("", "child");              
    author.setName("author");               
    author.addChild(0, Node.TEXT, "Colin Wilson");      
    book.addChild(0, Node.ELEMENT, author);

    Element title = d.createElement("", "child");           
    title.setName("title");             
    title.addChild(0, Node.TEXT, "The Mind Parasites");     
    book.addChild(1, Node.ELEMENT, title);

    Element genre = d.createElement("", "child");           
    genre.setName("genre");
    genre.addChild(0, Node.TEXT, "Horror novel, Science fiction novel");    
    book.addChild(2, Node.ELEMENT, genre);

    Element publishDate = d.createElement("", "child");             
    publishDate.setName("publish-date");                
    publishDate.addChild(0, Node.TEXT, "1967"); 
    book.addChild(3, Node.ELEMENT, publishDate);

    root.addChild(0, Node.ELEMENT, book);
    d.addChild(root.ELEMENT, root);

在 BlackBerry 文件系统上保存 XML

  • 如果使用模拟器不要忘记模拟 SD 卡(工具->更改 SD 卡...)
  • 确保您拥有读/写操作的访问权限

    Save XML on BlackBerry filesystem

    • If use emulator don't forget to emulate SD card (Tools->Change SD Card...)
    • be sure you have access rights for read/write operation

      String fileName = "file:///SDCard/books.xml";
      DataOutputStream os = null;
      FileConnection fc = null;
      try {
          fc = (FileConnection) Connector.open(fileName, Connector.READ_WRITE);
          if (!fc.exists())
              fc.create();
      
          os = fconn.openDataOutputStream();
          KXmlSerializer serializer = new KXmlSerializer();
          serializer.setOutput(os, "UTF-8");
          d.write(serializer);
      
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } 
      

      另见:
      RIM API - 接口文件连接
      SUN Dev Network - FileConnection API 入门
      RIM - 如何 - 向应用程序添加纯文本或二进制文件
      BB 支持论坛 - 关于 FileConnection/JSR 75 的一些问题
      索尼爱立信论坛 - 创建 XML 文件

          Document d= new Document();
          FileConnection fc =  null;
          DataInputStream is = null;
          try {
              fc = (FileConnection) Connector.open(fileName, Connector.READ);
              is = fc.openDataInputStream();
      
              KXmlParser parser = new KXmlParser();
              parser.setInput(is, "UTF-8");
              d.parse(parser);
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (XmlPullParserException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      

      另见:RoseIndia.net - J2ME Kxml 示例

      您所要做的就是获取所需的元素并对其进行更改:

      All you have to do is get needed element and change it:

          Element catalog = d.getElement("", "catalog");
      
          Element book = catalog.getElement("", "book");
      
          Element title = book.getElement("", "title");
          title.removeChild(0);
          title.addChild(Element.TEXT, "Spider World: The Tower");
      
          Element publish = book.getElement("", "publish-date");
          publish.removeChild(0);
          publish.addChild(Element.TEXT, "1987");
      

      将 XML 文档输出到 BlackBerry 屏幕(在 Screen 类中的某处)

      只需将 xml doc 序列化为字符串并放入 RichTextField 中:

      Output XML document to BlackBerry screen (somewhere in Screen class)

      Simply serialize xml doc to string and put it in RichTextField:

          deleteAll();
          ByteArrayOutputStream baos = new ByteArrayOutputStream();       
          KXmlSerializer serializer = new KXmlSerializer();
          try {
              serializer.setOutput(baos, "UTF-8");
              d.write(serializer);    
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }   
          add(new RichTextField(baos.toString()));
      

      这篇关于在 Blackberry 中创建 XML 的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆