0

i have a class which is returning a string type value and i want to return an String array, so please tell how can i able to do that

  1. i have an xml file like resource.xml
<prompts> 
        <prompt id="p1">welcome to</prompt> 
        <prompt id ="p2">stack overflow</prompt>
        <prompt id="p3">You entered</prompt> 
        <prompt id="p4">the correct number</prompt> 
<prompts>

i am parsing it using sax parser

    public class XmlReaderPrompt {  
        public List<PromptBean> load(String langMode)
        {
            String fileName="resource.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 (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;
        }
}

and a UserFunction class to return the text from the xml file

public class UserFunction{  

        List<PromptBean> promptObject = new ArrayList<PromptBean>(); 
public String getPromptFunction(String promptTag,String langMode )
        {           
            List<PromptBean> promptObject=xrpObject.load(langMode);
            for (Iterator<PromptBean> iterator = promptObject.iterator(); iterator.hasNext();){
            PromptBean promptBean= (PromptBean)iterator.next();             
            if(promptBean.getId().equalsIgnoreCase(promptTag)){
                return StringEscapeUtils.escapeXml(promptBean.getPrompt());
            }
            }
             return null;
        }

The problem is that I have to call the method getPromptFunction of UserFunction class every time I need to get text from the sub element like

String pr1 = UserFunction.getPromptFunction("p1" "resource");
String pr1 = UserFunction.getPromptFunction("p2" "resource");
String pr1 = UserFunction.getPromptFunction("p3" "resource");

and using it in jsp page as <%=pr1%>

So I want to use array like

String[] pr = UserFunction.getPromptFunction('"p1","p2","p3"' "resource")

So how I am able to do that and also tell how to use it in jsp page .

4

1 回答 1

1

你可以这样做

public String[] getPromptFunction(String promptTag,String langMode )
    {   
        String temp[] = new String[promptObject.size()];        
        List<PromptBean> promptObject=xrpObject.load(langMode);
        int i = 0; 
        for (Iterator<PromptBean> iterator = promptObject.iterator(); iterator.hasNext();) {
        PromptBean promptBean= (PromptBean)iterator.next();             
        if(promptBean.getId().equalsIgnoreCase(promptTag)){
            temp[i] = StringEscapeUtils.escapeXml(promptBean.getPrompt());
        }
        i++;
        }
        return temp;
    }
于 2012-10-15T10:55:52.670 回答