0

I have a xml file like this:

<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>

And a class for the categories

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

and the use

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 + "]]";
}
}

I want to get 1 category object with the other categories and usages in the arraylist but I can't figure out how to analyse it with the standard parsers.

I know how to do it with only one category and no sub-categories.

0

1 Answer 1

1

As mentioned you can use JAXB, this is a very basic example, just for you to see how easy is to work with this useful tool:

First, it would be better if you turn your XML into a schema, you can use:

http://www.freeformatter.com/xsd-generator.html

But if you wanna work with your class, then you can use annotations, for example, this is a basic class:

@XmlRootElement(name = "programmer")
public class ProgrammerBean implements Serializable {
 //Here attributes and getters and setters.
}

I will declare a class which is a singleton to manage my class:

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

Then, for usage you can:

XmlFileHandler xmlHandler = XmlFileHandler.getInstance();
ProgrammerBean myObject = (ProgrammerBean) xmlHandler.readXml("/home/developer/programmer.xml", ProgrammerBean.class);

Hope it helped you with the basics of JaxB, as mentioned it would be easier if you use an schema and generate jaxb classes from it, then it is just a matter of set and get methods. Best regards.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.