xml写作有困难

时间:2014-03-05 07:33:38

标签: android xml

我试图使用xmlSerializer创建一个xml文件。这将从ui,username&密码。因此,每个条目都将附加到xml文件中。 如果我使用下面的代码,它只保留最后一个条目 -

FileOutputStream fileos= getApplicationContext().openFileOutput(xmlFile, Context.MODE_PRIVATE);

同样,如果我使用MODE_APPEND而不是私有,则需要使用所有xml标签 - 如同版本等等。

FileOutputStream fileos= getApplicationContext().openFileOutput(xmlFile, Context.MODE_APPEND);

但我只需要附加条目 - 这是xml文件中的用户名和密码标记。

以下是我的总代码:

public class MainActivity extends Activity 
{

EditText txtKey;
EditText txtValue;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtKey = (EditText)findViewById(R.id.txtvwKey);
    txtValue = (EditText)findViewById(R.id.txtvwValue);
    btn = (Button)findViewById(R.id.btnSave);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}




public void saveClk(View v) 
throws FileNotFoundException, SAXException
{
    final String xmlFile="userMemo.xml";
    String key= txtKey.getText().toString();
    String val= txtValue.getText().toString();

    try {
    FileOutputStream fileos= getApplicationContext().openFileOutput(xmlFile, Context.MODE_APPEND);

    XmlSerializer xmlSerializer = Xml.newSerializer();              
    StringWriter writer = new StringWriter();
    xmlSerializer.setOutput(writer);
    xmlSerializer.startDocument("UTF-8",true);
    xmlSerializer.startTag(null, "userData");
        xmlSerializer.startTag(null, "userName");
            xmlSerializer.text(key);
        xmlSerializer.endTag(null,"userName");
        xmlSerializer.startTag(null,"password");
            xmlSerializer.text(val);
        xmlSerializer.endTag(null, "password");             
    xmlSerializer.endTag(null, "userData");
    xmlSerializer.endDocument();
    xmlSerializer.flush();
    String dataWrite=writer.toString();
    fileos.write(dataWrite.getBytes());
    fileos.close();
    } 


    catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

 }
}

我将如何解决它?

1 个答案:

答案 0 :(得分:0)

您正在使用xerces-j吗? 除非它是一个非常大的(比如几百个Mo)xml文件,否则我建议你使用DOM代替SAX,更具可读性。

看起来像是:

public void saveClk(View v) throws FileNotFoundException, SAXException {
    final String xmlFile="userMemo.xml";
    // the name I suppose
    String key= txtKey.getText().toString();
    // the password I suppose
    String val= txtValue.getText().toString();

    // Load the xml from the file
    DOMParser parser = new DOMParser();
    parser.parse(xmlFile);
    Document doc = parser.getDocument();

    // Create a userData node: <userData></userData>
    Node userDataNode = dom.createElement("userData");

    // Create a username node: <userName></userName>
    Node userNameNode = dom.createElement("userName");
    // Add the userName node a  value as a child text node
    // -> <userName>username</userName>
    Text nodeVal = dom.createTextNode(key);
    userNameNode.appendChild(nodeVal);

    // Create a password node: <password></password>
    Node passwordNode = dom.createElement("password");
    // Add the userName node a  value as a child text node
    // -> <password>password</password>
    nodeVal = dom.createTextNode(value);
    passwordNode.appendChild(nodeVal);

    // -> <userData><userName>username</userName><password>password</password></userData>
    userDataNode.appendChild(userNameNode);
    userDataNode.appendChild(passwordNode);

    // Get the document's root XML node
    NodeList root = doc.getChildNodes();
    // append the newly created node as a new child (so keeping the existing data)
    root.appendChild(userDataNode);

    // Write updated XML
    doc = parser.getDocument();
    OutputFormat format = new OutputFormat(doc);
    format.setIndenting(true);
    XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File("userMemo.xml")), format);
    serializer.serialize(doc);
}

您尝试的问题是MODE_APPEND与文件编写器有关,而与xml序列化程序无关。