You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Hanson, Anthony (ITD)" <An...@state.ma.us> on 2005/01/20 16:20:24 UTC

Re: trouble mapping XML to HashMap

I am writing a component that takes an XML stream from the database then
uses betwixt to build a bean. The bean has two read only properties which
are mapping fine. The problem is mapping the HashMap. The problem is the
add/set method. Stepping through the code, I can see the registerBeanClass
doesn't invoke either method.
User.XML
<User>
<userID>ahanson</userID>
<displayName>Hanson, Anthony MD</displayName>
<security>
<entry className="java.util.HashMap">
<key>1</key>
<value>63</value>
</entry>
<entry className="java.util.HashMap">
<key>3</key>
<value>63</value>
</entry>
security>
</User>
 
I created the class and created this xml file using the beanwriter (except
for the className attribute). This was exactly the file I was starting with
except for the element names so I modified my file to match what the
beanwriter was creating.
 
User.class
import java.util.*;
public class User {
    private String displayName;
    private String userID;
    private HashMap security;
    public User() {}
    public User(String userID,String displayName,HashMap security) {
        setDisplayName(displayName);
        setUserID(userID);
       // setSecurity(security);
    }
    
    public java.lang.String getDisplayName() {
        return displayName;
    }
    
    public void setDisplayName(java.lang.String displayName) {
        this.displayName = displayName;
    }
    
    public java.lang.String getUserID() {
        return userID;
    }
 
    public void setUserID(java.lang.String userID) {
        this.userID = userID;
    }
    public String toString() {
        
        StringBuffer sb = new StringBuffer();
        sb.append("User[name='" + displayName + "',userid='" + userID +
"']");
        if (security == null){
            return sb.toString();
        }else{
        Set mappings = security.entrySet();
        for (Iterator i = mappings.iterator(); i.hasNext();) {
            Map.Entry me = (Map.Entry)i.next();
            sb.append(me);
        }
        }
        return sb.toString();
    }
    public boolean isAuthorized(String module,int secLevel) {
        Integer secModule = (Integer)security.get(module);
        if((secModule.intValue() & secLevel) == secLevel){
            return true;
        }else{
            return false;
        }
    }
    public java.util.HashMap getSecurity() {
        return security;
    }    
//    public void setSecurity(java.util.HashMap security) {
//        this.security = security;
//    }
        public void addSecurity(java.util.HashMap security) {
        this.security.putAll(security);
    }  
}
 
The latest thing I tried was setting the className attribute and using it
for the add method. Here is the code I call the write the bean. Please tell
me what I'm doing wrong.
            //sb is a string buffer containing the xml returned from the
database.
            String result = sb.toString();
            StringReader xmlReader = new StringReader(result);
            
            // Now convert this to a bean using betwixt
            // Create BeanReader
            BeanReader beanReader  = new BeanReader();
 
beanReader.getXMLIntrospector().setAttributesForPrimitives(false);
           
            beanReader.setMatchIDs(false);
            beanReader.registerBeanClass("User",   User.class);
            
            User user = (User) beanReader.parse(xmlReader);
            System.out.println(user); //only prints the displayname and
userid
 
Thanks
Anthony