我有一个像这样的xml文件:
<passwords>
<use>
<title>LoL</title>
<username>hallo</username>
<password>asdasd</password>
<note>asdasdasdasd</note>
</use>
<E-Mail>
<use>
<title>GMail</title>
<username>hallo</username>
<password>asdasd</password>
<note>asdasdasdasd</note>
</use>
<Webmail>
<use>
<title>Yahoo</title>
<username>hallo</username>
<password>asdasd</password>
<note>asdasdasdasd</note>
</use>
</Webmail>
</E-Mail>
类别的类
public class Category {
private ArrayList<Usage> usages;
private ArrayList<Category> categories;
private String name;
public Category(String name){
this.name = name;
usages = new ArrayList<>();
categories = new ArrayList<>();
}
public void addUsage(Usage usage){
usages.add(usage);
}
public void addCategory(Category category){
categories.add(category);
}
}
和使用
public class Usage {
private String title;
private String username;
private String password;
private String notes;
public void setTitle(String title) {
this.title = title;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setNotes(String notes) {
this.notes = notes;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getNotes() {
return notes;
}
public String getTitle(){
return title;
}
public String toString(){
return "[[" + this.title + "][" + this.username + "][" + this.password + "][" + this.notes + "]]";
}
}
我想在arraylist中使用其他类别和用法获得1个类别对象,但我无法弄清楚如何使用标准解析器进行分析。
我知道如何只使用一个类别而不使用子类别。
答案 0 :(得分:1)
如上所述,您可以使用JAXB,这是一个非常基本的示例,只是为了让您了解使用这个有用的工具是多么容易:
首先,如果将XML转换为模式会更好,您可以使用:
http://www.freeformatter.com/xsd-generator.html
但是如果你想和你的班级一起工作,那么你可以使用注释,例如,这是一个基本类:
@XmlRootElement(name = "programmer")
public class ProgrammerBean implements Serializable {
//Here attributes and getters and setters.
}
我将声明一个用于管理我的类的单例类:
public class XMLFileHandler {
private static XMLFileHandler instance;
private XMLFileHandler() {
}
public static XMLFileHandler getInstance() {
if (instance == null) {
instance = new XMLFileHandler();
}
return instance;
}
public void writeXml(String filePath, Object targetObject)
throws FileNotFoundException, JAXBException {
File xmlFile = new File(filePath);
OutputStream xmlOutput = new FileOutputStream(xmlFile);
// Define a JAXBContext
JAXBContext context = null;
// When I write I use a marshaller
Marshaller xmlWriter = null;
// I set the class of the objet to write
context = JAXBContext.newInstance(targetObject.getClass());
// I create the marshaller
xmlWriter = context.createMarshaller();
// I set the properties for the output
xmlWriter.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Finally write
xmlWriter.marshal(targetObject, xmlOutput);
}
public Object readXml(String filePath, Class objType) throws JAXBException {
Object read = null;
JAXBContext context = null;
Unmarshaller xmlReader = null;
context = JAXBContext.newInstance(objType);
xmlReader = context.createUnmarshaller();
read = xmlReader.unmarshal(new File(filePath));
return read;
}
}
然后,您可以使用:
XmlFileHandler xmlHandler = XmlFileHandler.getInstance();
ProgrammerBean myObject = (ProgrammerBean) xmlHandler.readXml("/home/developer/programmer.xml", ProgrammerBean.class);
希望它能帮助您了解JaxB的基础知识,如上所述,如果您使用模式并从中生成jaxb类会更容易,那么它只是set和get方法的问题。最好的问候。