【发布时间】:2009-05-18 12:12:27
【问题描述】:
我已经创建了 XML 文件,但是我无法查看/输出它。我知道没有办法输出创建的 XML 文件。
谁能建议创建 xml 文件的更好方法? 1.用DocumentBuilderFactory创建xml然后解析或者 2.手动创建硬编码的xml并保存在sd卡上,然后访问解析。
我在 xml 文件中有不断变化的文本数据。 哪种方法最适合我?
【问题讨论】:
标签: xml blackberry file-io java-me
我已经创建了 XML 文件,但是我无法查看/输出它。我知道没有办法输出创建的 XML 文件。
谁能建议创建 xml 文件的更好方法? 1.用DocumentBuilderFactory创建xml然后解析或者 2.手动创建硬编码的xml并保存在sd卡上,然后访问解析。
我在 xml 文件中有不断变化的文本数据。 哪种方法最适合我?
【问题讨论】:
标签: xml blackberry file-io java-me
我正在使用kXML2 创建/更改/保存/读取 xml。与黑莓一起使用记住:
- 对于发布,您必须预先验证它并使用 ant 构建项目
Ahmad Ferdous Bin Alam - How to Import kxml jar File to Your Project
Slashdev - BlackBerry Development with Ant & Eclipse
更新: Tutorial: How To Use 3rd Party Libraries in your Applications
- 对于调试,您必须将 kXML sources 和 org.xmlpull.v1 sources 添加到您的 BB 项目中
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);
确保您具有读/写操作的访问权限
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 - Interface FileConnection
SUN Dev Network - Getting Started with the FileConnection APIs
RIM - How To - Add plain text or binary files to an application
BB Support Forum - Some questions about FileConnection/JSR 75
Sony Ericsson Forum - Create XML file
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 Example
您所要做的就是获取所需的元素并对其进行更改:
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 doc 序列化为字符串并放入 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()));
【讨论】:
使用DOMInternalRepresentation 类怎么样?尽管有它的名字,它是公共 API 的一部分,并且自 JDE 4.0.0 以来就存在。使用DOMInternalReprestationClass,您可以将Document 写入XMLWriter。
例如,写信给ByteArrayOutputStream(方便发送Connection):
ByteArrayOutputStream os = new ByteArrayOutputStream();
XMLWriter xw = new XMLWriter(os);
DOMInternalRepresentation.parse( myDocument, xw );
当然,您可以使用FileOutputStream 将 XML 输出定向到磁盘上的文件而不是字节数组。
(如果由于某种原因您不能使用外部库,这似乎是一种非常可行的方法)
【讨论】:
org.w3c.dom.Document 类设置文档的编码。有一个 getter (getXmlEncoding()) 但没有 setter。另一方面,如果使用kXML2,它就像在`org.kxml2.kdom.Document' 类上使用setEncoding 一样简单。
您也可以使用 net.rim.device.api.xml.jaxp.XMLWriter 但它需要编写一整页代码来生成一个 除非我错过了什么?
【讨论】: