我已创建XML文件,但我无法查看/输出它。我知道无法输出创建的XML文件。
有人可以建议什么是创建xml文件的更好方法? 1.使用DocumentBuilderFactory创建xml,然后解析它 2.手动创建硬编码的xml并将其保存在SD卡上,然后访问它进行解析。
我在xml文件中持续改变文本数据。 哪种方法最适合我?
答案 0 :(得分:15)
我正在使用kXML2创建/ chage / save / read xml。与BlackBerry一起使用时请记住:
- 要发布,你必须预先验证它&与蚂蚁建立项目
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序列化为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()));
答案 1 :(得分:1)
使用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输出定向到光盘上的文件而不是字节数组。
(如果出于某种原因无法使用外部库,这似乎是一种非常可行的方法)
答案 2 :(得分:-1)
您也可以使用net.rim.device.api.xml.jaxp.XMLWriter 但它需要编写一整页代码来生成一个 除非我错过了什么?