0

我有两个类,其中一个正在实现地图,我想将其更改为列表,但我面临的问题是 get 和 put 方法。

我的第一堂课是解析 xml 并将其返回到地图中,所以请帮我将其转换为列表 这是我的第一堂课

public class XmlReaderPrompt {  
    public Map<String, PromptBean> load(String langMode)
    {
        String fileName="db_processor/English.xml";     
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        InputStream prompt_configfile=Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
        DocumentBuilder db = null;
        Map<String, PromptBean> promptMap = new HashMap<String, PromptBean>();
        try {
            try {
                db = dbf.newDocumentBuilder();
            } catch (ParserConfigurationException e) {              
                e.printStackTrace();
            }
            Document doc = null;
            try {
                doc = db.parse(prompt_configfile);
            }
            catch(FileNotFoundException fnfexp)
            {
                fnfexp.printStackTrace();               
            }
            catch (SAXException e) {                
                e.printStackTrace();
            }           
            NodeList nodeList=doc.getElementsByTagName("prompt");
            for(int i=0;i<nodeList.getLength();i++)
            {
                Node node=nodeList.item(i);
                if(node.getNodeType()==Node.ELEMENT_NODE)
                {                                           
                    Element element=(Element)node;
                    String id = element.getAttribute("id");             
                    String name = element.getAttribute("name");                 
                    String prompt=getTextValue(element);
                    promptMap.put(id, new PromptBean(id,name,prompt));
                }
            }
        }        
        catch(Exception io)
        {
            io.printStackTrace();           
        }
        finally
        {
            db=null;
            dbf=null;
        }       
        return promptMap;
    }   

    private String getTextValue(Element element) {      
        String textValue=element.getFirstChild().getTextContent().toString();
        return textValue;
    }   

}

第二类功能是在我想使用列表时解析 xml 并将其返回到地图,但我在 Map 的 put ad get 方法中遇到错误

public class UserFunction{  

        Map<String,PromptBean> promptObject=new HashMap<String,PromptBean>(); 
        Map<String,AudioBean> audioObject = new HashMap<String,AudioBean>();

        XmlReaderPrompt xrpObject=new XmlReaderPrompt();

        public String getPromptFunction(String promptTag,String langMode )
        {           
            Map<String, PromptBean> promptObject=xrpObject.load(langMode);
            PromptBean promptBean= (PromptBean)promptObject.get(promptTag);             
            String pv=StringEscapeUtils.escapeXml(promptBean.getPrompt());          
            return pv;
        }
}
4

1 回答 1

1

头等舱

class XmlReaderPrompt {  
public List<PromptBean> load(String langMode)
{
    String fileName="db_processor/English.xml";     
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    InputStream prompt_configfile=Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    DocumentBuilder db = null;
    List<PromptBean> promptMap = new ArrayList<PromptBean>();
    try { 
        try {
            db = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {              
            e.printStackTrace();
        }
        Document doc = null;
        try {
            doc = db.parse(prompt_configfile);
        }
        catch(FileNotFoundException fnfexp)
        {
            fnfexp.printStackTrace();               
        }
        catch (SAXException e) {                
            e.printStackTrace();
        }           
        NodeList nodeList=doc.getElementsByTagName("prompt");
        for(int i=0;i<nodeList.getLength();i++)
        {
            Node node=nodeList.item(i);
            if(node.getNodeType()==Node.ELEMENT_NODE)
            {                                           
                Element element=(Element)node;
                String id = element.getAttribute("id");             
                String name = element.getAttribute("name");                 
                String prompt=getTextValue(element);
                promptMap.add(new PromptBean(id,name,prompt));
            }
        }
    }        
    catch(Exception io)
    {
        io.printStackTrace();           
    }
    finally
    {
        db=null;
        dbf=null;
    }       
    return promptMap;
}   

private String getTextValue(Element element) {      
    String textValue=element.getFirstChild().getTextContent().toString();
    return textValue;
}   

}

二等的方法

public String getPromptFunction(String promptTag,String langMode )
{           
    List<PromptBean> promptObject=xrpObject.load(langMode);
    for (Iterator iterator = promptObject.iterator(); iterator.hasNext();) {
        PromptBean promptBean = (PromptBean) iterator.next();
        if(promptBean.getId().equalsIgnoreCase(promptTag)){
            return StringEscapeUtils.escapeXml(promptBean.getPrompt());          
        }
    }
    return null;
} 
于 2012-10-01T07:07:45.297 回答