将 Xml 文档写入文件以保存 Blackberry 中的更改

发布于 2025-01-06 17:52:17 字数 908 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2025-01-13 17:52:17

为了插入新节点,您应该使用 Node#insertBefore() 而不是 Node#appendChild()。检查文档 此处

替换

Node emp=myDocument.createElement("Emp");
Text NodeText = myDocument.createTextNode("DD");
emp.appendChild(NodeText);
myDocument.appendChild(emp); 

Node emp=myDocument.createElement("Emp");
Text NodeText = myDocument.createTextNode("DD");
emp.appendChild(NodeText); 
myDocument.insertBefore(emp, someExistingNode); 

其中 someExistingNode 是要在其之前添加新 NodeNode(可能是 Element员工

编辑 1: 如何将 XML 写入文件

try {
    String filePath = "file:///store/home/user/XmlFile.xml";
    FileConnection fc = (FileConnection) Connector.open(filePath, Connector.READ_WRITE);
    if (!fc.exists()) {
        fc.create();  // create the file if it doesn't exist
    } else {
        fc.truncate(0); // truncate the file if it exists
    }

    OutputStream os = fc.openOutputStream();
    XMLWriter xmlWriter = new XMLWriter(os);
    xmlWriter.setPrettyPrint();
    DOMInternalRepresentation.parse(myDocument, xmlWriter);
    os.close();
    fc.close();

} catch (Exception e) {
    // Place exception handling code here
}

编辑 2: 添加了节点插入和 XML 到文件写入的代码示例

try {
    // Creating document
    Document myDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

    Element parentElement = myDocument.createElement("parentTag");

    // create first element and append it to parent
    Element firstElement = myDocument.createElement("firstElement");
    firstElement.appendChild(myDocument.createTextNode("1"));
    parentElement.appendChild(firstElement);

    // create third element and append it to parent
    Element thirdElement = myDocument.createElement("thirdElement");
    thirdElement.appendChild(myDocument.createTextNode("3"));
    parentElement.appendChild(thirdElement);

    // create second element and insert it between first and third elements
    Element secondElement = myDocument.createElement("secondElement");
    secondElement.appendChild(myDocument.createTextNode("2"));
    parentElement.insertBefore(secondElement, thirdElement);

    myDocument.appendChild(parentElement);

    // Writing document to file
    String filePath = "file:///store/home/user/XmlFile.xml";
    FileConnection fc = (FileConnection) Connector.open(filePath, Connector.READ_WRITE);
    if (!fc.exists()) {
        fc.create();  // create the file if it doesn't exist
    } else {
        fc.truncate(0); // truncate the file if it exists
    }

    OutputStream os = fc.openOutputStream();
    XMLWriter xmlWriter = new XMLWriter(os);
    xmlWriter.setPrettyPrint();
    DOMInternalRepresentation.parse(myDocument, xmlWriter);
    os.close();
    fc.close();            

} catch (ParserConfigurationException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (SAXException e) {
    e.printStackTrace();
}     

另请检查 这个问题有关在 BlackBerry 上创建 XML。

In order to insert new node(s) you should use Node#insertBefore() instead of Node#appendChild(). Check documentation here.

Replace

Node emp=myDocument.createElement("Emp");
Text NodeText = myDocument.createTextNode("DD");
emp.appendChild(NodeText);
myDocument.appendChild(emp); 

with

Node emp=myDocument.createElement("Emp");
Text NodeText = myDocument.createTextNode("DD");
emp.appendChild(NodeText); 
myDocument.insertBefore(emp, someExistingNode); 

Where someExistingNode is the Node (probably Element) before which you want to add your new Node emp.

Edit 1: How to write XML to file

try {
    String filePath = "file:///store/home/user/XmlFile.xml";
    FileConnection fc = (FileConnection) Connector.open(filePath, Connector.READ_WRITE);
    if (!fc.exists()) {
        fc.create();  // create the file if it doesn't exist
    } else {
        fc.truncate(0); // truncate the file if it exists
    }

    OutputStream os = fc.openOutputStream();
    XMLWriter xmlWriter = new XMLWriter(os);
    xmlWriter.setPrettyPrint();
    DOMInternalRepresentation.parse(myDocument, xmlWriter);
    os.close();
    fc.close();

} catch (Exception e) {
    // Place exception handling code here
}

Edit 2: Added code sample for node insertion and XML-to-file writing

try {
    // Creating document
    Document myDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

    Element parentElement = myDocument.createElement("parentTag");

    // create first element and append it to parent
    Element firstElement = myDocument.createElement("firstElement");
    firstElement.appendChild(myDocument.createTextNode("1"));
    parentElement.appendChild(firstElement);

    // create third element and append it to parent
    Element thirdElement = myDocument.createElement("thirdElement");
    thirdElement.appendChild(myDocument.createTextNode("3"));
    parentElement.appendChild(thirdElement);

    // create second element and insert it between first and third elements
    Element secondElement = myDocument.createElement("secondElement");
    secondElement.appendChild(myDocument.createTextNode("2"));
    parentElement.insertBefore(secondElement, thirdElement);

    myDocument.appendChild(parentElement);

    // Writing document to file
    String filePath = "file:///store/home/user/XmlFile.xml";
    FileConnection fc = (FileConnection) Connector.open(filePath, Connector.READ_WRITE);
    if (!fc.exists()) {
        fc.create();  // create the file if it doesn't exist
    } else {
        fc.truncate(0); // truncate the file if it exists
    }

    OutputStream os = fc.openOutputStream();
    XMLWriter xmlWriter = new XMLWriter(os);
    xmlWriter.setPrettyPrint();
    DOMInternalRepresentation.parse(myDocument, xmlWriter);
    os.close();
    fc.close();            

} catch (ParserConfigurationException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (SAXException e) {
    e.printStackTrace();
}     

Also check this question regarding XML creation on BlackBerry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文