如何使此代码输出第二个节点?

时间:2013-02-25 17:35:20

标签: android xml-serialization

如何让它输出第二个节点?目前它似乎恰好位于旧数据的顶部。我的导师说它应该有效,但事实并非如此。

protected void ModifyXMLFile(String sName, String sClue1, String sClue2,
                String sClue3, String sAnswer, String sLocation, String sPoints) {
            // Write Data to a File in XML Format

            try {
                FileOutputStream mOutput = openFileOutput(FILENAME, Activity.MODE_PRIVATE);
                //we create a XmlSerializer in order to write xml data

                XmlSerializer serializer = Xml.newSerializer();

                try {

                    //we set the FileOutputStream as output for the serialize, using UTF-8 encoding

                    serializer.setOutput(mOutput, "UTF-8");

                    //Write <?xml declaration with encoding (if encoding not null) and stand alone flag (if stand alone not null)

                    serializer.startDocument(null, Boolean.valueOf(true));

                    //set indentation option

                    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

                    //start a tag called "root"
                    serializer.startTag(null, "Items");

                    //i indent code just to have a view similar to xml-tree
                    serializer.startTag(null, "Treasure");

                        serializer.startTag(null, "Name");
                        serializer.attribute(null, "Name", sName);
                        serializer.endTag(null, "Name");

                        serializer.startTag(null, "Clue1");
                        serializer.attribute(null, "Clue1", sClue1);
                        serializer.endTag(null, "Clue1");

                        serializer.startTag(null, "Clue2");
                        serializer.attribute(null, "Clue2", sClue2);
                        serializer.endTag(null, "Clue2");

                        serializer.startTag(null, "Clue3");
                        serializer.attribute(null, "Clue3", sClue3);
                        serializer.endTag(null, "Clue3");

                        serializer.startTag(null, "Answer");
                        serializer.attribute(null, "Answer", sAnswer);
                        serializer.endTag(null, "Answer");

                        serializer.startTag(null, "Location");
                        serializer.attribute(null, "Location", sLocation);
                        serializer.endTag(null, "Location");

                        serializer.startTag(null, "Points");
                        serializer.attribute(null, "Points", sPoints);
                        serializer.endTag(null, "Points");

                    serializer.endTag(null, "Treasure");

                serializer.endTag(null, "Items");


                serializer.endDocument();

                //write xml data into the FileOutputStream

                serializer.flush();

                //finally we close the file stream

                mOutput.close();

                } catch (Exception e) {

                Log.e("Exception","error occurred while creating xml file");

                }

             //   mOutput.write(data.getBytes());
             //   mOutput.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            }

Current输出如下所示。

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Items>
  <Treasure>
    <Name Name="p" />
    <Clue1 Clue1="l" />
    <Clue2 Clue2="m" />
    <Clue3 Clue3="o" />
    <Answer Answer="k" />
    <Location Location="n" />
    <Points Points="i" />
  </Treasure>
</Items>

在第二次通过之后它应该是这样的。

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Items>
  <Treasure>
    <Name Name="Treasure 1" />
    <Clue1 Clue1="l" />
    <Clue2 Clue2="m" />
    <Clue3 Clue3="o" />
    <Answer Answer="k" />
    <Location Location="n" />
    <Points Points="i" />
  </Treasure>
 <Treasure>
    <Name Name="Treasure 2" />
    <Clue1 Clue1="b" />
    <Clue2 Clue2="c" />
    <Clue3 Clue3="d" />
    <Answer Answer="e" />
    <Location Location="f" />
    <Points Points="g" />
  </Treasure>
</Items>

好的,我添加了以下更改

FileOutputStream mOutput = openFileOutput(FILENAME, Activity.MODE_APPEND);

我也添加了这一行,因为它似乎缺失了。

StringWriter writer = new StringWriter();

当我开始这个时,我得到了同样的东西。必须有更好的方法。

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Items>
  <Treasure>
    <Name Name="quit" />
    <Clue1 Clue1="was" />
    <Clue2 Clue2="each" />
    <Clue3 Clue3="race" />
    <Answer Answer="the" />
    <Location Location="life" />
    <Points Points="35" />
  </Treasure>
</Items><?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Items>
  <Treasure>
    <Name Name="zone" />
    <Clue1 Clue1="Xbox" />
    <Clue2 Clue2="city" />
    <Clue3 Clue3="very" />
    <Answer Answer="but" />
    <Location Location="new" />
    <Points Points="12" />
  </Treasure>
</Items>

1 个答案:

答案 0 :(得分:1)

这是代码。

protected void ModifyXMLFile(String sName, String sClue1, String sClue2,
        String sClue3, String sAnswer, String sLocation, String sPoints) {

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = null;

        File file = getFileStreamPath(FILENAME);
        if(!file.exists()) {
            // Create an empty xml file
            FileOutputStream mOutput = openFileOutput(FILENAME, Activity.MODE_PRIVATE);
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(mOutput, "UTF-8");
            serializer.startDocument(null, Boolean.valueOf(true));
            serializer.startTag(null, "Items");
            serializer.endTag(null, "Items");
            serializer.endDocument();
            serializer.flush();
            mOutput.close();    
        }

        // Parse the existing xml file
        FileInputStream fis = openFileInput(FILENAME);
        doc = db.parse(fis);
        fis.close();

        // Append new Treasure
        Element treasure = doc.createElement("Treasure");
        treasure.appendChild(createChild(doc, "Name", sName));
        treasure.appendChild(createChild(doc, "Clue1", sClue1));
        treasure.appendChild(createChild(doc, "Clue2", sClue2));
        treasure.appendChild(createChild(doc, "Clue3", sClue3));
        treasure.appendChild(createChild(doc, "Answer", sAnswer));
        treasure.appendChild(createChild(doc, "Location", sLocation));
        treasure.appendChild(createChild(doc, "Points", sPoints));

        Element items = doc.getDocumentElement();
        items.appendChild(treasure);

        // Write the xml file
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        FileOutputStream fos = openFileOutput(FILENAME, MODE_PRIVATE);
        t.transform(new DOMSource(doc), new StreamResult(fos));
        fos.close();

    } catch (Exception e) {
        e.printStackTrace();
    }       
}

private Element createChild(Document doc, String name, String value) {
    Element child = doc.createElement(name);
    child.setAttribute(name, value);
    return child;       
}