将 Xml 文档写入文件以保存 Blackberry 中的更改
我正在尝试在 Blackberry 中解析 Xml。我将xml复制到SD卡上。我尝试了这段代码并且成功了。我尝试将新标签(节点)插入到 xml 中,它可以工作,但它们被添加到文件末尾,但我不知道这是否是最好的方法,但是我如何将 Xml 文档写入保存更改的文件?
DocumentBuilderFactory docBuilderFactory= DocumentBuilderFactory. newInstance();
DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
doc = docBuilder.parse(conn.openInputStream());
InsertBlock(doc);
doc.getDocumentElement ().normalize ();
NodeList list=doc.getElementsByTagName("*");
node=new String();
element = new String();
for (int i=0;i<list.getLength();i++){
Node value=list.item(i).getChildNodes().item(0);
node=list.item(i).getNodeName();
element=value.getNodeValue();
}
并插入新节点:
Node emp=myDocument.createElement("Emp");
Text NodeText = myDocument.createTextNode("DD");
emp.appendChild(NodeText);
myDocument.appendChild(emp);
I'm trying to parse Xml in Blackberry. I copied the xml to the SD card. I tried this code and I succeeded. I tried to insert new tags (Nodes) to the xml and it works but they are added to the end of the file but I don't know if it is the best way to do that, but how can I write the Xml document to the file to save the changes??
DocumentBuilderFactory docBuilderFactory= DocumentBuilderFactory. newInstance();
DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
doc = docBuilder.parse(conn.openInputStream());
InsertBlock(doc);
doc.getDocumentElement ().normalize ();
NodeList list=doc.getElementsByTagName("*");
node=new String();
element = new String();
for (int i=0;i<list.getLength();i++){
Node value=list.item(i).getChildNodes().item(0);
node=list.item(i).getNodeName();
element=value.getNodeValue();
}
And for inserting new Nodes :
Node emp=myDocument.createElement("Emp");
Text NodeText = myDocument.createTextNode("DD");
emp.appendChild(NodeText);
myDocument.appendChild(emp);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了插入新节点,您应该使用
Node#insertBefore()
而不是Node#appendChild()
。检查文档 此处。替换
为
其中 someExistingNode 是要在其之前添加新
Node
的Node
(可能是Element
)员工。编辑 1: 如何将 XML 写入文件
编辑 2: 添加了节点插入和 XML 到文件写入的代码示例
另请检查 这个问题有关在 BlackBerry 上创建 XML。
In order to insert new node(s) you should use
Node#insertBefore()
instead ofNode#appendChild()
. Check documentation here.Replace
with
Where someExistingNode is the
Node
(probablyElement
) before which you want to add your newNode
emp.Edit 1: How to write XML to file
Edit 2: Added code sample for node insertion and XML-to-file writing
Also check this question regarding XML creation on BlackBerry.