Skip to content

This small repository will help those who want to map xml elements to Java classes and then build a composition out of them.

License

Notifications You must be signed in to change notification settings

Rillde/JaxpToObjects

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JaxpToObjects

Purpose


This project is used to map xml elements into Java classes and compose them using Java API for XML Processing (JAXP). You may want to use this approach to make the xml elements clearer and at the same time encapsulate the logic for getting data out of the elements.

Download


JAR on release page

Simple example


Example xml elements:

<Bot token="11223">
    <Replies>
        <Reply>Text1</Reply>
        <Reply>Text2</Reply>
    </Replies>
</Bot>

An example of mapping xml elements with JaxpToObjects:

Bot

public class Bot extends AbstractComplexElement {
    private String token;
    private Replies replies;

    public Bot(Node node) {
        super(node);
        replies = new Replies(getElement("Replies"));
        token = getAttribute("token");
    }
    
    public String getToken() {
        return token;
    }

    public Replies getReplies() {
        return replies;
    }
}

Replies

public class Replies extends AbstractComplexElement {
    private List<Reply> replyList;

    public Replies(Node node) {
        super(node);
        replyList = new ArrayList();
        for (Node reply: getElements("Reply")) {
            replyList.add(new Reply(reply));
        }
    }

    public List<Reply> getReplyList() {
        return replyList;
    }
}

Reply

public class Reply extends AbstractSimpleElement {
    public Reply(Node node) {
        super(node);
    }
}

License

MIT

About

This small repository will help those who want to map xml elements to Java classes and then build a composition out of them.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages