在Blackberry中创建XML的更好方法

iOS*_*Dev 2 xml file-io blackberry java-me

我已创建XML文件,但我无法查看/输出它.我知道无法输出创建的XML文件.

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

我在xml文件中持续改变文本数据.哪种方法最适合我?

Mak*_*tar 15

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

创建XML

    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);
Run Code Online (Sandbox Code Playgroud)

在BlackBerry文件系统上保存XML

读取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();
    }
Run Code Online (Sandbox Code Playgroud)

另请参见: RoseIndia.net - J2ME Kxml示例

更改XML文档

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

    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");
Run Code Online (Sandbox Code Playgroud)

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

只需将xml doc序列化为string并将其放在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()));
Run Code Online (Sandbox Code Playgroud)