有困难的XML写作 [英] having difficulty with xml writing

查看:108
本文介绍了有困难的XML写作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用XmlSerializer的XML文件。它会从用户界面,用户名和放大器2的值;密码。因此,每个条目将被追加到XML文件。 如果我使用下面的code,比只保留了最后一项 -

I was trying to make a xml file using xmlSerializer. Which will take 2 values from ui, username & password. So, each entry will be appended to the xml file. If i use the below code, than it only keeps the last entry-

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

此外,如果我使用的,而不是私人MODE_APPEND比通吃给予般的版本等信息的XML标记。

Again, if i use MODE_APPEND instead of private than it takes all the xml tags given -like version and so on.

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

但我需要追加只有─这是在XML文件中的用户名和密码标记的条目。

But i need to append the entries only- which is username and password tag in the xml file.

下面是我的总code:

Below is my total code:

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();
    }

 }
}

我应该如何去解决呢?

How am i going to solve it?

推荐答案

您正在使用的Xerces-J对不对? 除非这是一个非常大的(比如几百莫)的XML文件,我建议你使用DOM而非SAX,更具可读性。

You're using xerces-j right? Unless it's a very big (say some hundreds of Mo) xml file, I suggest you use DOM instead of SAX, more readable.

这将是这样的:

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序列化。

The problem in your attempt is that the MODE_APPEND is related to the file writer, not to the xml serializer.

这篇关于有困难的XML写作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆