Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用pullparser for hashmap创建xml文件_Android_Xml_Xmlpullparser - Fatal编程技术网

Android 如何使用pullparser for hashmap创建xml文件

Android 如何使用pullparser for hashmap创建xml文件,android,xml,xmlpullparser,Android,Xml,Xmlpullparser,我试图从用户的输入值填充xml文件。用户将给2个条目一个键和一个值。我有一个模型类,如下所示: public class Person { private HashMap<String, String> hash = new HashMap<String, String>(); public Person() { } public Person(String key, String val) { hash.put(key, val); } publi

我试图从用户的输入值填充xml文件。用户将给2个条目一个键和一个值。我有一个模型类,如下所示:

public class Person
{    private HashMap<String, String> hash = new HashMap<String, String>();

public Person()
{   }

public Person(String key, String val)
{ hash.put(key, val);   }


public String GetFirstName(String k)
{ return hash.get(k);   }

}
公共类人物
{private HashMap hash=new HashMap();
公众人士()
{   }
公众人物(字符串键、字符串值)
{hash.put(key,val);}
公共字符串GetFirstName(字符串k)
{返回hash.get(k);}
}
如何从类的这个对象生成xml?以及如何根据键从xml检索值

我希望xml如下所示:

<AllEntries>
   <entry key="key1">value1</entry> 
   <entry key="key2">value2</entry> 
   <entry key="key3">value3</entry>
</AllEntries> 

价值1
价值2
价值3

您需要使用类似Java或XML的XML解析器。下面的示例是JavaDOM,它向您展示了如何循环使用
HashMap
,并将条目添加到
your_xml.xml

File xmlFile = new File("your_xml.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);

for (Map.Entry<String, String> m : hash.entrySet()) {
    // Create your entry element and set it's value
    Element entry = doc.createElement("entry");
    entry.setTextContent(m.getValue());
    // Create an attribute, set it's value and add the attribute to your entry       
    Attr attr = doc.createAttribute("key");
    attr.setValue(m.getKey());
    entry.setAttributeNode(attr);
    // Append your entry to the root element
    doc.getDocumentElement().appendChild(entry);
}
File xmlFile=新文件(“your_xml.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(xmlFile);
对于(Map.Entry m:hash.entrySet()){
//创建entry元素并设置其值
元素条目=doc.createElement(“条目”);
entry.setTextContent(m.getValue());
//创建一个属性,设置其值并将该属性添加到条目中
Attr Attr=doc.createAttribute(“键”);
attr.setValue(m.getKey());
entry.setAttributeNode(attr);
//将条目附加到根元素
doc.getDocumentElement().appendChild(条目);
}

然后您只需要将文档保存在原始文件上。一旦您的
文档
被编辑,您可能会想要解析到您选择的
输出流
进行保存。

通过填充,我假设您的意思是向现有xml添加条目?这些参赛作品会去哪里?在根节点中?您希望条目如何显示?像这样的
?我们需要更多的信息。你应该附加到现有的xml中。我希望xml是这样的:value1 value2 value3可能将其添加到问题中并格式化它。它将帮助人们回答问题,并使你的答案更具体地针对你的问题。如果有人还没有帮你解决这个问题,我会在午餐休息时看一看那太好了,因为我必须在2小时内提交我的项目:-)什么是散列&如何在xml中附加我的数据,我是说新数据和旧数据?呃,还有如何在xml中添加hashmap?@ShaonHasan-
hash
是您的
hashmap
实例,正如您在问题中的示例代码所示。上面的代码将数据以您指定的格式添加到名为“your_file.xml”的xml文件中。您需要在那里指定文件位置;在活动开始时。但是hash.entrySet()在entrySet上显示错误:-(@ShaonHasan-
hash
需要是一个
HashMap
的实例,而不是一个
。我认为你需要阅读一本好的Java书籍或一些教程来理解作用域和对象是一种关系。我不确定我能否说服你通过所有这些来获得你需要的结果。这对于StackOverflow.k的答案来说太难了。.added HashMap hash=new HashMap();hash.put(“2”,“Shaon”);在活动中。而try-catch块中的代码也在活动中。我又做错了吗?