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

时间:2012-02-17 11:58:05

标签: file blackberry xml-parsing

我正在尝试解析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);

1 个答案:

答案 0 :(得分:1)

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

替换

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 Node(可能是Element),您希望在其中添加新的Node \ temp

编辑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创建的this question