JAXB地图与列表

时间:2012-03-05 20:24:10

标签: xml list map jaxb

我必须道歉,我发誓不要问这个问题,因为这是一个共同的主题,但尽管有类似的问题,我仍然有问题。

更新:我发现我的代码中有一些错误已修复,但仍然失败。我更新了帖子以反映最后的更改。

更新2:我决定简化代码,而不是使用方法设置属性,我将所有属性更改为公共并删除方法。现在它编译并运行没有问题,但生成的Map是空的。这是新代码。

public class BCLURLClearanceIssuer
{
private File xml = null;
private JAXBContext aJAXBContext = null;
private Unmarshaller unmarshaller = null;
private BCLURLRestrictions aURLRestrictionList = null;

public BCLURLClearanceIssuer()
    {
    this.xml = new File("/www/hosts/www.bucle.co/webapps/ROOT/WEB-INF/BCLURLRestrictions.xml");
    try{
        this.aJAXBContext = JAXBContext.newInstance(BCLURLRestrictions.class);
        }
    catch (JAXBException e)
        {
         System.out.println(e);
        }
    try
        {
        this.unmarshaller = aJAXBContext.createUnmarshaller();
        }
    catch (JAXBException e2)
        {
        System.out.println(e2);
        }

        unmarshaller.setAdapter(new BCLMapAdapter());
    try
        {
        this.aURLRestrictionList = (BCLURLRestrictions) unmarshaller.unmarshal(this.xml);
        }
    catch(JAXBException e3)
        {
        System.out.println(e3);
        }
    }

public Map<String,BCLURLRestriction> getRestrictions()
    {
    return this.aURLRestrictionList.getRestrictions();
    }
}

我的目标是读取这样的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<BCLURLRestrictions>
<restriction>
    <appName>app1</appName>
    <url>/apps/app1/</url>
    <page>app1.html</page>
    <page>app1b.html</page>
</restriction>
<restriction>
    <appName>app2</appName>
    <url>/apps/app2/</url>
    <page>app2.html</page>
</restriction>
</BCLURLRestrictions>

这些是所涉及的课程。

BCLURLRestriction.java

public class BCLURLRestriction
    {

    @XmlElement(name = "AppName")
    public String name;

    @XmlElement(name = "url")
    public List<String> urls;

    @XmlElement(name = "page")
    public List<String> pages;


    }

BCLURLRestrictions.java

@XmlRootElement(name = "BCLURLRestrictions")
@XmlAccessorType(XmlAccessType.FIELD)
public class BCLURLRestrictions
{

@XmlJavaTypeAdapter(BCLMapAdapter.class)
public Map <String,BCLURLRestriction> restrictionMap = new HashMap<String,BCLURLRestriction>();

public Map <String,BCLURLRestriction>  getRestrictionMap()
    {
    return this.restrictionMap;
    }

public void  setRestrictionMap(Map aMap)
    {
    this.restrictionMap = aMap;
    }
}

BCLURLRestrictionListType.java

public class BCLURLRestrictionsListType 
{


public List <BCLURLRestriction> restrictionsList = new ArrayList<BCLURLRestriction>();

public BCLURLRestrictionsListType()
    {
    }

}

BCLMapAdapter.java

public class BCLMapAdapter extends XmlAdapter<BCLURLRestrictionsListType,Map<String,BCLURLRestriction>> 
{

public BCLMapAdapter()
    {

    }

public Map<String,BCLURLRestriction> unmarshal(BCLURLRestrictionsListType aRestrictionListType) throws Exception 
    {
    HashMap<String,BCLURLRestriction> aRestrictionMap = new HashMap<String,BCLURLRestriction>();
    for(BCLURLRestriction aRestriction: aRestrictionListType.restrictionsList)
        {
        aRestrictionMap.put(aRestriction.name,aRestriction);
        }
    return aRestrictionMap;
    }


public BCLURLRestrictionsListType marshal(Map<String,BCLURLRestriction> aRestrictionMap) throws Exception
    {
    BCLURLRestrictionsListType aRestrictionsListType = new BCLURLRestrictionsListType();
    for(Entry<String, BCLURLRestriction> aRestrictionEntry: aRestrictionMap.entrySet())
        {
        BCLURLRestriction aRestriction = new BCLURLRestriction();
        aRestriction.name = aRestrictionEntry.getValue().name;
        aRestriction.urls = aRestrictionEntry.getValue().urls;
        aRestriction.pages = aRestrictionEntry.getValue().pages;
        aRestrictionsListType.restrictionsList.add(aRestriction);
        }

    return aRestrictionsListType;
    }


}

建议都欢迎!

0 个答案:

没有答案