如何从 xml 中读取值并将其存储在 java 中的哈希映射中
How to read values from xml and store it in hash map in java
I am new to xml parsing.I have to retrieve these particular fields (USERNAME,PASSWORD) and needs to save in a hashmap in java
.
<?xml version="1.0"?>
<REQUEST version="10">
<AUTHENTICATION>
<USERNAME>ABC</USERNAME>
<PASSWORD>123</PASSWORD>
</AUTHENTICATION>
<DATA>
<ADDRESS>add1</ADDRESS>
</DATA>
</REQUEST>
有人可以帮忙吗?
提前致谢
您应该检查此以从 xml 文件中读取特定参数:
但是有更好的方法来处理 xml 文件。由于您没有提供更多详细信息,我将 post 一种从 xml 文件中读取数据的方法。
最简单的方法是定义一个类型来表示 xml 文件中的数据。然后您可以轻松地将数据读取和写入 xml 文件。
例如你定义类型 Person:
public class Person{
private String username="";
private String password="";
private String address="";
// Constructor(s), getters, setters...
}
当您想将数据保存到 xml 时,只需序列化 Person 对象:
public boolean serialize(Person p, String path) {
try {
XMLEncoder encoder = new XMLEncoder(new FileOutputStream(new File(path+File.separator+p.getUsername()+".xml")));
encoder.writeObject(s);
encoder.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
现在当你想从 xml 文件中读取时,只需将其反序列化为 Person 对象:
public Person deserialization(String name, String path) {
Person p=null;
try {
XMLDecoder decoder = new XMLDecoder(new FileInputStream(new File(path+File.separator+name+".xml")));
p=(Person) decoder.readObject();
decoder.close();
} catch (Exception e) {
e.printStackTrace();
}
return p;
}
最后,您可以更轻松地在 HashMap 中存储它:
HashMap<String, String> hash_map = new HashMap<String, String>();
hash_map.put(p.getUsername(),p.getPassword()); // p is a Person object
注意:这只是一种方法,您可以根据需要进行调整。你应该注意你提供的文件路径(如果你使用来自某些服务器的 xml 文件可能方法不同,这个例子参考使用来自本地文件系统的 xml 文件)。
另请注意,密码应受到保护,例如加密。
I am new to xml parsing.I have to retrieve these particular fields (USERNAME,PASSWORD) and needs to save in a hashmap in java
.
<?xml version="1.0"?>
<REQUEST version="10">
<AUTHENTICATION>
<USERNAME>ABC</USERNAME>
<PASSWORD>123</PASSWORD>
</AUTHENTICATION>
<DATA>
<ADDRESS>add1</ADDRESS>
</DATA>
</REQUEST>
有人可以帮忙吗?
提前致谢
您应该检查此以从 xml 文件中读取特定参数:
但是有更好的方法来处理 xml 文件。由于您没有提供更多详细信息,我将 post 一种从 xml 文件中读取数据的方法。
最简单的方法是定义一个类型来表示 xml 文件中的数据。然后您可以轻松地将数据读取和写入 xml 文件。
例如你定义类型 Person:
public class Person{
private String username="";
private String password="";
private String address="";
// Constructor(s), getters, setters...
}
当您想将数据保存到 xml 时,只需序列化 Person 对象:
public boolean serialize(Person p, String path) {
try {
XMLEncoder encoder = new XMLEncoder(new FileOutputStream(new File(path+File.separator+p.getUsername()+".xml")));
encoder.writeObject(s);
encoder.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
现在当你想从 xml 文件中读取时,只需将其反序列化为 Person 对象:
public Person deserialization(String name, String path) {
Person p=null;
try {
XMLDecoder decoder = new XMLDecoder(new FileInputStream(new File(path+File.separator+name+".xml")));
p=(Person) decoder.readObject();
decoder.close();
} catch (Exception e) {
e.printStackTrace();
}
return p;
}
最后,您可以更轻松地在 HashMap 中存储它:
HashMap<String, String> hash_map = new HashMap<String, String>();
hash_map.put(p.getUsername(),p.getPassword()); // p is a Person object
注意:这只是一种方法,您可以根据需要进行调整。你应该注意你提供的文件路径(如果你使用来自某些服务器的 xml 文件可能方法不同,这个例子参考使用来自本地文件系统的 xml 文件)。 另请注意,密码应受到保护,例如加密。