Rather, use JAXB. JAXP is an old and very verbose API. JAXB relies on Javabeans and is therefore clean and relatively simple. First create a Javabean that maps 1: 1 to an XML file using javax.xml.bind annotations.
@XmlRootElement public class Access { @XmlElement private User buyer; @XmlElement private User seller; @XmlElement private User administrator; public User getBuyer() { return buyer; } public User getSeller() { return seller; } public User getAdministrator() { return administrator; } public static class User { @XmlElement(name="page") private List<String> pages; public List<String> getPages() { return pages; } } }
Then do the next part to map it (provided that allowedpages.xml is placed at the root of the class path).
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("allowedpages.xml"); Access access = (Access) JAXBContext.newInstance(Access.class).createUnmarshaller().unmarshal(input);
Note that you should not use new File() for this. See Also getResourceAsStream() vs FileInputStream .
Finally, you can access all of the buyerβs pages as follows:
List<String> buyerPages = access.getBuyer().getPages();
Needless to say, home security is not always the best practice. Java EE 6 comes with container protection.
Balusc
source share