无法连续将数据附加到现有XML文件

时间:2018-04-30 19:21:37

标签: java android xml

我试图创建一个XML文件。我可以第一次将数据附加到它,但不能追加更多的数据。我使用了以下代码。

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try {
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    //root elements
    Document doc = docBuilder.newDocument();

    Element contact1 = doc.createElement("Contact");
    doc.appendChild(contact1);

    Element contact = doc.createElement("ContactNo");
    contact.appendChild(doc.createTextNode("1"));
    contact1.appendChild(contact);

    Element firstname = doc.createElement("Number");
    firstname.appendChild(doc.createTextNode("553827575"));
    contact.appendChild(firstname);

    Element lastname = doc.createElement("Name");
    lastname.appendChild(doc.createTextNode("hfghhfgh"));
    contact.appendChild(lastname);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);

    File sdcardxml = Environment.getExternalStorageDirectory();

    File dirxml = new File(sdcardxml.getAbsolutePath() + 
      "/xml/");
    if(dirxml.mkdirs()){ }else{ }
    new File(dirxml, "contacts.xml");

    StreamResult result =  new StreamResult(new 
     File("/storage/sdcard0/xml/contacts.xml"));
    transformer.transform(source, result);

    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));

    String personXMLStringValue1 = writer.getBuffer().toString();
}
catch(ParserConfigurationException pce)
{
    pce.printStackTrace();
}
catch(TransformerException tfe)
{
    tfe.printStackTrace();
}

上面的代码是创建XML文件。这很好用。 然后,要将数据附加到现有XML文件,我已使用此代码。

try {
    File newXmlFile = new File(
            Environment.getExternalStorageDirectory() + "/xml/contacts.xml");
    String FILE_PATH = newXmlFile.getPath();

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

    if (newXmlFile.exists()) {
        docFactory.setValidating(false);

        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.parse(newXmlFile);
        doc.getDocumentElement().normalize();

        Node rootElement = doc.getFirstChild();
        NodeList rootList = doc.getElementsByTagName("Contact");
        Node root = rootList.item(0);

        Element sayingParent = doc.createElement("ContactNo");
        sayingParent.appendChild(doc.createTextNode("2"));
        root.appendChild(sayingParent);

        Element firstname = doc.createElement("Number");
        firstname.appendChild(doc.createTextNode(num));
        sayingParent.appendChild(firstname);

        Element lastname = doc.createElement("Name");
        lastname.appendChild(doc.createTextNode(name));
        sayingParent.appendChild(lastname);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(FILE_PATH));
        transformer.transform(source, result);          
    }
}
catch (Exception e)
{
    e.printStackTrace();
}

我希望将以下XML文件作为输出:

<?xml version="1.0" encoding="UTF-8"?>
<Contact>
  <ContactNo>1
     <Number>55535</Number>
     <Name>hdhfhg</Name>
  </ContactNo>
  <ContactNo>2
     <Number>242585</Number>
     <Name>sdasds</Name>
  </ContactNo>
  <ContactNo>2
     <Number>454565</Number>
     <Name>grgfdgf</Name>
  </ContactNo>
</Contact>

事实上,数据并不是追加更多的数据。

1 个答案:

答案 0 :(得分:0)

尝试使用while或for循环

,而不是使用if语句
while (newXmlFile.exists()) {
      do something